External Reflection Tutorial
In this quick tutorial we show a simple example of opC++ external reflection.
By external reflection, we mean learning about an object (names, types, modifiers, members, etc.) without having an actual instance of the object in question.
In opC++, you can use external reflection on opclasses and opstructs. An opclass is a category introduced by the opC++ Standard Dialect. It is a step above the C++ class construct, and automatically adds serialization and reflection features. To begin, let's first define a simple opclass that we can perform reflection on:
In this simple opclass, called Person, we define several public data members. Two of the data members have modifiers introduced in the opC++ Standard Dialect: native and transient. The native modifier specifies that the data member it accompanies is not reflectable (and not serializable). The transient modifier means that the reflected data member is marked as not serializable by meta-data.
Suppose that we do not have an instance of the Person opclass, but that we want to know about what data members, modifiers, etc. the opclass has. We can do this using opC++ external reflection:
First, we want to see if an opclass called Person exists. We can ask the opC++ class_tracker to locate an opclass called Person. If none is found, it returns NULL. Assuming we make it this far, we can then determine how many fields the opclass has, as well as other information.
In the example above we loop through the opclass' fields and print their names to the standard out. If a data member is transient, we print out that information also.
The debug output on the console is shown here:
Sure enough, we find the Person opclass and print out the names of all it's data members. Notice that the SocialSecurityNum variable from the Person opclass is not printed - this is because it is declared native, and is not reflected.
This simple example is only the tip of the iceberg. You can query loads of metadata externally without a single instance of the opclass or opstruct in question with opC++ reflection!
Project Files (VS 2005): ExternalReflection.zip