📠

Reflection

CategoryC++

Goals

Usage

RTTI

Run-time type information (RTTI) is a C++ feature that allows the type of an object to be determined at run-time.

RTTI is optional with some compilers, and must be enabled.

✔️

As RTTI lacks fundamental features that are required for a reflection system, it should be disabled and implemented with a custom solution.

Structure

There are two types of data: structured and unstructured.

Structured data works with the reflection system and is described as a graph of objects, themselves described as a set of properties.

Unstructured data is represented by a block of memory that cannot be introspected such as a texture resource.

A reflection system should fully describe structured data, but only provide unstructured data as an opaque type.

Strategies

Preprocessor Macros

A reflection system based on macros is generally implemented by injecting static members into types.

The system can be implemented using templates that are declared by the macros.

✔️

Examples

Parsing and Code Generation

A more elegant approach is to modify the build system in two passes.

The first pass uses a custom utility to parse the C++ source code and generate additional files.

The second pass performs the compilation of the engine with the generated files.

There are two strategies to parse C++ code.

✔️

Examples

Data Language and Code Generation

The previous solutions require the C++ source code to be decorated and are rather intrusive.

A different approach is to design a custom data language to describe the type and their members.

With a build system in two passes, the first pass parses the data specifications and generates additional C++ source files used by the second phase of the build system.

The C++ source files are left unchanged.

✔️

Examples