Created
May 27, 2024 10:34
-
-
Save aliakseis/93ff3b9c41ed82e20992606dee4a7ae1 to your computer and use it in GitHub Desktop.
This could be a part of a meta-programming framework in C++ that uses macros and templates to generate metadata about classes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define BEGIN_META_MAP(classname) \ | |
| template<int id, int> friend struct CDescriptor; \ | |
| private: \ | |
| enum { ID_REFERENCE_POINT = __COUNTER__ }; \ | |
| typedef classname TheClass; \ | |
| template<int id, int=id> struct CDescriptor \ | |
| { \ | |
| }; \ | |
| template<int id> struct CDescriptor<id, 0> \ | |
| { \ | |
| }; | |
| #define END_META_MAP \ | |
| private: \ | |
| typedef CDescriptor<__COUNTER__ - ID_REFERENCE_POINT - 1> CFinalDescr; | |
| #define META_PROP_INTERNAL(name) \ | |
| private: \ | |
| template<int id> struct CDescriptor<id, __COUNTER__-ID_REFERENCE_POINT> \ | |
| { \ | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
• BEGIN_META_MAP(classname): This macro starts the definition of metadata for a class named classname. It does several things:
• Declares a template structure CDescriptor as a friend, allowing it to access private members of the class.
• Defines an enumeration ID_REFERENCE_POINT which is set to the value of COUNTER, a preprocessor variable that increments each time it's used (presumably a custom or compiler-specific feature).
• Defines a type alias TheClass to refer to the classname.
• Declares two template specializations of CDescriptor. The first is a general template, and the second is a specialization for when the second template parameter is zero.
• END_META_MAP: This macro defines a type alias CFinalDescr that refers to a CDescriptor with a specific ID. This ID is calculated based on the difference between the current value of COUNTER and ID_REFERENCE_POINT, minus one. This likely marks the end of the metadata definitions for the class.
• META_PROP_INTERNAL(name): This macro is used to define metadata for a property within the class. It declares a specialized version of CDescriptor with an ID that is the difference between the current COUNTER value and ID_REFERENCE_POINT.
• The id is a compile-time constant expression that uniquely identifies each instance of CDescriptor. It's used to create a sequence of descriptors, each corresponding to a different class member. Please see https://stackoverflow.com/questions/1162401/is-it-possible-to-access-values-of-non-type-template-parameters-in-specialized-t/52699027#52699027
Here's how the code might be used in practice:
class MyClass {
BEGIN_META_MAP(MyClass)
// Metadata definitions for MyClass properties would go here.
END_META_MAP
};
// Later, you could use the metadata to do things like serialization, reflection, etc.
// Note that metadata definitions do not have to strictly follow each other and may be interspersed with other declarations, statements, etc.