SerializableAttribute.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
  42. const RTTI * GetMemberPrimitiveType() const
  43. {
  44. return mGetMemberPrimitiveType();
  45. }
  46. /// Check if this attribute is of a specific type
  47. bool IsType(int inArrayDepth, EOSDataType inDataType, const char *inClassName) const
  48. {
  49. return mIsType(inArrayDepth, inDataType, inClassName);
  50. }
  51. /// Read the data for this attribute into attribute containing class inObject
  52. bool ReadData(IObjectStreamIn &ioStream, void *inObject) const
  53. {
  54. return mReadData(ioStream, reinterpret_cast<uint8 *>(inObject) + mMemberOffset);
  55. }
  56. /// Write the data for this attribute from attribute containing class inObject
  57. void WriteData(IObjectStreamOut &ioStream, const void *inObject) const
  58. {
  59. mWriteData(ioStream, reinterpret_cast<const uint8 *>(inObject) + mMemberOffset);
  60. }
  61. /// Write the data type of this attribute to a stream
  62. void WriteDataType(IObjectStreamOut &ioStream) const
  63. {
  64. mWriteDataType(ioStream);
  65. }
  66. private:
  67. // Name of the attribute
  68. const char * mName;
  69. // Offset of the member relative to the class
  70. uint mMemberOffset;
  71. // In case this attribute contains an RTTI type, return it (note that a Array<sometype> will return the rtti of sometype)
  72. pGetMemberPrimitiveType mGetMemberPrimitiveType;
  73. // Serialization operations
  74. pIsType mIsType;
  75. pReadData mReadData;
  76. pWriteData mWriteData;
  77. pWriteDataType mWriteDataType;
  78. };
  79. JPH_NAMESPACE_END