SerializableAttribute.h 4.4 KB

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