-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add property setter function that does not depend on getter #271
Conversation
Sorry, I'm not familiar with the library you mentioned. |
Here's the documentation for refl-cpp. A very brief reflection example might be something like: class MyType
{
int value;
public:
MyType : value(0) {}
int GetValue() const { return value; }
void SetValue(int newval) { value = newval; }
int SomeOtherFunction(std::string &text);
}; I have written a (partially) compile-time #include "refl.hpp"
template<class T, typename TN>
void AddClassMembers(TN& bridgeClass)
{
constexpr auto type = refl::reflect<T>();
constexpr auto members = get_members(type);
for_each(members, [&](auto member)
{
bridgeClass.addFunction(refl::descriptor::get_name(member).c_str(), refl::descriptor::get_pointer(member));
if constexpr (refl::descriptor::is_property(member))
{
if constexpr (refl::descriptor::is_writable(member))
{
bridgeClass.addPropertySetter(refl::descriptor::get_display_name(member), refl::descriptor::get_pointer(member));
}
else
{
bridgeClass.addProperty(refl::descriptor::get_display_name(member), refl::descriptor::get_pointer(member));
}
}
});
}
template<class T>
void AddClass(const std::string& nameSpace, lua_State *l)
{
constexpr auto type = refl::reflect<T>();
auto bridgeClass = luabridge::getGlobalNamespace(l)
.beginNamespace (nameSpace.c_str())
.beginClass<T>(refl::descriptor::get_name(type).c_str());
AddClassMembers<T>(bridgeClass);
bridgeClass.endClass()
.endNamespace();
}
.
.
.
// and here I actually add the class to LuaBridge
// first, the REFL_AUTO macro that defines the reflected members
REFL_AUTO (
type(MyType),
func(GetValue, property()),
func(SetValue, property()),
func(SomeOtherFunction)
)
// then add the class
AddClass<MyClass>(namespace_str, l);
Originally I created a second list of methods to search for property setters. It required two nested levels deep of lambda functions. Although it worked in XCode, it was cumbersome and difficult to read. But it failed entirely in Visual Studio because (due possibly to a bug), Visual Studio will not pass lambda captures 2 nested levels deep in a Adding a separate |
Also, I should add that in my code the property setters and getters come higgledy-piggledy in any order, but ultimately they all get added correctly. |
Also, it seems to me that one would either use addProperty(name, getter, setter); or addPropertySetter(name, setter);
addProperty(name, getter);
// in either order, which currently works with my pr But not both. Does LuaBridge now permit adding properties twice? |
I have actually encountered write-only properties in the wild. I'm not saying it's something I would ever implement, but I have seen them. A recent example where I might conceivably have implemented one is on a list-control class I was working on. There is an option on To be clear, I am not proposing this pull request to have write-only properties. That's just a harmless side-effect. Answering your second question, REFL_AUTO (
type(MyType),
func(GetValue, property()),
func(SetValue, property()),
func(SomeOtherFunction)
) Note that every member is func. I have several hundred classes, each with anywhere from a dozen to a couple of hundred functions being bound thru LuaBridge with lists like these. The order they are listed in is pretty much random. I need to be able to enter the property setters when I encounter them and the property getters when I encounter them, without having to know both at the same time. That's the reason for the pr. To further clarify, when the member is a function, |
So I took a look at the if constexpr (refl::descriptor::is_writable(member))
{
bridgeClass.addProperty(
refl::descriptor::get_display_name(member),
refl::descriptor::get_pointer(refl::descriptor::get_reader(member)),
refl::descriptor::get_pointer(member));
}
else You only should carefully handle the |
*mind-blown emoji* Actually, what I need is Thank you for digging that out. |
I ended up using The puny POS 32-bit compiler in Visual Studio 2019 just can't eat my largest I may still use my fork with |
This pull request implements a separate
addPropertySetter
function forClass
. If you use a reflection library likerefl-cpp
to load your classes intoLuaBridge
, it is inefficient or even impossible (due to Visual Studio 2019 limitations) to know the property getter and setter at the same time. This simple new function resolves the issue.It would have been possible to overload
addProperty
, but that had the disadvantage of making it far too easy to add a setter instead of a getter by mistake (or vice versa). So I opted to give it a different function name.Resolves #265