Gamasutra has a very detailed article about the feature sored feature: behind the mirror-adding reflection to C ++.
- Rtti
-
- After the class declaration, the practice of adding rtti macros almost quickly becomes a standard for various engines. The rtti of C ++ is too weak.
- Previously, I only thought that the reason for implementing rtti was that dynamic_cast was not efficient and required by the object factory. I turned off this compilation option and reduced the memory used.
- Field (property)
-
- The implementation reflection must have some attribute member description classes. Currently, three implementation methods are available for this information.
-
- It is not recommended to use tools/compiler intermediate information to parse and generate Based on C ++ code (Havok may seem like this implementation). See the original article.
- Using various templates and macros for registration like rtti makes it easy to query. The disadvantage is that it increases the startup time and memory usage and is difficult to debug in one step.
- The visitor mode is used to create information only when you need to query information. The encoding is flexible, but the access speed is slow.
- Generally, the offset and size of the member variable relative to the class/struct are recorded, and the memory address is calculated based on the instance pointer.
- Method
-
- At present, we have not seen the engine that uses reflection for the method. It is implemented in cppreflect. The more concise the code, the better. The complicated code can be replaced by scripts.
- Clone
-
- Another advantage of reflection is that objects can be copied in depth, which is also the basic requirement of prefab/template.
- Serialization
-
- With reflection, serialization is quite convenient. If not, it works like gamebryo.
- After name (or CRC) and value are serialized together, version compatibility can be achieved. This is useful when member attributes are frequently modified.
- The binary serialization of repetitive data can be compressed, such as arrays.
- For XML serialization of memory buffer, base64 can be used to encode it into a string
- Inheritance
-
- Data inheritance, or data copying, means that the data between the object instances has an inheritance relationship and only stores data different from the parent instance (Template ).
- Others
-
- In the past, when implementing delegate, I only touched the member function pointer. I didn't expect a member variable pointer. c ++ is really profound and profound.
- Enum and container need special treatment
- To reflect to. Net propertygrid, you can use icustomtypedescriptor to construct dynamic attribute objects, or use reflection. emit to dynamically compile and generate types.