ObjectStream.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/StaticArray.h>
  5. #include <Core/Reference.h>
  6. namespace JPH {
  7. /// Base class for object stream input and output streams.
  8. class ObjectStream
  9. {
  10. public:
  11. /// Stream type
  12. enum class EStreamType
  13. {
  14. Text,
  15. Binary,
  16. };
  17. /// Data type
  18. enum class EDataType
  19. {
  20. /// Control codes
  21. Declare, ///< Used to declare the attributes of a new object type
  22. Object, ///< Start of a new object
  23. Instance, ///< Used in attribute declaration, indicates that an object is an instanced attribute (no pointer)
  24. Pointer, ///< Used in attribute declaration, indicates that an object is a pointer attribute
  25. Array, ///< Used in attribute declaration, indicates that this is an array of objects
  26. // Basic types (primitives)
  27. #define JPH_DECLARE_PRIMITIVE(name) T_##name,
  28. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  29. #include <ObjectStream/ObjectStreamTypes.h>
  30. // Error values for read functions
  31. Invalid, ///< Next token on the stream was not a valid data type
  32. };
  33. protected:
  34. /// Constructor
  35. virtual ~ObjectStream() = default;
  36. /// Identifier for objects
  37. using Identifier = uint32;
  38. static constexpr int sVersion = 1;
  39. static constexpr int sRevision = 0;
  40. static constexpr Identifier sNullIdentifier = 0;
  41. };
  42. // Define macro to declare functions for a specific primitive type
  43. #define JPH_DECLARE_PRIMITIVE(name) \
  44. bool OSIsType(name *inNull, int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName);
  45. // This file uses the JPH_DECLARE_PRIMITIVE macro to define all types
  46. #include <ObjectStream/ObjectStreamTypes.h>
  47. // Define serialization templates
  48. template <class T>
  49. bool OSIsType(vector<T> *inArray, int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName)
  50. {
  51. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  52. }
  53. template <class T, uint N>
  54. bool OSIsType(StaticArray<T, N> *inArray, int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName)
  55. {
  56. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  57. }
  58. template <class T, uint N>
  59. bool OSIsType(T (*inArray)[N], int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName)
  60. {
  61. return (inArrayDepth > 0 && OSIsType((T *)nullptr, inArrayDepth - 1, inDataType, inClassName));
  62. }
  63. template <class T>
  64. bool OSIsType(Ref<T> *inNull, int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName)
  65. {
  66. return OSIsType((T *)nullptr, inArrayDepth, inDataType, inClassName);
  67. }
  68. template <class T>
  69. bool OSIsType(RefConst<T> *inNull, int inArrayDepth, ObjectStream::EDataType inDataType, const char *inClassName)
  70. {
  71. return OSIsType((T *)nullptr, inArrayDepth, inDataType, inClassName);
  72. }
  73. } // JPH