ISerialize.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : main header file
  9. #pragma once
  10. #include <AzCore/Math/Vector3.h>
  11. #include <Cry_Math.h>
  12. #include <IXml.h>
  13. #include <StlUtils.h>
  14. // Forward declarations
  15. class CTimeValue;
  16. // this enumeration details what "kind" of serialization we are
  17. // performing, so that classes that want to, for instance, tailor
  18. // the data they present depending on where data is being written
  19. // to can do so
  20. enum class ESerializationTarget
  21. {
  22. eST_SaveGame,
  23. };
  24. //////////////////////////////////////////////////////////////////////////
  25. // Temporary class for string serialization.
  26. //////////////////////////////////////////////////////////////////////////
  27. struct SSerializeString
  28. {
  29. SSerializeString(){};
  30. SSerializeString(const SSerializeString& src)
  31. {
  32. m_str.assign(src.c_str());
  33. };
  34. explicit SSerializeString(const char* sbegin, const char* send)
  35. : m_str(sbegin, send){};
  36. ~SSerializeString()
  37. {
  38. }
  39. // Casting to const char*
  40. SSerializeString(const char* s)
  41. : m_str(s){};
  42. SSerializeString& operator=(const SSerializeString& src)
  43. {
  44. m_str.assign(src.c_str());
  45. return *this;
  46. }
  47. SSerializeString& operator=(const char* src)
  48. {
  49. m_str.assign(src);
  50. return *this;
  51. }
  52. bool operator!=(const SSerializeString& src)
  53. {
  54. return m_str != src.m_str;
  55. }
  56. size_t size() const
  57. {
  58. return m_str.size();
  59. }
  60. size_t length() const
  61. {
  62. return m_str.length();
  63. }
  64. const char* c_str() const
  65. {
  66. return m_str.c_str();
  67. };
  68. bool empty() const
  69. {
  70. return m_str.empty();
  71. }
  72. void resize(int sz)
  73. {
  74. m_str.resize(sz);
  75. }
  76. void reserve(int sz)
  77. {
  78. m_str.reserve(sz);
  79. }
  80. void set_string(const AZStd::string& s)
  81. {
  82. m_str.assign(s.begin(), s.size());
  83. }
  84. operator const AZStd::string() const
  85. {
  86. return m_str;
  87. }
  88. private:
  89. AZStd::string m_str;
  90. };
  91. // the ISerialize is intended to be implemented by objects that need
  92. // to read and write from various data sources, in such a way that
  93. // different tradeoffs can be balanced by the object that is being
  94. // serialized, and so that objects being serialized need only write
  95. // a single function in order to be read from and written to
  96. struct ISerialize
  97. {
  98. static const int ENUM_POLICY_TAG = 0xe0000000;
  99. ISerialize()
  100. {
  101. }
  102. virtual ~ISerialize()
  103. {
  104. }
  105. // this is for string values -- they need special support
  106. virtual void ReadStringValue(const char* name, SSerializeString& curValue) = 0;
  107. virtual void WriteStringValue(const char* name, SSerializeString& buffer) = 0;
  108. //////////////////////////////////////////////////////////////////////////
  109. // these functions should be implemented to deal with groups
  110. //////////////////////////////////////////////////////////////////////////
  111. // Begins a serialization group - must be matched by an EndGroup
  112. // szName is preferably as short as possible for performance reasons
  113. // Spaces in szName cause undefined behaviour, use alpha characters,underscore and numbers only for a name.
  114. virtual void BeginGroup(const char* szName) = 0;
  115. virtual bool BeginOptionalGroup(const char* szName, bool condition) = 0;
  116. virtual void EndGroup() = 0;
  117. //////////////////////////////////////////////////////////////////////////
  118. virtual bool IsReading() const = 0;
  119. // declare all primitive Value() implementations
  120. #define SERIALIZATION_TYPE(T) \
  121. virtual void Value(const char* name, T& x) = 0;
  122. #include "SerializationTypes.h"
  123. #undef SERIALIZATION_TYPE
  124. };
  125. // this class provides a wrapper so that ISerialize can be used much more
  126. // easily; it is a template so that if we need to wrap a more specific
  127. // ISerialize implementation we can do so easily
  128. template<class TISerialize>
  129. class CSerializeWrapper
  130. {
  131. public:
  132. CSerializeWrapper(TISerialize* pSerialize)
  133. : m_pSerialize(pSerialize)
  134. {
  135. }
  136. // we provide a wrapper around the abstract implementation
  137. // ISerialize to allow easy changing of our
  138. // interface, and easy implementation of our details.
  139. // some of the wrappers are trivial, however for consistency, they
  140. // have been made to follow the trend.
  141. // the value function allows us to declare that a value needs
  142. // to be serialized/deserialized;
  143. template<typename T_Value>
  144. ILINE void Value(const char* szName, T_Value& value)
  145. {
  146. m_pSerialize->Value(szName, value);
  147. }
  148. void Value(const char* szName, AZStd::string& value)
  149. {
  150. if (IsWriting())
  151. {
  152. SSerializeString& serializeString = SetSharedSerializeString(value);
  153. m_pSerialize->WriteStringValue(szName, serializeString);
  154. }
  155. else
  156. {
  157. value = "";
  158. SSerializeString& serializeString = SetSharedSerializeString(value);
  159. m_pSerialize->ReadStringValue(szName, serializeString);
  160. value = serializeString.c_str();
  161. }
  162. }
  163. void Value(const char* szName, const AZStd::string& value)
  164. {
  165. if (IsWriting())
  166. {
  167. SSerializeString& serializeString = SetSharedSerializeString(value);
  168. m_pSerialize->WriteStringValue(szName, serializeString);
  169. }
  170. else
  171. {
  172. assert(0 && "This function can only be used for Writing");
  173. }
  174. }
  175. // groups help us find common data
  176. ILINE void BeginGroup(const char* szName)
  177. {
  178. m_pSerialize->BeginGroup(szName);
  179. }
  180. ILINE bool BeginOptionalGroup(const char* szName, bool condition)
  181. {
  182. return m_pSerialize->BeginOptionalGroup(szName, condition);
  183. }
  184. ILINE void EndGroup()
  185. {
  186. m_pSerialize->EndGroup();
  187. }
  188. ILINE bool IsWriting() const
  189. {
  190. return !m_pSerialize->IsReading();
  191. }
  192. ILINE bool IsReading() const
  193. {
  194. return m_pSerialize->IsReading();
  195. }
  196. friend ILINE TISerialize* GetImpl(CSerializeWrapper<TISerialize> ser)
  197. {
  198. return ser.m_pSerialize;
  199. }
  200. operator CSerializeWrapper<ISerialize>()
  201. {
  202. return CSerializeWrapper<ISerialize>(m_pSerialize);
  203. }
  204. SSerializeString& SetSharedSerializeString(const AZStd::string& str)
  205. {
  206. static SSerializeString serializeString;
  207. serializeString.set_string(str);
  208. return serializeString;
  209. }
  210. private:
  211. TISerialize* m_pSerialize;
  212. };
  213. // default serialize class to use!!
  214. typedef CSerializeWrapper<ISerialize> TSerialize;