Unigine::Reflection Class
Header: | #include <UnigineInterpreter.h> |
This class is used to create reflections for user or extern classes and namespaces. It allows you to get names and custom attribute strings for all variables, arrays, user classes and namespaces within it. Such attributes can be used for smart automatic code generation for GUI or any game logic. For example, it is possible to get attributes, parse them in the required way, and feed them to the Expression which will compile the resulting code.
Usage Example
The sample below is showing how to create an XML serialization system for any instance classes by simple adding of special attributes to getter and setter functions.
#include <unigine.h>
/*
* Implementation of a MyClass, the user class with special functions for XML serialization
*/
class MyClass {
private:
int int_value;
vec3 vec3_value;
public:
[serialize:int_value]
int getIntValue() {
return int_value;
}
[serialize:int_value]
void setIntValue(int value) {
int_value = value;
}
[serialize:vec3_value]
vec3 getVec3Value() {
return vec3_value;
}
[serialize:vec3_value]
void setVec3Value(vec3 value) {
vec3_value = value;
}
};
/*
* Serialize XML for any class instance
*/
Xml serialize(int instance) {
// specify only simple types of data to be serialized
string function_types[] = (
"int" : "int",
"float" : "float",
"double" : "double",
"vec3" : "vec3",
"Vec3" : "Vec3",
"vec4" : "vec4",
"Vec4" : "Vec4",
"string" : "string",
);
// create a reflection class
Reflection reflection = new Reflection();
// return NULL value if the instance cannot be reflected
if(reflection.reflect(instance) == false) return NULL;
// create an XML class instance
Xml xml = new Xml();
xml.setName("inctance");
// write the instance class name as an attribute
xml.setArg("type",reflection.getName());
// simple check of an attribute function for serialization
int is_serialize(string str,string &name) {
// the attribute string must be like [serialize:%parameter_name]
string ret[0];
strsplit(str,":",ret);
if(ret.size() < 2) return false;
if(trim(ret[0]) != "serialize") return false;
name = ret[1];
return true;
}
// a loop for all class functions
forloop(int i = 0; reflection.getNumFunctions()) {
string name = "";
// check function for a "serialize" attribute
if(is_serialize(reflection.getFunctionAttribute(i),name) == false) continue;
// specify get and check function types
string type = function_types.check(reflection.getFunctionType(i),"");
if(type == "") continue;
// specify that the parameter "get" function must have zero arguments
if(reflection.getNumFunctionArgs(i) != 0) continue;
// add parameter child to XML
Xml parameter = xml.addChild("parameter");
parameter.setArg("name",name);
parameter.setArg("type",type);
// call the "get" function instance to get a parameter value
parameter.setData(string(instance.call(reflection.getFunctionID(i))));
}
// return the XML with instance parameters data
return xml;
}
int init() {
// create MyClass instance
MyClass instance = new MyClass();
// set the parameters to the class instance
instance.setIntValue(33);
instance.setVec3Value(vec3(1.2f,2.3f,3.1f));
// create the XML
Xml xml = serialize(instance);
// write XML to the file
if(xml != NULL) {
xml.save("my_class.xml");
}
return 1;
}
As a result we will get the XML file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<instance type="MyClass">
<parameter name="int_value" type="int">33</parameter>
<parameter name="vec3_value" type="vec3">1.2 2.3 3.1</parameter>
</instance>
Reflection Class
Members
Reflection(void * interpreter, const Variable & v)
Arguments
- void * interpreter
- const Variable & v
int getNumFunctionDefaultArgs(int num)
Arguments
- int num
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
Variable callExternClassFunction(Variable object, const char * function, int num_args, const Variable * args)
Arguments
- Variable object
- const char * function
- int num_args
- const Variable * args
Variable callExternClassFunction(Variable object, int function)
Arguments
- Variable object
- int function
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0)
Arguments
- Variable object
- const char * function
- const Variable & arg0
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
Variable callExternClassFunction(Variable object, int function, int num_args, const Vector<Variable> & args)
Arguments
- Variable object
- int function
- int num_args
- const Vector<Variable> & args
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
Variable callExternClassFunction(Variable object, const char * function)
Arguments
- Variable object
- const char * function
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
Variable callExternClassFunction(Variable object, int function, const Variable & arg0)
Arguments
- Variable object
- int function
- const Variable & arg0
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
Variable callExternClassFunction(Variable object, const char * function, int num_args, const Vector<Variable> & args)
Arguments
- Variable object
- const char * function
- int num_args
- const Vector<Variable> & args
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
Variable callExternClassFunction(Variable object, const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7, const Variable & arg8)
Arguments
- Variable object
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
- const Variable & arg8
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7, const Variable & arg8)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
- const Variable & arg8
Variable callExternClassFunction(Variable object, int function, int num_args, const Variable * args)
Arguments
- Variable object
- int function
- int num_args
- const Variable * args
Variable callExternClassFunction(Variable object, int function, const Variable & arg0, const Variable & arg1)
Arguments
- Variable object
- int function
- const Variable & arg0
- const Variable & arg1
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
Variable callExternFunction(const char * function, int num_args, const Vector<Variable> & args)
Arguments
- const char * function
- int num_args
- const Vector<Variable> & args
Variable callExternFunction(int function, int num_args, const Variable * args)
Arguments
- int function
- int num_args
- const Variable * args
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
Variable callExternFunction(const char * function)
Arguments
- const char * function
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7, const Variable & arg8)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
- const Variable & arg8
Variable callExternFunction(const char * function, const Variable & arg0)
Arguments
- const char * function
- const Variable & arg0
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
Variable callExternFunction(int function, const Variable & arg0)
Arguments
- int function
- const Variable & arg0
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7, const Variable & arg8)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
- const Variable & arg8
Variable callExternFunction(int function, int num_args, const Vector<Variable> & args)
Arguments
- int function
- int num_args
- const Vector<Variable> & args
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
Variable callExternFunction(const char * function, int num_args, const Variable * args)
Arguments
- const char * function
- int num_args
- const Variable * args
Variable callExternFunction(int function)
Arguments
- int function
Variable callExternFunction(const char * function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6, const Variable & arg7)
Arguments
- const char * function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
- const Variable & arg7
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1, const Variable & arg2, const Variable & arg3, const Variable & arg4, const Variable & arg5, const Variable & arg6)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
- const Variable & arg2
- const Variable & arg3
- const Variable & arg4
- const Variable & arg5
- const Variable & arg6
Variable callExternFunction(int function, const Variable & arg0, const Variable & arg1)
Arguments
- int function
- const Variable & arg0
- const Variable & arg1
int reflect(void * interpreter, const Variable & v)
Arguments
- void * interpreter
- const Variable & v
Last update: 03.07.2017
Помогите сделать статью лучше
Была ли эта статья полезной?
(или выберите слово/фразу и нажмите Ctrl+Enter