Skip to content

Commit

Permalink
Merge branch 'refs/heads/pr/34'
Browse files Browse the repository at this point in the history
# Conflicts:
#	include/kaguya/metatable.hpp
#	test/test_02_classreg.cpp
  • Loading branch information
satoren committed Aug 8, 2016
2 parents d10e7ba + 0e4362a commit ea6b368
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/kaguya/metatable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ namespace kaguya
return *this;
}

/// @brief add member property with setter, getter functions.(experimental)
/// @param name function name for lua
/// @param getter getter function
/// @param setter setter function
template<typename GetType>
UserdataMetatable& addProperty(const char* name, GetType(*getter)(const class_type*))
{
if (has_key(name))
{
throw KaguyaException("already registered.");
return *this;
}
property_map_[name] = AnyDataPusher(function(getter));
return *this;
}

/// @brief add member property with setter, getter functions.(experimental)
/// @param name function name for lua
/// @param getter getter function
Expand All @@ -332,6 +348,23 @@ namespace kaguya
return *this;
}


/// @brief add member property with external setter, getter functions.(experimental)
/// @param name function name for lua
/// @param getter getter function
/// @param setter setter function
template<typename GetType, typename SetType>
UserdataMetatable& addProperty(const char* name, GetType(*getter)(const class_type*), void (*setter)(class_type*, SetType))
{
if (has_key(name))
{
throw KaguyaException("already registered.");
return *this;
}
property_map_[name] = AnyDataPusher(overload(getter, setter));
return *this;
}

/// @brief add non member function
/// @param name function name for lua
/// @param f function
Expand Down
21 changes: 21 additions & 0 deletions test/test_02_classreg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1607,5 +1607,26 @@ KAGUYA_TEST_FUNCTION_DEF(arg_type_mismatch_error)(kaguya::State& state)

TEST_CHECK(last_error_message.find("mismatch") != std::string::npos);
};
int Base_a_getter(const Base* self)
{
return self->a;
}
void Base_a_setter(Base* self,int v)
{
self->a = v;
}
KAGUYA_TEST_FUNCTION_DEF(add_property_external)(kaguya::State& state)
{

state["Base"].setClass(kaguya::UserdataMetatable<Base>()
.setConstructors<Base()>()
.addProperty("a", &Base_a_getter,&Base_a_setter)
);
Base base;
state["base"] = &base;
TEST_CHECK(state("base.a=1"));
TEST_CHECK(state("assert(1 == base.a)"));
TEST_EQUAL(base.a, 1);
}

KAGUYA_TEST_GROUP_END(test_02_classreg)

0 comments on commit ea6b368

Please sign in to comment.