SerializableAttribute.h 4.4 KB

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