Skip to content

Commit ea6b368

Browse files
committed
Merge branch 'refs/heads/pr/34'
# Conflicts: # include/kaguya/metatable.hpp # test/test_02_classreg.cpp
2 parents d10e7ba + 0e4362a commit ea6b368

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

include/kaguya/metatable.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,22 @@ namespace kaguya
316316
return *this;
317317
}
318318

319+
/// @brief add member property with setter, getter functions.(experimental)
320+
/// @param name function name for lua
321+
/// @param getter getter function
322+
/// @param setter setter function
323+
template<typename GetType>
324+
UserdataMetatable& addProperty(const char* name, GetType(*getter)(const class_type*))
325+
{
326+
if (has_key(name))
327+
{
328+
throw KaguyaException("already registered.");
329+
return *this;
330+
}
331+
property_map_[name] = AnyDataPusher(function(getter));
332+
return *this;
333+
}
334+
319335
/// @brief add member property with setter, getter functions.(experimental)
320336
/// @param name function name for lua
321337
/// @param getter getter function
@@ -332,6 +348,23 @@ namespace kaguya
332348
return *this;
333349
}
334350

351+
352+
/// @brief add member property with external setter, getter functions.(experimental)
353+
/// @param name function name for lua
354+
/// @param getter getter function
355+
/// @param setter setter function
356+
template<typename GetType, typename SetType>
357+
UserdataMetatable& addProperty(const char* name, GetType(*getter)(const class_type*), void (*setter)(class_type*, SetType))
358+
{
359+
if (has_key(name))
360+
{
361+
throw KaguyaException("already registered.");
362+
return *this;
363+
}
364+
property_map_[name] = AnyDataPusher(overload(getter, setter));
365+
return *this;
366+
}
367+
335368
/// @brief add non member function
336369
/// @param name function name for lua
337370
/// @param f function

test/test_02_classreg.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,5 +1607,26 @@ KAGUYA_TEST_FUNCTION_DEF(arg_type_mismatch_error)(kaguya::State& state)
16071607

16081608
TEST_CHECK(last_error_message.find("mismatch") != std::string::npos);
16091609
};
1610+
int Base_a_getter(const Base* self)
1611+
{
1612+
return self->a;
1613+
}
1614+
void Base_a_setter(Base* self,int v)
1615+
{
1616+
self->a = v;
1617+
}
1618+
KAGUYA_TEST_FUNCTION_DEF(add_property_external)(kaguya::State& state)
1619+
{
1620+
1621+
state["Base"].setClass(kaguya::UserdataMetatable<Base>()
1622+
.setConstructors<Base()>()
1623+
.addProperty("a", &Base_a_getter,&Base_a_setter)
1624+
);
1625+
Base base;
1626+
state["base"] = &base;
1627+
TEST_CHECK(state("base.a=1"));
1628+
TEST_CHECK(state("assert(1 == base.a)"));
1629+
TEST_EQUAL(base.a, 1);
1630+
}
16101631

16111632
KAGUYA_TEST_GROUP_END(test_02_classreg)

0 commit comments

Comments
 (0)