The cproperty Type
This type is declared within the struct module, and is used to trigger functions whenever an attribute has been assigned. It is similar to using the @property and @<function>.setter methods in python. For example, when the name of variable is assigned some variable, the set_name function is automatically called and modifies the event_t struct attributes.
The cproperty class can be imported via the following header
#include <structs/property.h>
-
class cproperty
-
cproperty()
The constructor of the cproperty.
-
void set_setter(std::function<void(T*, G*)> c)
Specifies the setter function.
-
void set_getter(std::function<void(T*, G*)> c)
Specifies the getter function.
-
void set_object(G *obj)
Sets the object that the property should interact with.
-
T operator+(const T &val)
Overloaded operator for addition.
-
bool operator==(const T &val)
Overloaded operator method for asserting if two values are the same.
-
bool operator!=(const T &val)
Overloaded operator method for asserting that two values are not the same.
-
operator T()
Constructor of the templated data type contained within the cproperty.
-
void clear()
Purges the current values of the setter and getter
-
T *operator&()
Overloaded operator method for getting the pointer address of the variable.
-
cproperty()
Example Usage
class some_object {
public:
some_object(){
this -> secret.set_setter(this -> set_secret);
this -> secret.set_getter(this -> get_secret);
this -> secret.set_object(this);
}
~some_object(){}
// instance initialization
cproperty<std::string, some_object*> secret;
private:
void static get_secret(std::string* val, some_object* obj){
*val = obj -> secret_value;
}
void static set_secret(std::string* val, some_object* obj){
obj -> secret_value = (*val);
}
std::string secret_value = "";
};
some_object* ob = new some_object();
ob -> secret = "super-cool"; // calls the set_secret function
std::string sec = obj -> secret; // calls the get_secret function