2
0

Serializable.h 8.0 KB

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