Serializable.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. class Deserializer;
  27. class Serializer;
  28. class XMLElement;
  29. /// Base class for objects with automatic serialization through attributes.
  30. class Serializable : public Object
  31. {
  32. OBJECT(Serializable);
  33. public:
  34. /// Construct.
  35. Serializable(Context* context);
  36. /// Destruct.
  37. virtual ~Serializable();
  38. /// Handle attribute write access. Default implementation writes to the variable at offset, or invokes the set accessor.
  39. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  40. /// Handle attribute read access. Default implementation reads the variable at offset, or invokes the get accessor.
  41. virtual void OnGetAttribute(const AttributeInfo& attr, Variant& dest);
  42. /// Load from binary data. Return true if successful.
  43. virtual bool Load(Deserializer& source);
  44. /// Save as binary data. Return true if successful.
  45. virtual bool Save(Serializer& dest);
  46. /// Load from XML data. Return true if successful.
  47. virtual bool LoadXML(const XMLElement& source);
  48. /// Save as XML data. Return true if successful.
  49. virtual bool SaveXML(XMLElement& dest);
  50. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  51. virtual void ApplyAttributes() {}
  52. /// %Set attribute by index. Return true if successfully set.
  53. bool SetAttribute(unsigned index, const Variant& value);
  54. /// %Set attribute by name. Return true if successfully set.
  55. bool SetAttribute(const String& name, const Variant& value);
  56. /// Write initial delta network update (compared to default attribute values) and prepare the last sent state.
  57. void WriteInitialDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState);
  58. /// Prepare delta and latest data network updates. Needs a previously prepared last sent state from WriteInitialDeltaUpdate().
  59. void PrepareUpdates(PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& classCurrentState, Vector<Variant>& replicationState, bool& deltaUpdate, bool& latestData);
  60. /// Write a delta network update prepared with PrepareUpdates().
  61. void WriteDeltaUpdate(Serializer& dest, PODVector<unsigned char>& deltaUpdateBits, Vector<Variant>& replicationState);
  62. /// Write a latestdata network update prepared with PrepareUpdates().
  63. void WriteLatestDataUpdate(Serializer& dest, Vector<Variant>& replicationState);
  64. /// Read and apply a network delta update.
  65. void ReadDeltaUpdate(Deserializer& source, PODVector<unsigned char>& deltaUpdateBits);
  66. /// Read and apply a network latest data update.
  67. void ReadLatestDataUpdate(Deserializer& source);
  68. /// Return attribute value by index. Return empty if illegal index.
  69. Variant GetAttribute(unsigned index);
  70. /// Return attribute value by name. Return empty if not found.
  71. Variant GetAttribute(const String& name);
  72. /// Return number of attributes.
  73. unsigned GetNumAttributes() const;
  74. /// Return number of network replication attributes.
  75. unsigned GetNumNetworkAttributes() const;
  76. /// Return attribute descriptions, or null if none defined.
  77. const Vector<AttributeInfo>* GetAttributes() const;
  78. /// Return network replication attribute descriptions, or null if none defined.
  79. const Vector<AttributeInfo>* GetNetworkAttributes() const;
  80. /// Return whether is loading attributes from a file. Is false during network deserialization.
  81. bool IsLoading() const { return loading_; }
  82. protected:
  83. /// Is loading flag.
  84. bool loading_;
  85. };
  86. /// Template implementation of the attribute accessor invoke helper class.
  87. template <class T, class U> class AttributeAccessorImpl : public AttributeAccessor
  88. {
  89. public:
  90. typedef U (T::*GetFunctionPtr)() const;
  91. typedef void (T::*SetFunctionPtr)(U);
  92. /// Construct with function pointers.
  93. AttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  94. getFunction_(getFunction),
  95. setFunction_(setFunction)
  96. {
  97. assert(getFunction_);
  98. assert(setFunction_);
  99. }
  100. /// Invoke getter function.
  101. virtual void Get(Serializable* ptr, Variant& dest)
  102. {
  103. assert(ptr);
  104. T* classPtr = static_cast<T*>(ptr);
  105. dest = (classPtr->*getFunction_)();
  106. }
  107. /// Invoke setter function.
  108. virtual void Set(Serializable* ptr, const Variant& value)
  109. {
  110. assert(ptr);
  111. T* classPtr = static_cast<T*>(ptr);
  112. (classPtr->*setFunction_)(value.Get<U>());
  113. }
  114. /// Class-specific pointer to getter function.
  115. GetFunctionPtr getFunction_;
  116. /// Class-specific pointer to setter function.
  117. SetFunctionPtr setFunction_;
  118. };
  119. /// Template implementation of the attribute accessor invoke helper class using const references.
  120. template <class T, class U> class RefAttributeAccessorImpl : public AttributeAccessor
  121. {
  122. public:
  123. typedef const U& (T::*GetFunctionPtr)() const;
  124. typedef void (T::*SetFunctionPtr)(const U&);
  125. /// Construct with function pointers.
  126. RefAttributeAccessorImpl(GetFunctionPtr getFunction, SetFunctionPtr setFunction) :
  127. getFunction_(getFunction),
  128. setFunction_(setFunction)
  129. {
  130. assert(getFunction_);
  131. assert(setFunction_);
  132. }
  133. /// Invoke getter function.
  134. virtual void Get(Serializable* ptr, Variant& dest)
  135. {
  136. assert(ptr);
  137. T* classPtr = static_cast<T*>(ptr);
  138. dest = (classPtr->*getFunction_)();
  139. }
  140. /// Invoke setter function.
  141. virtual void Set(Serializable* ptr, const Variant& value)
  142. {
  143. assert(ptr);
  144. T* classPtr = static_cast<T*>(ptr);
  145. (classPtr->*setFunction_)(value.Get<U>());
  146. }
  147. /// Class-specific pointer to getter function.
  148. GetFunctionPtr getFunction_;
  149. /// Class-specific pointer to setter function.
  150. SetFunctionPtr setFunction_;
  151. };
  152. #define COPY_BASE_ATTRIBUTES(className, sourceClassName) context->CopyBaseAttributes<sourceClassName, className>()
  153. #define REMOVE_ATTRIBUTE(className, name) context->RemoveAttribute<className>(name)
  154. #define ATTRIBUTE(className, type, name, variable, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(type, name, offsetof(className, variable), defaultValue, mode))
  155. #define ENUM_ATTRIBUTE(className, name, variable, enumNames, defaultValue, mode) context->RegisterAttribute<className>(AttributeInfo(name, offsetof(className, variable), enumNames, defaultValue, mode))
  156. #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))
  157. #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))
  158. #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))