SerializableAttribute.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. class RTTI;
  6. class IObjectStreamIn;
  7. class IObjectStreamOut;
  8. /// Data type
  9. enum class EOSDataType
  10. {
  11. /// Control codes
  12. Declare, ///< Used to declare the attributes of a new object type
  13. Object, ///< Start of a new object
  14. Instance, ///< Used in attribute declaration, indicates that an object is an instanced attribute (no pointer)
  15. Pointer, ///< Used in attribute declaration, indicates that an object is a pointer attribute
  16. Array, ///< Used in attribute declaration, indicates that this is an array of objects
  17. // Basic types (primitives)
  18. #define JPH_DECLARE_PRIMITIVE(name) T_##name,
  19. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  20. #include <Jolt/ObjectStream/ObjectStreamTypes.h>
  21. // Error values for read functions
  22. Invalid, ///< Next token on the stream was not a valid data type
  23. };
  24. /// Attributes are members of classes that need to be serialized.
  25. class SerializableAttribute
  26. {
  27. public:
  28. ///@ Serialization functions
  29. using pGetMemberPrimitiveType = const RTTI * (*)();
  30. using pIsType = bool (*)(int inArrayDepth, EOSDataType inDataType, const char *inClassName);
  31. using pReadData = bool (*)(IObjectStreamIn &ioStream, void *inObject);
  32. using pWriteData = void (*)(IObjectStreamOut &ioStream, const void *inObject);
  33. using pWriteDataType = void (*)(IObjectStreamOut &ioStream);
  34. /// Constructor
  35. SerializableAttribute(const char *inName, uint inMemberOffset, pGetMemberPrimitiveType inGetMemberPrimitiveType, pIsType inIsType, pReadData inReadData, pWriteData inWriteData, pWriteDataType inWriteDataType) : mName(inName), mMemberOffset(inMemberOffset), mGetMemberPrimitiveType(inGetMemberPrimitiveType), mIsType(inIsType), mReadData(inReadData), mWriteData(inWriteData), mWriteDataType(inWriteDataType) { }
  36. /// Construct from other attribute with base class offset
  37. SerializableAttribute(const SerializableAttribute &inOther, int inBaseOffset) : mName(inOther.mName), mMemberOffset(inOther.mMemberOffset + inBaseOffset), mGetMemberPrimitiveType(inOther.mGetMemberPrimitiveType), mIsType(inOther.mIsType), mReadData(inOther.mReadData), mWriteData(inOther.mWriteData), mWriteDataType(inOther.mWriteDataType) { }
  38. /// Name of the attribute
  39. void SetName(const char *inName) { mName = inName; }
  40. const char * GetName() const { return mName; }
  41. /// Access to the memory location that contains the member
  42. template <class T>
  43. inline T * GetMemberPointer(void *inObject) const { return reinterpret_cast<T *>(reinterpret_cast<uint8 *>(inObject) + mMemberOffset); }
  44. template <class T>
  45. inline const T * GetMemberPointer(const void *inObject) const { return reinterpret_cast<const T *>(reinterpret_cast<const uint8 *>(inObject) + mMemberOffset); }
  46. /// In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
  47. const RTTI * GetMemberPrimitiveType() const
  48. {
  49. return mGetMemberPrimitiveType();
  50. }
  51. /// Check if this attribute is of a specific type
  52. bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
  53. {
  54. return mIsType(inArrayDepth, inDataType, inClassName);
  55. }
  56. /// Read the data for this attribute into attribute containing class inObject
  57. bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
  58. {
  59. return mReadData(ioStream, GetMemberPointer<void>(inObject));
  60. }
  61. /// Write the data for this attribute from attribute containing class inObject
  62. void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
  63. {
  64. mWriteData(ioStream, GetMemberPointer<void>(inObject));
  65. }
  66. /// Write the data type of this attribute to a stream
  67. void WriteDataType(IObjectStreamOut &ioStream) const
  68. {
  69. mWriteDataType(ioStream);
  70. }
  71. private:
  72. // Name of the attribute
  73. const char * mName;
  74. // Offset of the member relative to the class
  75. uint mMemberOffset;
  76. // In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
  77. pGetMemberPrimitiveType mGetMemberPrimitiveType;
  78. // Serialization operations
  79. pIsType mIsType;
  80. pReadData mReadData;
  81. pWriteData mWriteData;
  82. pWriteDataType mWriteDataType;
  83. };
  84. JPH_NAMESPACE_END