29 #include <string_view>
33 std::decay_t<T>
lua_check(lua_State *L,
int n);
37 inline static std::map<std::string_view , std::reference_wrapper<luaW_Registry>>
lookup;
38 using getters_list = std::map<std::string , std::function<bool(lua_State* L,
bool nop)>>;
41 using setters_list = std::map<std::string, std::function<bool(lua_State* L,
int idx,
bool nop)>>;
56 int get(lua_State* L);
58 int set(lua_State* L);
60 int dir(lua_State* L);
65 template<
typename object_type,
typename value_type>
68 virtual value_type
get(lua_State* L,
const object_type& obj)
const = 0;
72 template<
typename object_type,
typename value_type>
75 virtual void set(lua_State* L, object_type& obj,
const value_type& value)
const = 0;
79 template<
typename object_type>
82 virtual bool is_active(lua_State* L,
const object_type& obj)
const = 0;
88 template<
typename object_type,
typename value_type,
typename action_type, lua_attrfunc_type type>
92 using map_type = std::conditional_t<type == lua_attrfunc_type::validator, luaW_Registry::validators_list, std::conditional_t<type == lua_attrfunc_type::setter, luaW_Registry::setters_list, luaW_Registry::getters_list>>;
93 using callback_type =
typename map_type::mapped_type;
98 fcn = [action = action_type()](lua_State* L,
int idx,
bool nop) {
101 action.set(L, obj, lua_check<value_type>(L, idx));
106 fcn = [action = action_type()](lua_State* L,
bool nop) {
113 fcn = [action = action_type()](lua_State* L) {
117 (*map)[std::string(name)] = fcn;
120 #define LATTR_MAKE_UNIQUE_ID(base, id, obj_name) BOOST_PP_CAT(BOOST_PP_CAT(base, id), BOOST_PP_CAT(_for_, obj_name))
122 #define LATTR_GETTER5(name, value_type, obj_type, obj_name, id) \
123 struct LATTR_MAKE_UNIQUE_ID(getter_, id, obj_name) : public lua_getter<obj_type, value_type> { \
124 using object_type = obj_type; \
125 virtual value_type get(lua_State* L, const object_type& obj_name) const override; \
127 struct LATTR_MAKE_UNIQUE_ID(getter_adder_, id, obj_name) { \
128 LATTR_MAKE_UNIQUE_ID(getter_adder_, id, obj_name) () \
130 register_lua_attribute<obj_type, value_type, LATTR_MAKE_UNIQUE_ID(getter_, id, obj_name), lua_attrfunc_type::getter>(name); \
133 static LATTR_MAKE_UNIQUE_ID(getter_adder_, id, obj_name) LATTR_MAKE_UNIQUE_ID(getter_adder_instance_, id, obj_name) ; \
134 value_type LATTR_MAKE_UNIQUE_ID(getter_, id, obj_name)::get([[maybe_unused]] lua_State* L, const LATTR_MAKE_UNIQUE_ID(getter_, id, obj_name)::object_type& obj_name) const
137 #define LATTR_SETTER5(name, value_type, obj_type, obj_name, id) \
138 struct LATTR_MAKE_UNIQUE_ID(setter_, id, obj_name) : public lua_setter<obj_type, value_type> { \
139 using object_type = obj_type; \
140 void set(lua_State* L, object_type& obj_name, const value_type& value) const override; \
142 struct LATTR_MAKE_UNIQUE_ID(setter_adder_, id, obj_name) { \
143 LATTR_MAKE_UNIQUE_ID(setter_adder_, id, obj_name) ()\
145 register_lua_attribute<obj_type, value_type, LATTR_MAKE_UNIQUE_ID(setter_, id, obj_name), lua_attrfunc_type::setter>(name); \
148 static LATTR_MAKE_UNIQUE_ID(setter_adder_, id, obj_name) LATTR_MAKE_UNIQUE_ID(setter_adder_instance_, id, obj_name); \
149 void LATTR_MAKE_UNIQUE_ID(setter_, id, obj_name)::set([[maybe_unused]] lua_State* L, LATTR_MAKE_UNIQUE_ID(setter_, id, obj_name)::object_type& obj_name, const value_type& value) const
152 #define LATTR_VALID5(name, obj_type, obj_name, id) \
153 struct LATTR_MAKE_UNIQUE_ID(check_, id, obj_name) : public lua_validator<obj_type> { \
154 using object_type = obj_type; \
155 bool is_active(lua_State* L, const object_type& obj_name) const override; \
157 struct LATTR_MAKE_UNIQUE_ID(check_adder_, id, obj_name) { \
158 LATTR_MAKE_UNIQUE_ID(check_adder_, id, obj_name) ()\
160 register_lua_attribute<obj_type, void, LATTR_MAKE_UNIQUE_ID(check_, id, obj_name), lua_attrfunc_type::validator>(name); \
163 static LATTR_MAKE_UNIQUE_ID(check_adder_, id, obj_name) LATTR_MAKE_UNIQUE_ID(check_adder_instance_, id, obj_name); \
164 bool LATTR_MAKE_UNIQUE_ID(check_, id, obj_name)::is_active([[maybe_unused]] lua_State* L, const LATTR_MAKE_UNIQUE_ID(check_, id, obj_name)::object_type& obj_name) const
173 #define LATTR_GETTER(name, value_type, obj_type, obj_name) LATTR_GETTER5(name, value_type, obj_type, obj_name, __LINE__)
175 #define LATTR_SETTER(name, value_type, obj_type, obj_name) LATTR_SETTER5(name, value_type, obj_type, obj_name, __LINE__)
177 #define LATTR_VALID(name, obj_type, obj_name) LATTR_VALID5(name, obj_type, obj_name, __LINE__)
A variable-expanding proxy for the config class.
Definitions for the interface to Wesnoth Markup Language (WML).
std::decay_t< T > lua_check(lua_State *L, int n)
void register_lua_attribute(const char *name)
void lua_push(lua_State *L, const T &val)
Holds a lookup table for members of one type of object.
int dir(lua_State *L)
Implement __dir metamethod.
getters_list getters
A map of callbacks that read data from the object.
setters_list setters
A map of callbacks that write data to the object.
std::map< std::string, std::function< bool(lua_State *L)> > validators_list
validators_list validators
A map of callbacks that check if a member is available.
static std::map< std::string_view, std::reference_wrapper< luaW_Registry > > lookup
std::string private_metatable
The internal metatable string for the object (from __metatable)
std::map< std::string, std::function< bool(lua_State *L, int idx, bool nop)> > setters_list
std::vector< std::string > public_metatable
Optional external metatable for the object (eg "wesnoth", "units") All entries of this table will be ...
int set(lua_State *L)
Implement __newindex metamethod.
int get(lua_State *L)
Implement __index metamethod.
std::map< std::string, std::function< bool(lua_State *L, bool nop)> > getters_list
virtual value_type get(lua_State *L, const object_type &obj) const =0
virtual ~lua_getter()=default
virtual void set(lua_State *L, object_type &obj, const value_type &value) const =0
virtual ~lua_setter()=default
virtual bool is_active(lua_State *L, const object_type &obj) const =0
virtual ~lua_validator()=default
static map_location::direction n