Properties

A property is a mix between a function and a variable. Properties are similar to C#'s getter/setter system. To Lua, a property looks like a normal variable, but every time that variable is read, a function is called. Similarly, every time the variable is changed, a function is called. These are getter/setter functions. If a property has a getter function but no setter, it is effectively read-only.

A property can be added with the addProperty (char const*, TG (T::*) () const, void (T::*) function. This function takes three arguments. The first one is the Lua name for the variable, and the second and third arguments are getter and setter functions, respectively. The addProperty function has an overloaded version that only takes a getter function. The following code demonstrates how to use properties:

int bar;
int get_bar () const {
// Potentially do some error checking?
return bar;
}

void set_bar (int b) {
bar = b;
}
getGlobalNamespace(L)
.beginNamespace("foo")
.addProperty("bar", get_bar, set_bar)
.addProperty("bar_readonly", get_bar)
.endNamespace()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.119.81