#pragma once #include "object_type_info.h" class Object { // OBJECT TYPE STUFF private: static ObjectTypeInfo s_type_info; public: typedef Object ThisClass; static const ObjectTypeInfo* StaticTypeInfo() { return &s_type_info; } static ObjectTypeInfo* StaticMutableTypeInfo() { return &s_type_info; } static const PROPERTY_DECLARATION* StaticPropertyMap() { return nullptr; } static ObjectFactory* StaticFactory() { return nullptr; } // END OBJECT TYPE STUFF public: Object(const ObjectTypeInfo* type_info = &s_type_info); virtual ~Object(); // Retrieves the type information for this object. const ObjectTypeInfo* GetTypeInfo() const { return m_type_info; } // Cast from one object type to another, unchecked. template const T* Cast() const { DebugAssert(m_type_info->IsDerived(T::StaticTypeInfo())); return static_cast(this); } template T* Cast() { DebugAssert(m_type_info->IsDerived(T::StaticTypeInfo())); return static_cast(this); } // Cast from one object type to another, checked. template const T* SafeCast() const { return (m_type_info->IsDerived(T::StaticTypeInfo())) ? static_cast(this) : nullptr; } template T* SafeCast() { return (m_type_info->IsDerived(T::StaticTypeInfo())) ? static_cast(this) : nullptr; } // Test if one object type is derived from another. template bool IsDerived() const { return (m_type_info->IsDerived(T::StaticTypeInfo())); } bool IsDerived(const ObjectTypeInfo* type) const { return (m_type_info->IsDerived(type)); } protected: // Type info pointer. Set by subclasses. const ObjectTypeInfo* m_type_info; }; // // GenericObjectFactory // template struct GenericObjectFactory final : public ObjectFactory { Object* CreateObject() override { return new T(); } Object* CreateObject(const String& identifier) override { return new T(); } void DeleteObject(Object* object) override { delete object; } }; #define DECLARE_OBJECT_GENERIC_FACTORY(Type) \ \ private: \ static GenericObjectFactory s_GenericFactory; \ \ public: \ static ObjectFactory* StaticFactory() { return &s_GenericFactory; } #define DEFINE_OBJECT_GENERIC_FACTORY(Type) \ GenericObjectFactory Type::s_GenericFactory = GenericObjectFactory();