CPPR

I developed a C++ runtime reflection system that enables automatic serialization of structs to JSON format. This system provides a flexible and efficient way to convert C++ objects into JSON representations without manual coding for each struct.

My Code

Reflection Macros for Struct Descriptor

This feature enables reflection for structs by defining macros that help with automatic member registration and struct description initialization.

Reflection Macros Code


      #define REFLECT() \
        static inline void initReflection(reflect::StructDescriptor*); \
        static inline reflect::StructDescriptor structDesc{initReflection};
      
      #define REFLECT_STRUCT_BEGIN(type) \
        inline void type::initReflection(reflect::StructDescriptor* typeDesc) { \
          using T = type; \
          typeDesc->name = #type; \
          typeDesc->size = sizeof(T);
      
      #define REFLECT_STRUCT_MEMBER(name) \
          typeDesc->AddMember(#name, sizeof(T::name), offsetof(T, name)); \
      
      #define REFLECT_STRUCT_END() \
        }
          

These macros help implement reflection in C++ by generating code that registers the members of a struct, associating them with their names, types, and memory offsets.

TypeDescriptor Struct

This struct holds information about a member of a structure, including its memory offset, type, and size. It's essential for dynamic access during reflection or serialization.

TypeDescriptor Code


      struct TypeDescriptor
      {
          size_t offset;
          std::unique_ptr type;
      
          TypeDescriptor(const char *memberName, size_t size, size_t memberOffset);
      };
          

This struct contains the memory offset, size, and type of a member, helping locate and reflect on that member dynamically during runtime.

StructDescriptor Struct

This struct manages metadata for an entire structure. It holds the name, size, and members of the structure, which is useful for reflection and serialization.

StructDescriptor Code


      struct StructDescriptor
      {
          const char *name;
          size_t size;
          std::vector members;
      
          StructDescriptor(const char* name, size_t size);
          StructDescriptor(void (*init)(StructDescriptor*));
      
          template
          void AddMember(const char *memberName, size_t size, size_t memberOffset)
          {
              TypeDescriptor member{memberName, size, memberOffset};
              member.type = create_type_info(memberName);
              if (member.type->kind == UNKNOWN) {
                  throw std::runtime_error("Unknown Type detected, member name: " + std::string(memberName));
                  return;
              }
              members.push_back(std::move(member));
          }
      
          void *GetMemberMemAdress(void *reference, size_t offset);
          std::string To_Json(const char* filePath, void* structRef);
          void From_Json(const char* filePath, void* structRef);
      };
          

This struct records a structure's metadata, such as its name, size, and members, allowing dynamic access to its members for serialization or reflection.

Suggested Projects

Crow Engine

A shader engine inspired by Shadertoy, built to create and explore graphical effects.

C++ Icon Clion Icon Vulkan Icon
Project Image

Breakpoint Injector

A Unity tool to inject breakpoint templates into the Rider IDE.

C# Icon Visual Studio Icon Unity Icon Rider Icon
Project Image