| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #pragma once
- #include "CmPrerequisitesUtil.h"
- #include "CmRTTIField.h"
- #include "CmIReflectable.h"
- namespace CamelotEngine
- {
- struct RTTIReflectablePtrFieldBase : public RTTIField
- {
- virtual std::shared_ptr<IReflectable> getValue(void* object) = 0;
- virtual std::shared_ptr<IReflectable> getArrayValue(void* object, UINT32 index) = 0;
- virtual void setValue(void* object, std::shared_ptr<IReflectable> value) = 0;
- virtual void setArrayValue(void* object, UINT32 index, std::shared_ptr<IReflectable> value) = 0;
- virtual std::shared_ptr<IReflectable> newObject() = 0;
- virtual bool hasDynamicSize() { return true; }
- };
- template <class DataType, class ObjectType>
- struct RTTIReflectablePtrField : public RTTIReflectablePtrFieldBase
- {
- /**
- * @brief Initializes a field pointing to a class implementing a IReflectable interface.
- *
- * @param name Name of the field.
- * @param uniqueId Unique identifier for this field. Although name is also a unique
- * identifier we want a small data type that can be used for efficiently
- * serializing data to disk and similar. It is primarily used for compatibility
- * between different versions of serialized data.
- * @param getter The getter method for the field. Cannot be null. Must be a specific signature: DataType*(ObjectType*)
- * @param setter The setter method for the field. Can be null. Must be a specific signature: void(ObjectType*, DataType*)
- */
- void initSingle(const std::string& name, UINT16 uniqueId, boost::any getter, boost::any setter)
- {
- initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_ReflectablePtr);
- }
- /**
- * @brief Initializes a VECTOR field with entries pointing to a class implementing IReflectable interface.
- *
- * @param name Name of the field.
- * @param uniqueId Unique identifier for this field. Although name is also a unique
- * identifier we want a small data type that can be used for efficiently
- * serializing data to disk and similar. It is primarily used for compatibility
- * between different versions of serialized data.
- * @param getter The getter method for the field. Cannot be null. Must be a specific signature: DataType*(ObjectType*, UINT32)
- * @param getSize Getter method that returns the size of an array. Cannot be null. Must be a specific signature: UINT32(ObjectType*)
- * @param setter The setter method for the field. Can be null. Must be a specific signature: void(ObjectType*, UINT32, DataType*)
- * @param setSize Setter method that allows you to resize an array. Can be null. Can be null. Must be a specific signature: void(ObjectType*, UINT32)
- */
- void initArray(const std::string& name, UINT16 uniqueId, boost::any getter,
- boost::any getSize, boost::any setter, boost::any setSize)
- {
- initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_ReflectablePtr);
- }
- virtual UINT32 getTypeSize()
- {
- return 0; // Complex types don't store size the conventional way
- }
- virtual std::shared_ptr<IReflectable> getValue(void* object)
- {
- checkIsArray(false);
- ObjectType* castObjType = static_cast<ObjectType*>(object);
- boost::function<std::shared_ptr<DataType>(ObjectType*)> f = boost::any_cast<boost::function<std::shared_ptr<DataType>(ObjectType*)>>(valueGetter);
- std::shared_ptr<IReflectable> castDataType = f(castObjType);
- return castDataType;
- }
- virtual std::shared_ptr<IReflectable> getArrayValue(void* object, UINT32 index)
- {
- checkIsArray(true);
- ObjectType* castObjType = static_cast<ObjectType*>(object);
- boost::function<std::shared_ptr<DataType>(ObjectType*, UINT32)> f = boost::any_cast<boost::function<std::shared_ptr<DataType>(ObjectType*, UINT32)>>(valueGetter);
- std::shared_ptr<IReflectable> castDataType = f(castObjType, index);
- return castDataType;
- }
- virtual void setValue(void* object, std::shared_ptr<IReflectable> value)
- {
- checkIsArray(false);
- if(valueSetter.empty())
- {
- CM_EXCEPT(InternalErrorException,
- "Specified field (" + mName + ") has no setter.");
- }
- ObjectType* castObjType = static_cast<ObjectType*>(object);
- std::shared_ptr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
- boost::function<void(ObjectType*, std::shared_ptr<DataType>)> f = boost::any_cast<boost::function<void(ObjectType*, std::shared_ptr<DataType>)>>(valueSetter);
- f(castObjType, castDataObj);
- }
- virtual void setArrayValue(void* object, UINT32 index, std::shared_ptr<IReflectable> value)
- {
- checkIsArray(true);
- if(valueSetter.empty())
- {
- CM_EXCEPT(InternalErrorException,
- "Specified field (" + mName + ") has no setter.");
- }
- ObjectType* castObjType = static_cast<ObjectType*>(object);
- std::shared_ptr<DataType> castDataObj = std::static_pointer_cast<DataType>(value);
- boost::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)> f = boost::any_cast<boost::function<void(ObjectType*, UINT32, std::shared_ptr<DataType>)>>(valueSetter);
- f(castObjType, index, castDataObj);
- }
- virtual UINT32 getArraySize(void* object)
- {
- checkIsArray(true);
- boost::function<UINT32(ObjectType*)> f = boost::any_cast<boost::function<UINT32(ObjectType*)>>(arraySizeGetter);
- ObjectType* castObject = static_cast<ObjectType*>(object);
- return f(castObject);
- }
- virtual void setArraySize(void* object, UINT32 size)
- {
- checkIsArray(true);
- if(arraySizeSetter.empty())
- {
- CM_EXCEPT(InternalErrorException,
- "Specified field (" + mName + ") has no array size setter.");
- }
- boost::function<void(ObjectType*, UINT32)> f = boost::any_cast<boost::function<void(ObjectType*, UINT32)>>(arraySizeSetter);
- ObjectType* castObject = static_cast<ObjectType*>(object);
- f(castObject, size);
- }
- virtual std::shared_ptr<IReflectable> newObject()
- {
- return std::shared_ptr<IReflectable>(DataType::getRTTIStatic()->newRTTIObject());
- }
- };
- }
|