1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
// 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
#include <sol/types.hpp>
#include <sol/ebco.hpp>
#include <type_traits>
#include <utility>
namespace sol {
namespace detail {
struct no_prop { };
} // namespace detail
template <typename R, typename W>
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:
template <typename Rx, typename Wx>
property_wrapper(Rx&& r, Wx&& w) : read_base_t(std::forward<Rx>(r)), write_base_t(std::forward<Wx>(w)) {
}
W& write() {
return write_base_t::value();
}
const W& write() const {
return write_base_t::value();
}
R& read() {
return read_base_t::value();
}
const R& read() const {
return read_base_t::value();
}
};
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;
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));
}
}
template <typename F>
inline decltype(auto) property(F&& f) {
typedef lua_bind_traits<meta::unqualified_t<F>> left_traits;
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));
}
}
template <typename F>
inline decltype(auto) readonly_property(F&& f) {
return property_wrapper<std::decay_t<F>, detail::no_prop>(std::forward<F>(f), detail::no_prop());
}
template <typename F>
inline decltype(auto) writeonly_property(F&& f) {
return property_wrapper<detail::no_prop, std::decay_t<F>>(detail::no_prop(), std::forward<F>(f));
}
template <typename T>
struct readonly_wrapper : detail::ebco<T> {
private:
using base_t = detail::ebco<T>;
public:
using base_t::base_t;
operator T&() {
return base_t::value();
}
operator const T&() const {
return base_t::value();
}
};
// Allow someone to make a member variable readonly (const)
template <typename R, typename T>
inline auto readonly(R T::*v) {
return readonly_wrapper<meta::unqualified_t<decltype(v)>>(v);
}
template <typename T>
struct var_wrapper : detail::ebco<T> {
private:
using base_t = detail::ebco<T>;
public:
using base_t::base_t;
};
template <typename V>
inline auto var(V&& v) {
typedef std::decay_t<V> T;
return var_wrapper<T>(std::forward<V>(v));
}
namespace meta {
template <typename T>
using is_member_object = std::integral_constant<bool, std::is_member_object_pointer_v<T> || is_specialization_of_v<T, readonly_wrapper>>;
template <typename T>
inline constexpr bool is_member_object_v = is_member_object<T>::value;
template <typename T>
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;
} // namespace meta
} // namespace sol
#endif // SOL_PROPERTY_HPP
|