| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- #pragma once
- #include "CmPrerequisitesUtil.h"
- #include "CmRTTIField.h"
- #include "CmException.h"
- namespace CamelotFramework
- {
- struct RTTIPlainFieldBase : public RTTIField
- {
- /**
- * @brief Throws an exception if the current field type and provided template types don't match.
- */
- template<class DataType>
- void checkType()
- {
- // TODO: Low priority. Because I wanted to get rid of SerializableType I have no way of checking the actual
- // type of the field and the type provided to get/set methods matches
- /*if(mType.id != SerializableType<DataType>().id)
- {
- CM_EXCEPT(InternalErrorException,
- "Invalid field type.",
- "SerializableSimpleTypeFieldBase::checkType()");
- }*/
- }
- virtual UINT32 getTypeId()
- {
- return 0;
- }
- virtual bool hasDynamicSize()
- {
- return false;
- }
- virtual UINT32 getDynamicSize(void* object)
- {
- return 0;
- }
- virtual UINT32 getArrayElemDynamicSize(void* object, int index)
- {
- return 0;
- }
- template<class ObjectType, class DataType>
- void getValue(ObjectType* object, DataType& value)
- {
- checkIsArray(false);
- checkType<DataType>();
- boost::function<DataType&(ObjectType*)> f = boost::any_cast<boost::function<DataType&(ObjectType*)>>(valueGetter);
- value = f(object);
- }
- template<class ObjectType, class DataType>
- void getArrayValue(ObjectType* object, UINT32 index, DataType& value)
- {
- checkIsArray(true);
- checkType<DataType>();
- boost::function<DataType&(ObjectType*, UINT32)> f = boost::any_cast<boost::function<DataType&(ObjectType*, UINT32)>>(valueGetter);
- value = f(object, index);
- }
- template<class ObjectType, class DataType>
- void setValue(ObjectType* object, DataType& value)
- {
- checkIsArray(false);
- checkType<DataType>();
- if(valueSetter.empty())
- {
- CM_EXCEPT(InternalErrorException,
- "Specified field (" + mName + ") has no setter.");
- }
- boost::function<void(ObjectType*, DataType&)> f = boost::any_cast<boost::function<void(ObjectType*, DataType&)>>(valueSetter);
- f(object, value);
- }
- template<class ObjectType, class DataType>
- void setArrayValue(ObjectType* object, UINT32 index, DataType& value)
- {
- checkIsArray(true);
- checkType<DataType>();
- if(valueSetter.empty())
- {
- CM_EXCEPT(InternalErrorException,
- "Specified field (" + mName + ") has no setter.");
- }
- boost::function<void(ObjectType*, UINT32, DataType&)> f = boost::any_cast<boost::function<void(ObjectType*, UINT32, DataType&)>>(valueSetter);
- f(object, index, value);
- }
- /**
- * @brief Retrieves the value from the provided field of the provided object, and copies
- * it into the buffer. WARNING - It does not check if buffer is large enough.
- *
- * @note Size of the data copied to buffer is mType.size
- */
- virtual void toBuffer(void* object, void* buffer) = 0;
- /**
- * @brief Retrieves the value at the specified array index on the provided field of the
- * provided object, and copies it into the buffer. WARNING - It does not check if buffer
- * is large enough.
- *
- * @note Size of the data copied to buffer is mType.size
- */
- virtual void arrayElemToBuffer(void* object, int index, void* buffer) = 0;
- /**
- * @brief Sets the value on the provided field of the provided object. Value is copied from the buffer.
- * WARNING - It does not check the value in the buffer in any way. You must make sure buffer points
- * to the proper location and contains the proper type.
- *
- * @note Size of the data copied from buffer is mType.size
- */
- virtual void fromBuffer(void* object, void* buffer) = 0;
- /**
- * @brief Sets the value at the specified array index on the provided field of the provided
- * object. Value is copied from the buffer. WARNING - It does not check the value in the
- * buffer in any way. You must make sure buffer points to the proper location and
- * contains the proper type.
- *
- * @note Size of the data copied from buffer is mType.size.
- */
- virtual void arrayElemFromBuffer(void* object, int index, void* buffer) = 0;
- };
- template <class DataType, class ObjectType>
- struct RTTIPlainField : public RTTIPlainFieldBase
- {
- /**
- * @brief Initializes a field with one of the built-in types. You may provide your own type ID,
- * just make sure it doesn't conflict with any standard types.
- *
- * @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)
- * @param flags Various flags you can use to specialize how systems handle this field
- */
- void initSingle(const String& name, UINT16 uniqueId, boost::any getter, boost::any setter, UINT64 flags)
- {
- int typeId = RTTIPlainType<DataType>::id; // Just making sure provided type has a type ID
- BOOST_STATIC_ASSERT_MSG((RTTIPlainType<DataType>::hasDynamicSize != 0 || (sizeof(DataType) <= 255)),
- "Trying to create a plain RTTI field with size larger than 255. In order to use larger sizes for plain types please specialize " \
- " RTTIPlainType, set hasDynamicSize to true.");
- initAll(getter, setter, nullptr, nullptr, name, uniqueId, false, SerializableFT_Plain, flags);
- }
- /**
- * @brief Initializes a VECTOR field with one of the built-in types. You may provide your own
- * type ID, just make sure it doesn't conflict with any standard types.
- *
- * @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. Must be a specific signature: void(ObjectType*, UINT32)
- * @param flags Various flags you can use to specialize how systems handle this field
- */
- void initArray(const String& name, UINT16 uniqueId, boost::any getter,
- boost::any getSize, boost::any setter, boost::any setSize, UINT64 flags)
- {
- int typeId = RTTIPlainType<DataType>::id; // Just making sure provided type has a type ID
- BOOST_STATIC_ASSERT_MSG((RTTIPlainType<DataType>::hasDynamicSize != 0 || (sizeof(DataType) <= 255)),
- "Trying to create a plain RTTI field with size larger than 255. In order to use larger sizes for plain types please specialize " \
- " RTTIPlainType, set hasDynamicSize to true.");
- initAll(getter, setter, getSize, setSize, name, uniqueId, true, SerializableFT_Plain, flags);
- }
- virtual UINT32 getTypeSize()
- {
- return sizeof(DataType);
- }
- virtual UINT32 getTypeId()
- {
- return RTTIPlainType<DataType>::id;
- }
- virtual bool hasDynamicSize()
- {
- return RTTIPlainType<DataType>::hasDynamicSize != 0;
- }
- virtual UINT32 getDynamicSize(void* object)
- {
- checkIsArray(false);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- getValue(castObject, value);
- return RTTIPlainType<DataType>::getDynamicSize(value);
- }
- virtual UINT32 getArrayElemDynamicSize(void* object, int index)
- {
- checkIsArray(true);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- getArrayValue(castObject, index, value);
- return RTTIPlainType<DataType>::getDynamicSize(value);
- }
- 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 void toBuffer(void* object, void* buffer)
- {
- checkIsArray(false);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- getValue(castObject, value);
- RTTIPlainType<DataType>::toMemory(value, (char*)buffer);
- }
- virtual void arrayElemToBuffer(void* object, int index, void* buffer)
- {
- checkIsArray(true);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- getArrayValue(castObject, index, value);
- RTTIPlainType<DataType>::toMemory(value, (char*)buffer);
- }
- virtual void fromBuffer(void* object, void* buffer)
- {
- checkIsArray(false);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- RTTIPlainType<DataType>::fromMemory(value, (char*)buffer);
- setValue(castObject, value);
- }
- virtual void arrayElemFromBuffer(void* object, int index, void* buffer)
- {
- checkIsArray(true);
- checkType<DataType>();
- ObjectType* castObject = static_cast<ObjectType*>(object);
- DataType value;
- RTTIPlainType<DataType>::fromMemory(value, (char*)buffer);
- setArrayValue(castObject, index, value);
- }
- };
- }
|