Skip to content

YanSchw/ReflectCPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReflectCPP

A small and simple C++ Reflection Library

Main Features

  • Allocate any type of Class by its ID handle (using std::new), if a default constructor is available
  • Access primitive ClassMembers without knowing the ClassType during compile-time
  • Access RunTimeTypeInformation (RTTI) using little to none Boilerplate code

Examples

Allocate any type of Class by its ID handle, if a default constructor is available

#include "ReflectCPP.h"
using namespace rfl;

struct MyClass
{
    RFL_CLASS(MyClass);
    MyClass() = default;
    // ...
};

int main()
{
    Class cls = MyClass::StaticClass();

    // Create an instance anytime during runtime
    void* inst = cls.NewInstance();
}

Access primitive ClassMembers without knowing the ClassType during compile-time

#include "ReflectCPP.h"
using namespace rfl;

struct MyClass
{
    RFL_CLASS(MyClass,
    {
        RFL_FIELD(MyClass, int, myValue);
    });

    MyClass(int val) : myValue(val)  { }
    
    int myValue;
};

int main()
{
    MyClass inst = MyClass(13);
    auto fields = MyClass::StaticClass().GetClassReflector().GetFields();
    for (auto& It : fields)
    {
        std::cout << It.m_Name << " " << (uint32_t)It.m_Type << " " << It.m_Offset << std::endl;

        // Increment ClassMember variable indirectly...
        int& ref = It.GetFieldRefFromObject<int>(&inst);
        ref++;
    }
}

Requirements

  • C++17 Compiler
  • Include the ReflectCPP.h anywhere you want to use it
  • Compile and link the ReflectCPP.cpp Translation Unit
  • No external Libraries needed!
  • Everything is pure C++