2020-11-20 04:12:56 +08:00
|
|
|
// sol3
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2016-06-04 09:40:23 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2020-08-13 22:42:37 +08:00
|
|
|
// Copyright (c) 2013-2020 Rapptz, ThePhD and contributors
|
2016-06-04 09:40:23 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_PROPERTY_HPP
|
|
|
|
#define SOL_PROPERTY_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/ebco.hpp>
|
2017-07-10 00:00:57 +08:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
2016-06-04 09:40:23 +08:00
|
|
|
namespace sol {
|
2019-02-11 04:02:40 +08:00
|
|
|
namespace detail {
|
2020-11-20 04:12:56 +08:00
|
|
|
struct no_prop { };
|
|
|
|
} // namespace detail
|
2016-06-04 09:40:23 +08:00
|
|
|
|
|
|
|
template <typename R, typename W>
|
2019-02-11 05:40:37 +08:00
|
|
|
struct property_wrapper : detail::ebco<R, 0>, detail::ebco<W, 1> {
|
|
|
|
private:
|
|
|
|
using read_base_t = detail::ebco<R, 0>;
|
|
|
|
using write_base_t = detail::ebco<W, 1>;
|
|
|
|
|
|
|
|
public:
|
2016-06-04 09:40:23 +08:00
|
|
|
template <typename Rx, typename Wx>
|
2020-11-20 04:12:56 +08:00
|
|
|
property_wrapper(Rx&& r, Wx&& w) : read_base_t(std::forward<Rx>(r)), write_base_t(std::forward<Wx>(w)) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-06-04 09:40:23 +08:00
|
|
|
|
2019-02-11 04:02:40 +08:00
|
|
|
W& write() {
|
2019-02-11 05:40:37 +08:00
|
|
|
return write_base_t::value();
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
2019-02-11 04:02:40 +08:00
|
|
|
|
|
|
|
const W& write() const {
|
2019-02-11 05:40:37 +08:00
|
|
|
return write_base_t::value();
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
2019-02-11 04:02:40 +08:00
|
|
|
|
|
|
|
R& read() {
|
2019-02-11 05:40:37 +08:00
|
|
|
return read_base_t::value();
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
2019-02-11 04:02:40 +08:00
|
|
|
|
|
|
|
const R& read() const {
|
2019-02-11 05:40:37 +08:00
|
|
|
return read_base_t::value();
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
2019-02-11 04:02:40 +08:00
|
|
|
};
|
2016-06-04 09:40:23 +08:00
|
|
|
|
|
|
|
template <typename F, typename G>
|
|
|
|
inline decltype(auto) property(F&& f, G&& g) {
|
|
|
|
typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
|
|
|
|
typedef lua_bind_traits<meta::unqualified_t<G>> right_traits;
|
2019-02-11 04:02:40 +08:00
|
|
|
if constexpr (left_traits::free_arity < right_traits::free_arity) {
|
|
|
|
return property_wrapper<std::decay_t<F>, std::decay_t<G>>(std::forward<F>(f), std::forward<G>(g));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return property_wrapper<std::decay_t<G>, std::decay_t<F>>(std::forward<G>(g), std::forward<F>(f));
|
|
|
|
}
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
inline decltype(auto) property(F&& f) {
|
|
|
|
typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
|
2019-02-11 04:02:40 +08:00
|
|
|
if constexpr (left_traits::free_arity < 2) {
|
|
|
|
return property_wrapper<std::decay_t<F>, detail::no_prop>(std::forward<F>(f), detail::no_prop());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return property_wrapper<detail::no_prop, std::decay_t<F>>(detail::no_prop(), std::forward<F>(f));
|
|
|
|
}
|
2016-08-08 08:56:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
inline decltype(auto) readonly_property(F&& f) {
|
2019-02-11 04:02:40 +08:00
|
|
|
return property_wrapper<std::decay_t<F>, detail::no_prop>(std::forward<F>(f), detail::no_prop());
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
|
|
|
|
2017-04-26 07:44:53 +08:00
|
|
|
template <typename F>
|
|
|
|
inline decltype(auto) writeonly_property(F&& f) {
|
2019-02-11 04:02:40 +08:00
|
|
|
return property_wrapper<detail::no_prop, std::decay_t<F>>(detail::no_prop(), std::forward<F>(f));
|
2017-04-26 07:44:53 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 22:32:37 +08:00
|
|
|
template <typename T>
|
2019-02-11 05:40:37 +08:00
|
|
|
struct readonly_wrapper : detail::ebco<T> {
|
2019-02-11 04:02:40 +08:00
|
|
|
private:
|
2019-02-11 05:40:37 +08:00
|
|
|
using base_t = detail::ebco<T>;
|
|
|
|
|
2019-02-11 04:02:40 +08:00
|
|
|
public:
|
|
|
|
using base_t::base_t;
|
2017-05-29 22:32:37 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
operator T&() {
|
2019-02-11 05:40:37 +08:00
|
|
|
return base_t::value();
|
2017-05-29 22:32:37 +08:00
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
operator const T&() const {
|
2019-02-11 05:40:37 +08:00
|
|
|
return base_t::value();
|
2017-05-29 22:32:37 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-04 09:40:23 +08:00
|
|
|
// Allow someone to make a member variable readonly (const)
|
|
|
|
template <typename R, typename T>
|
2017-09-13 14:46:56 +08:00
|
|
|
inline auto readonly(R T::*v) {
|
2017-05-29 22:32:37 +08:00
|
|
|
return readonly_wrapper<meta::unqualified_t<decltype(v)>>(v);
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-07 05:29:07 +08:00
|
|
|
template <typename T>
|
2019-02-11 05:40:37 +08:00
|
|
|
struct var_wrapper : detail::ebco<T> {
|
2019-02-11 04:02:40 +08:00
|
|
|
private:
|
2019-02-11 05:40:37 +08:00
|
|
|
using base_t = detail::ebco<T>;
|
|
|
|
|
2019-02-11 04:02:40 +08:00
|
|
|
public:
|
|
|
|
using base_t::base_t;
|
2016-08-07 05:29:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename V>
|
|
|
|
inline auto var(V&& v) {
|
2019-05-18 08:02:49 +08:00
|
|
|
typedef std::decay_t<V> T;
|
2016-08-07 05:29:07 +08:00
|
|
|
return var_wrapper<T>(std::forward<V>(v));
|
|
|
|
}
|
|
|
|
|
2017-05-29 22:32:37 +08:00
|
|
|
namespace meta {
|
|
|
|
template <typename T>
|
2020-11-20 04:12:56 +08:00
|
|
|
using is_member_object = std::integral_constant<bool, std::is_member_object_pointer_v<T> || is_specialization_of_v<T, readonly_wrapper>>;
|
2017-05-29 22:32:37 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2020-11-20 04:12:56 +08:00
|
|
|
inline constexpr bool is_member_object_v = is_member_object<T>::value;
|
2019-01-29 00:32:32 +08:00
|
|
|
|
|
|
|
template <typename T>
|
2020-11-20 04:12:56 +08:00
|
|
|
using is_member_object_or_function = std::integral_constant<bool, is_member_object_v<T> || std::is_member_pointer_v<T>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr bool is_member_object_or_function_v = is_member_object_or_function<T>::value;
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace meta
|
2017-05-29 22:32:37 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-06-04 09:40:23 +08:00
|
|
|
|
|
|
|
#endif // SOL_PROPERTY_HPP
|