Skip to content

dou1984/gen_rfl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gen_rfl

gen_rfl is a C++ reflection code generation tool that automatically generates reflection functions for C++ classes when executed. With these reflection functions, you can quickly get or set the member variables of structures.

gen_rfl is designed for extreme performance, while also ensuring safety, ease of use, and non-intrusive integration, providing developers with a convenient and reliable reflection capability.

The reflection code generated by gen_rfl operates at O(1) time complexity.

Advantages of gen_rfl

  1. All metadata generated and initialized at compile time. Compile-time Reflection with Zero Runtime Overhead。
static meta<base> g_base_meta[] = {
    {
        .m_variant = "a",
        .m_type = "signed char",
        .m_flags = 0x80001,
        .m_field = e__base__a,
        .m_member = [](const base *c) -> const void * { 
            return std::addressof(static_cast<const base*>(c)->a);
        },
    },    
};
  1. Stores all metadata in contiguous memory with a cache-friendly memory layout.
static meta<base> g_base_meta[] = {    
    { /* a */ },
    { /* b */ },
    { /* c */ }, 
    ...
};
  1. Direct jump, no function call overhead, o(1) time complexity for field lookup.
constexpr void *__meta_label[] = { &&label_0_0, &&label_0_1, ... };
auto value = tag();
auto index = value % count;
goto *__meta_label[index];  
label_0_0:
    return ...;
  1. Runtime type checking, prevents type mismatch access
void *get_value(const base *cls, const std::string &_tag, const char *expected_type)
{    
    return (strcmp(expected_type, _meta.m_type) == 0) ? ... : nullptr;
}
  1. Support for Complex Data Structures, native support for STL containers and custom types
{
    .m_variant = "n",
    .m_type = "std::map<int, int>",
    .m_flags = 0x80001,
    .m_field = e__base__n,
    .m_member = [](const base *c) -> const void * { 
        return std::addressof(static_cast<const base*>(c)->n);
    },
}

Suitable Application Scenarios

  1. High-performance serialization/deserialization
  2. Game engine property systems
  3. Database ORM mapping
  4. Network protocol processing
  5. Configuration systems

Environment

lib version
libctemplate-dev 2.4-1
libctemplate3 2.4-1
llvm-15-dev 1:15.0.7
llvm-15 1:15.0.7

Build and Install

execute ./build.sh

About

Generate C++ structure reflection source code optimized for o1 performance.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors