Serializable.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Attribute.h"
  25. #include "Object.h"
  26. #include <cstddef>
  27. class Deserializer;
  28. class Serializer;
  29. class XMLElement;
  30. /// Base class for objects with automatic serialization through attributes.
  31. class Serializable : public Object
  32. {
  33. OBJECT(Serializable);
  34. public:
  35. /// Construct.
  36. Serializable(Context* context);
  37. /// Destruct.
  38. virtual ~Serializable();
  39. /// Handle attribute write access. Default implementation writes to the variable at offset, or invokes the set accessor.
  40. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  41. /// Handle attribute read access. Default implementation reads the variable at offset, or invokes the get accessor.
  42. virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest);
  43. /// Load from binary data. Return true if successful.
  44. virtual bool Load(Deserializer& source);
  45. /// Save as binary data. Return true if successful.
  46. virtual bool Save(Serializer& dest);
  47. /// Load from XML data. Return true if successful.
  48. virtual bool LoadXML(const XMLElement& source);
  49. /// Save as XML data. Return true if successful.
  50. virtual bool SaveXML(XMLElement& dest);
  51. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  52. virtual void ApplyAttributes() {}
  53. /// %Set attribute by index. Return true if successfully set.
  54. bool SetAttribute(unsigned index, const Variant& value);
  55. /// %Set attribute by name. Return true if successfully set.
  56. bool SetAttribute(const String& name, const Variant& value);
  57. /// Write initial delta network update (compared to default attribute values) and prepare the last sent state.
  58. void WriteInitialDeltaUpdate(unsigned frameNumber, Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState);
  59. /// Prepare delta and latest data network updates. Needs a previously prepared last sent state from WriteInitialDeltaUpdate().
  60. void PrepareUpdates(unsigned frameNumber, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState, bool& deltaUpdate, bool& latestData);
  61. /// Write a delta network update prepared with PrepareUpdates().
  62. void WriteDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState);
  63. /// Write a latestdata network update prepared with PrepareUpdates().
  64. void WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& replicationState);
  65. /// Read and apply a network delta update.
  66. void ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits);
  67. /// Read and apply a network latest data update.
  68. void ReadLatestDataUpdate(Deserializer& source);
  69. /// Return attribute value by index. Return empty if illegal index.
  70. Variant GetAttribute(unsigned index);
  71. /// Return attribute value by name. Return empty if not found.
  72. Variant GetAttribute(const String& name);
  73. /// Return number of attributes.
  74. unsigned GetNumAttributes() const;
  75. /// Return number of network replication attributes.
  76. unsigned GetNumNetworkAttributes() const;
  77. /// Return attribute descriptions, or null if none defined.
  78. const Vector<AttributeInfo>* GetAttributes() const;
  79. /// Return network replication attribute descriptions, or null if none defined.
  80. const Vector<AttributeInfo>* GetNetworkAttributes() const;
  81. /// Return whether is loading attributes from a file. Is false during network deserialization.
  82. bool IsLoading() const { return loading_; }
  83. private:
  84. /// Current attributes for sending network updates. Updated only once per network frame, not per user.
  85. Vector<Variant> currentState_;
  86. /// Cached network attribute infos.
  87. mutable const Vector<AttributeInfo>* networkAttributes_;
  88. /// Last server frame number.
  89. unsigned serverFrameNumber_;
  90. /// Is loading flag.
  91. bool loading_;
  92. };
  93. /// Template implementation of the attribute accessor invoke helper class.
  94. template <class T, class U> class AttributeAccessorImpl : public AttributeAccessor
  95. {
  96. public:
  97. typedef U (T::*GetFunctionPtr)() const;
  98. typedef void (T::*SetFunctionPtr)(U);
  99. /// Construct with function pointers.
  100. AttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  101. getFunction_(getFunction),
  102. setFunction_(setFunction)
  103. {
  104. assert(getFunction_);
  105. assert(setFunction_);
  106. }
  107. /// Invoke getter function.
  108. virtual void Get(Serializable* ptr, Variant& dest)
  109. {
  110. assert(ptr);
  111. T* classPtr = static_cast<T*>(ptr);
  112. dest = (classPtr->*getFunction_)();
  113. }
  114. /// Invoke setter function.
  115. virtual void Set(Serializable* ptr, const Variant& value)
  116. {
  117. assert(ptr);
  118. T* classPtr = static_cast<T*>(ptr);
  119. (classPtr->*setFunction_)(value.Get<U>());
  120. }
  121. /// Class-specific pointer to getter function.
  122. GetFunctionPtr getFunction_;
  123. /// Class-specific pointer to setter function.
  124. SetFunctionPtr setFunction_;
  125. };
  126. /// Template implementation of the attribute accessor invoke helper class using const references.
  127. template <class T, class U> class RefAttributeAccessorImpl : public AttributeAccessor
  128. {
  129. public:
  130. typedef const U& (T::*GetFunctionPtr)() const;
  131. typedef void (T::*SetFunctionPtr)(const U&);
  132. /// Construct with function pointers.
  133. RefAttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  134. getFunction_(getFunction),
  135. setFunction_(setFunction)
  136. {
  137. assert(getFunction_);
  138. assert(setFunction_);
  139. }
  140. /// Invoke getter function.
  141. virtual void Get(Serializable* ptr, Variant& dest)
  142. {
  143. assert(ptr);
  144. T* classPtr = static_cast<T*>(ptr);
  145. dest = (classPtr->*getFunction_)();
  146. }
  147. /// Invoke setter function.
  148. virtual void Set(Serializable* ptr, const Variant& value)
  149. {
  150. assert(ptr);
  151. T* classPtr = static_cast<T*>(ptr);
  152. (classPtr->*setFunction_)(value.Get<U>());
  153. }
  154. /// Class-specific pointer to getter function.
  155. GetFunctionPtr getFunction_;
  156. /// Class-specific pointer to setter function.
  157. SetFunctionPtr setFunction_;
  158. };
  159. #define COPY_BASE_ATTRIBUTES(className, sourceClassName) context->CopyBaseAttributes<sourceClassName, className>()
  160. #define REMOVE_ATTRIBUTE(className, name) context->RemoveAttribute<className>(name)
  161. #define ATTRIBUTE(className, type, name, variable, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(type, name, offsetof(className, variable), defaultValue, mode))
  162. #define ENUM_ATTRIBUTE(className, name, variable, enumNames, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(name, offsetof(className, variable), enumNames, defaultValue, mode))
  163. #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))
  164. #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))
  165. #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))