Serializable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Attribute.h"
  24. #include "Object.h"
  25. #include <cstddef>
  26. namespace Urho3D
  27. {
  28. class Connection;
  29. class Deserializer;
  30. class Serializer;
  31. class XMLElement;
  32. struct DirtyBits;
  33. struct NetworkState;
  34. struct ReplicationState;
  35. /// Base class for objects with automatic serialization through attributes.
  36. class URHO3D_API Serializable : public Object
  37. {
  38. OBJECT(Serializable);
  39. public:
  40. /// Construct.
  41. Serializable(Context* context);
  42. /// Destruct.
  43. virtual ~Serializable();
  44. /// Handle attribute write access. Default implementation writes to the variable at offset, or invokes the set accessor.
  45. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  46. /// Handle attribute read access. Default implementation reads the variable at offset, or invokes the get accessor.
  47. virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const;
  48. /// Return attribute descriptions, or null if none defined.
  49. virtual const Vector<AttributeInfo>* GetAttributes() const;
  50. /// Return network replication attribute descriptions, or null if none defined.
  51. virtual const Vector<AttributeInfo>* GetNetworkAttributes() const;
  52. /// Load from binary data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
  53. virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
  54. /// Save as binary data. Return true if successful.
  55. virtual bool Save(Serializer& dest) const;
  56. /// Load from XML data. When setInstanceDefault is set to true, after setting the attribute value, store the value as instance's default value. Return true if successful.
  57. virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
  58. /// Save as XML data. Return true if successful.
  59. virtual bool SaveXML(XMLElement& dest) const;
  60. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  61. virtual void ApplyAttributes() {}
  62. /// Return whether should save default-valued attributes into XML. Default false.
  63. virtual bool SaveDefaultAttributes() const { return false; }
  64. /// Mark for attribute check on the next network update.
  65. virtual void MarkNetworkUpdate() {}
  66. /// Set attribute by index. Return true if successfully set.
  67. bool SetAttribute(unsigned index, const Variant& value);
  68. /// Set attribute by name. Return true if successfully set.
  69. bool SetAttribute(const String& name, const Variant& value);
  70. /// Reset all editable attributes to their default values.
  71. void ResetToDefault();
  72. /// Remove instance's default values if they are set previously.
  73. void RemoveInstanceDefault();
  74. /// Set temporary flag. Temporary objects will not be saved.
  75. void SetTemporary(bool enable);
  76. /// Allocate network attribute state.
  77. void AllocateNetworkState();
  78. /// Write initial delta network update.
  79. void WriteInitialDeltaUpdate(Serializer& dest);
  80. /// Write a delta network update according to dirty attribute bits.
  81. void WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits);
  82. /// Write a latest data network update.
  83. void WriteLatestDataUpdate(Serializer& dest);
  84. /// Read and apply a network delta update.
  85. void ReadDeltaUpdate(Deserializer& source);
  86. /// Read and apply a network latest data update.
  87. void ReadLatestDataUpdate(Deserializer& source);
  88. /// Return attribute value by index. Return empty if illegal index.
  89. Variant GetAttribute(unsigned index) const;
  90. /// Return attribute value by name. Return empty if not found.
  91. Variant GetAttribute(const String& name) const;
  92. /// Return attribute default value by index. Return empty if illegal index.
  93. Variant GetAttributeDefault(unsigned index) const;
  94. /// Return attribute default value by name. Return empty if not found.
  95. Variant GetAttributeDefault(const String& name) const;
  96. /// Return number of attributes.
  97. unsigned GetNumAttributes() const;
  98. /// Return number of network replication attributes.
  99. unsigned GetNumNetworkAttributes() const;
  100. /// Return whether is temporary.
  101. bool IsTemporary() const { return temporary_; }
  102. protected:
  103. /// Network attribute state.
  104. NetworkState* networkState_;
  105. private:
  106. /// Set instance-level default value. Allocate the internal data structure as necessary.
  107. void SetInstanceDefault(const String& name, const Variant& defaultValue);
  108. /// Get instance-level default value.
  109. Variant GetInstanceDefault(const String& name) const;
  110. /// Attribute default value at each instance level.
  111. VariantMap* instanceDefaultValues_;
  112. /// Temporary flag.
  113. bool temporary_;
  114. };
  115. /// Template implementation of the enum attribute accessor invoke helper class.
  116. template <typename T, typename U> class EnumAttributeAccessorImpl : public AttributeAccessor
  117. {
  118. public:
  119. typedef U (T::*GetFunctionPtr)() const;
  120. typedef void (T::*SetFunctionPtr)(U);
  121. /// Construct with function pointers.
  122. EnumAttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  123. getFunction_(getFunction),
  124. setFunction_(setFunction)
  125. {
  126. assert(getFunction_);
  127. assert(setFunction_);
  128. }
  129. /// Invoke getter function.
  130. virtual void Get(const Serializable* ptr, Variant& dest) const
  131. {
  132. assert(ptr);
  133. const T* classPtr = static_cast<const T*>(ptr);
  134. dest = (classPtr->*getFunction_)();
  135. }
  136. /// Invoke setter function.
  137. virtual void Set(Serializable* ptr, const Variant& value)
  138. {
  139. assert(ptr);
  140. T* classPtr = static_cast<T*>(ptr);
  141. (classPtr->*setFunction_)((U)value.GetInt());
  142. }
  143. /// Class-specific pointer to getter function.
  144. GetFunctionPtr getFunction_;
  145. /// Class-specific pointer to setter function.
  146. SetFunctionPtr setFunction_;
  147. };
  148. /// Attribute trait (default use const reference for object type).
  149. template<typename T> struct AttributeTrait
  150. {
  151. /// Get function return type.
  152. typedef const T& ReturnType;
  153. /// Set function parameter type.
  154. typedef const T& ParameterType;
  155. };
  156. /// Int attribute trait.
  157. template<> struct AttributeTrait<int>
  158. {
  159. typedef int ReturnType;
  160. typedef int ParameterType;
  161. };
  162. /// unsigned attribute trait.
  163. template<> struct AttributeTrait<unsigned>
  164. {
  165. typedef unsigned ReturnType;
  166. typedef unsigned ParameterType;
  167. };
  168. /// Bool attribute trait.
  169. template<> struct AttributeTrait<bool>
  170. {
  171. typedef bool ReturnType;
  172. typedef bool ParameterType;
  173. };
  174. /// Float attribute trait.
  175. template<> struct AttributeTrait<float>
  176. {
  177. typedef float ReturnType;
  178. typedef float ParameterType;
  179. };
  180. /// Mixed attribute trait (use const reference for set function only).
  181. template<typename T> struct MixedAttributeTrait
  182. {
  183. typedef T ReturnType;
  184. typedef const T& ParameterType;
  185. };
  186. /// Template implementation of the attribute accessor invoke helper class.
  187. template <typename T, typename U, typename Trait> class AttributeAccessorImpl : public AttributeAccessor
  188. {
  189. public:
  190. typedef typename Trait ::ReturnType (T::*GetFunctionPtr)() const;
  191. typedef void (T::*SetFunctionPtr)(typename Trait ::ParameterType);
  192. /// Construct with function pointers.
  193. AttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  194. getFunction_(getFunction),
  195. setFunction_(setFunction)
  196. {
  197. assert(getFunction_);
  198. assert(setFunction_);
  199. }
  200. /// Invoke getter function.
  201. virtual void Get(const Serializable* ptr, Variant& dest) const
  202. {
  203. assert(ptr);
  204. const T* classPtr = static_cast<const T*>(ptr);
  205. dest = (classPtr->*getFunction_)();
  206. }
  207. /// Invoke setter function.
  208. virtual void Set(Serializable* ptr, const Variant& value)
  209. {
  210. assert(ptr);
  211. T* classPtr = static_cast<T*>(ptr);
  212. (classPtr->*setFunction_)(value.Get<U>());
  213. }
  214. /// Class-specific pointer to getter function.
  215. GetFunctionPtr getFunction_;
  216. /// Class-specific pointer to setter function.
  217. SetFunctionPtr setFunction_;
  218. };
  219. #define COPY_BASE_ATTRIBUTES(sourceClassName) context->CopyBaseAttributes<sourceClassName, ClassName>()
  220. #define REMOVE_ATTRIBUTE(name) context->RemoveAttribute<ClassName>(name)
  221. #define ENUM_ATTRIBUTE(name, variable, enumNames, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(name, offsetof(ClassName, variable), enumNames, defaultValue, mode))
  222. #define ATTRIBUTE(name, typeName, variable, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName >(), name, offsetof(ClassName, variable), defaultValue, mode))
  223. #define ENUM_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, enumNames, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(name, new Urho3D::EnumAttributeAccessorImpl<ClassName, typeName >(&ClassName::getFunction, &ClassName::setFunction), enumNames, defaultValue, mode))
  224. #define ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName >(), name, new Urho3D::AttributeAccessorImpl<ClassName, typeName, AttributeTrait<typeName > >(&ClassName::getFunction, &ClassName::setFunction), defaultValue, mode))
  225. #define MIXED_ACCESSOR_ATTRIBUTE(name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<ClassName>(Urho3D::AttributeInfo(GetVariantType<typeName >(), name, new Urho3D::AttributeAccessorImpl<ClassName, typeName, MixedAttributeTrait<typeName > >(&ClassName::getFunction, &ClassName::setFunction), defaultValue, mode))
  226. #define UPDATE_ATTRIBUTE_DEFAULT_VALUE(name, defaultValue) context->UpdateAttributeDefaultValue<ClassName>(name, defaultValue)
  227. }