Attribute.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2017 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 "../Container/Ptr.h"
  24. #include "../Core/Variant.h"
  25. namespace Atomic
  26. {
  27. /// Attribute shown only in the editor, but not serialized.
  28. static const unsigned AM_EDIT = 0x0;
  29. /// Attribute used for file serialization.
  30. static const unsigned AM_FILE = 0x1;
  31. /// Attribute used for network replication.
  32. static const unsigned AM_NET = 0x2;
  33. /// Attribute used for both file serialization and network replication (default).
  34. static const unsigned AM_DEFAULT = 0x3;
  35. /// Attribute should use latest data grouping instead of delta update in network replication.
  36. static const unsigned AM_LATESTDATA = 0x4;
  37. /// Attribute should not be shown in the editor.
  38. static const unsigned AM_NOEDIT = 0x8;
  39. /// Attribute is a node ID and may need rewriting.
  40. static const unsigned AM_NODEID = 0x10;
  41. /// Attribute is a component ID and may need rewriting.
  42. static const unsigned AM_COMPONENTID = 0x20;
  43. /// Attribute is a node ID vector where first element is the amount of nodes.
  44. static const unsigned AM_NODEIDVECTOR = 0x40;
  45. /// Attribute is readonly. Can't be used with binary serialized objects.
  46. static const unsigned AM_FILEREADONLY = 0x81;
  47. class Serializable;
  48. /// Abstract base class for invoking attribute accessors.
  49. class ATOMIC_API AttributeAccessor : public RefCounted
  50. {
  51. ATOMIC_REFCOUNTED(AttributeAccessor)
  52. public:
  53. /// Get the attribute.
  54. virtual void Get(const Serializable* ptr, Variant& dest) const = 0;
  55. /// Set the attribute.
  56. virtual void Set(Serializable* ptr, const Variant& src) = 0;
  57. };
  58. /// Description of an automatically serializable variable.
  59. struct AttributeInfo
  60. {
  61. /// Construct empty.
  62. AttributeInfo() :
  63. type_(VAR_NONE),
  64. offset_(0),
  65. enumNames_(0),
  66. variantStructureElementNames_(0),
  67. mode_(AM_DEFAULT),
  68. ptr_(0)
  69. {
  70. }
  71. /// Construct offset attribute.
  72. AttributeInfo(VariantType type, const char* name, size_t offset, const Variant& defaultValue, unsigned mode) :
  73. type_(type),
  74. name_(name),
  75. offset_((unsigned)offset),
  76. enumNames_(0),
  77. variantStructureElementNames_(0),
  78. defaultValue_(defaultValue),
  79. mode_(mode),
  80. ptr_(0)
  81. {
  82. }
  83. /// Construct offset enum attribute.
  84. AttributeInfo(const char* name, size_t offset, const char** enumNames, const Variant& defaultValue, unsigned mode) :
  85. type_(VAR_INT),
  86. name_(name),
  87. offset_((unsigned)offset),
  88. enumNames_(enumNames),
  89. variantStructureElementNames_(0),
  90. defaultValue_(defaultValue),
  91. mode_(mode),
  92. ptr_(0)
  93. {
  94. }
  95. /// Construct accessor attribute.
  96. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  97. type_(type),
  98. name_(name),
  99. offset_(0),
  100. enumNames_(0),
  101. variantStructureElementNames_(0),
  102. accessor_(accessor),
  103. defaultValue_(defaultValue),
  104. mode_(mode),
  105. ptr_(0)
  106. {
  107. }
  108. /// Construct accessor enum attribute.
  109. AttributeInfo(const char* name, AttributeAccessor* accessor, const char** enumNames, const Variant& defaultValue,
  110. unsigned mode) :
  111. type_(VAR_INT),
  112. name_(name),
  113. offset_(0),
  114. enumNames_(enumNames),
  115. variantStructureElementNames_(0),
  116. accessor_(accessor),
  117. defaultValue_(defaultValue),
  118. mode_(mode),
  119. ptr_(0)
  120. {
  121. }
  122. /// Construct variant structure (structure, which packed to VariantVector) attribute.
  123. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, const char** variantStructureElementNames, unsigned mode) :
  124. type_(type),
  125. name_(name),
  126. offset_(0),
  127. enumNames_(0),
  128. variantStructureElementNames_(variantStructureElementNames),
  129. accessor_(accessor),
  130. defaultValue_(defaultValue),
  131. mode_(mode),
  132. ptr_(0)
  133. {
  134. }
  135. /// Attribute type.
  136. VariantType type_;
  137. /// Name.
  138. String name_;
  139. /// Byte offset from start of object.
  140. unsigned offset_;
  141. /// Enum names.
  142. const char** enumNames_;
  143. /// Variant structure elements names.
  144. const char** variantStructureElementNames_;
  145. /// Helper object for accessor mode.
  146. SharedPtr<AttributeAccessor> accessor_;
  147. /// Default value for network replication.
  148. Variant defaultValue_;
  149. /// Attribute mode: whether to use for serialization, network replication, or both.
  150. unsigned mode_;
  151. /// Attribute data pointer if elsewhere than in the Serializable.
  152. void* ptr_;
  153. };
  154. }