Serializable.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Copyright (c) 2008-2013 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 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);
  48. /// Load from binary data. Return true if successful.
  49. virtual bool Load(Deserializer& source);
  50. /// Save as binary data. Return true if successful.
  51. virtual bool Save(Serializer& dest);
  52. /// Load from XML data. Return true if successful.
  53. virtual bool LoadXML(const XMLElement& source);
  54. /// Save as XML data. Return true if successful.
  55. virtual bool SaveXML(XMLElement& dest);
  56. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  57. virtual void ApplyAttributes() {}
  58. /// Return whether should save default-valued attributes into XML. Default false.
  59. virtual bool SaveDefaultAttributes() const { return false; }
  60. /// Set attribute by index. Return true if successfully set.
  61. bool SetAttribute(unsigned index, const Variant& value);
  62. /// Set attribute by name. Return true if successfully set.
  63. bool SetAttribute(const String& name, const Variant& value);
  64. /// Allocate network attribute state.
  65. void AllocateNetworkState();
  66. /// Write initial delta network update.
  67. void WriteInitialDeltaUpdate(Serializer& dest);
  68. /// Write a delta network update according to dirty attribute bits.
  69. void WriteDeltaUpdate(Serializer& dest, const DirtyBits& attributeBits);
  70. /// Write a latest data network update.
  71. void WriteLatestDataUpdate(Serializer& dest);
  72. /// Read and apply a network delta update.
  73. void ReadDeltaUpdate(Deserializer& source);
  74. /// Read and apply a network latest data update.
  75. void ReadLatestDataUpdate(Deserializer& source);
  76. /// Return attribute value by index. Return empty if illegal index.
  77. Variant GetAttribute(unsigned index);
  78. /// Return attribute value by name. Return empty if not found.
  79. Variant GetAttribute(const String& name);
  80. /// Return number of attributes.
  81. unsigned GetNumAttributes() const;
  82. /// Return number of network replication attributes.
  83. unsigned GetNumNetworkAttributes() const;
  84. /// Return attribute descriptions, or null if none defined.
  85. const Vector<AttributeInfo>* GetAttributes() const;
  86. /// Return network replication attribute descriptions, or null if none defined.
  87. const Vector<AttributeInfo>* GetNetworkAttributes() const;
  88. protected:
  89. /// Network attribute state.
  90. NetworkState* networkState_;
  91. };
  92. /// Template implementation of the attribute accessor invoke helper class.
  93. template <class T, class U> class AttributeAccessorImpl : public AttributeAccessor
  94. {
  95. public:
  96. typedef U (T::*GetFunctionPtr)() const;
  97. typedef void (T::*SetFunctionPtr)(U);
  98. /// Construct with function pointers.
  99. AttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  100. getFunction_(getFunction),
  101. setFunction_(setFunction)
  102. {
  103. assert(getFunction_);
  104. assert(setFunction_);
  105. }
  106. /// Invoke getter function.
  107. virtual void Get(Serializable* ptr, Variant& dest)
  108. {
  109. assert(ptr);
  110. T* classPtr = static_cast<T*>(ptr);
  111. dest = (classPtr->*getFunction_)();
  112. }
  113. /// Invoke setter function.
  114. virtual void Set(Serializable* ptr, const Variant& value)
  115. {
  116. assert(ptr);
  117. T* classPtr = static_cast<T*>(ptr);
  118. (classPtr->*setFunction_)(value.Get<U>());
  119. }
  120. /// Class-specific pointer to getter function.
  121. GetFunctionPtr getFunction_;
  122. /// Class-specific pointer to setter function.
  123. SetFunctionPtr setFunction_;
  124. };
  125. /// Template implementation of the attribute accessor invoke helper class using const references.
  126. template <class T, class U> class RefAttributeAccessorImpl : public AttributeAccessor
  127. {
  128. public:
  129. typedef const U& (T::*GetFunctionPtr)() const;
  130. typedef void (T::*SetFunctionPtr)(const U&);
  131. /// Construct with function pointers.
  132. RefAttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  133. getFunction_(getFunction),
  134. setFunction_(setFunction)
  135. {
  136. assert(getFunction_);
  137. assert(setFunction_);
  138. }
  139. /// Invoke getter function.
  140. virtual void Get(Serializable* ptr, Variant& dest)
  141. {
  142. assert(ptr);
  143. T* classPtr = static_cast<T*>(ptr);
  144. dest = (classPtr->*getFunction_)();
  145. }
  146. /// Invoke setter function.
  147. virtual void Set(Serializable* ptr, const Variant& value)
  148. {
  149. assert(ptr);
  150. T* classPtr = static_cast<T*>(ptr);
  151. (classPtr->*setFunction_)(value.Get<U>());
  152. }
  153. /// Class-specific pointer to getter function.
  154. GetFunctionPtr getFunction_;
  155. /// Class-specific pointer to setter function.
  156. SetFunctionPtr setFunction_;
  157. };
  158. #define COPY_BASE_ATTRIBUTES(className, sourceClassName) context->CopyBaseAttributes<sourceClassName, className>()
  159. #define REMOVE_ATTRIBUTE(className, name) context->RemoveAttribute<className>(name)
  160. #define ATTRIBUTE(className, type, name, variable, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(type, name, offsetof(className, variable), defaultValue, mode))
  161. #define ENUM_ATTRIBUTE(className, name, variable, enumNames, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(name, offsetof(className, variable), enumNames, defaultValue, mode))
  162. #define ACCESSOR_ATTRIBUTE(className, type, name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(type, name, new AttributeAccessorImpl<className, typeName>(&className::getFunction, &className::setFunction), defaultValue, mode))
  163. #define ENUM_ACCESSOR_ATTRIBUTE(className, name, getFunction, setFunction, typeName, enumNames, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(name, new AttributeAccessorImpl<className, typeName>(&className::getFunction, &className::setFunction), enumNames, defaultValue, mode))
  164. #define REF_ACCESSOR_ATTRIBUTE(className, type, name, getFunction, setFunction, typeName, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(type, name, new RefAttributeAccessorImpl<className, typeName>(&className::getFunction, &className::setFunction), defaultValue, mode))
  165. }