Type System
Feature: Type System
Section titled “Feature: Type System”The Entropy Type System provides compile-time reflection, unique type identification (TypeID), and field introspection for C++ classes.
Overview
Section titled “Overview”The system uses macros to generate metadata at compile-time, allowing for efficient:
- Serialization: Automatically traversing fields for saving/loading.
- Scripting: Exposing C++ types to scripting languages (e.g., Lua/Python).
- Scripting: Binding C++ objects to scripting languages.
1. Registration
Section titled “1. Registration”To make a class reflectable, use ENTROPY_REGISTER_TYPE and ENTROPY_FIELD within the class definition.
#include <EntropyCore/TypeSystem/Reflection.h>
class UserProfile {public: // 1. Register the type ENTROPY_REGISTER_TYPE(UserProfile);
// 2. Register fields ENTROPY_FIELD(float, creditBalance); ENTROPY_FIELD(std::string, username); ENTROPY_FIELD(glm::vec3, lastLocation);
// Private fields can also be registeredprivate: ENTROPY_FIELD(bool, isAdmin);};2. Introspection
Section titled “2. Introspection”Query type information at runtime using TypeInfo.
// Get metadataconst TypeInfo* info = TypeInfo::get<UserProfile>();
if (info) { Log::info("Inspecting type: {}", info->getName());
UserProfile profile; profile.creditBalance = 100.0f;
// Iterate fields for (const auto& field : info->getFields()) { Log::info("Field: {} (Offset: {})", field.name, field.offset);
// Type-safe value access if (field.name == "creditBalance") { auto val = TypeInfo::get_field_value<float>(&profile, field); if (val) { Log::info("Balance is {}", *val); } } }}Key Concepts
Section titled “Key Concepts”TypeID
Section titled “TypeID”A 64-bit stable hash that uniquely identifies a type.
TypeID id = createTypeId<UserProfile>();Extensions
Section titled “Extensions”You can register fields as “Extensions” (e.g., for USD schema augmentation) using ENTROPY_FIELD_EXTENSION.
ENTROPY_FIELD_EXTENSION(float, lightIntensity);