Attribute.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2008-2015 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. class Serializable;
  46. /// Abstract base class for invoking attribute accessors.
  47. class ATOMIC_API AttributeAccessor : public RefCounted
  48. {
  49. REFCOUNTED(AttributeAccessor)
  50. public:
  51. /// Get the attribute.
  52. virtual void Get(const Serializable* ptr, Variant& dest) const = 0;
  53. /// Set the attribute.
  54. virtual void Set(Serializable* ptr, const Variant& src) = 0;
  55. };
  56. /// Description of an automatically serializable variable.
  57. struct AttributeInfo
  58. {
  59. /// Construct empty.
  60. AttributeInfo() :
  61. type_(VAR_NONE),
  62. offset_(0),
  63. enumNames_(0),
  64. mode_(AM_DEFAULT),
  65. ptr_(0)
  66. {
  67. }
  68. /// Construct offset attribute.
  69. AttributeInfo(VariantType type, const char* name, size_t offset, const Variant& defaultValue, unsigned mode) :
  70. type_(type),
  71. name_(name),
  72. offset_((unsigned)offset),
  73. enumNames_(0),
  74. defaultValue_(defaultValue),
  75. mode_(mode),
  76. ptr_(0)
  77. {
  78. }
  79. /// Construct offset enum attribute.
  80. AttributeInfo(const char* name, size_t offset, const char** enumNames, const Variant& defaultValue, unsigned mode) :
  81. type_(VAR_INT),
  82. name_(name),
  83. offset_((unsigned)offset),
  84. enumNames_(enumNames),
  85. defaultValue_(defaultValue),
  86. mode_(mode),
  87. ptr_(0)
  88. {
  89. }
  90. /// Construct accessor attribute.
  91. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  92. type_(type),
  93. name_(name),
  94. offset_(0),
  95. enumNames_(0),
  96. accessor_(accessor),
  97. defaultValue_(defaultValue),
  98. mode_(mode),
  99. ptr_(0)
  100. {
  101. }
  102. /// Construct accessor enum attribute.
  103. AttributeInfo(const char* name, AttributeAccessor* accessor, const char** enumNames, const Variant& defaultValue,
  104. unsigned mode) :
  105. type_(VAR_INT),
  106. name_(name),
  107. offset_(0),
  108. enumNames_(enumNames),
  109. accessor_(accessor),
  110. defaultValue_(defaultValue),
  111. mode_(mode),
  112. ptr_(0)
  113. {
  114. }
  115. /// Attribute type.
  116. VariantType type_;
  117. /// Name.
  118. String name_;
  119. /// Byte offset from start of object.
  120. unsigned offset_;
  121. /// Enum names.
  122. const char** enumNames_;
  123. /// Helper object for accessor mode.
  124. SharedPtr<AttributeAccessor> accessor_;
  125. /// Default value for network replication.
  126. Variant defaultValue_;
  127. /// Attribute mode: whether to use for serialization, network replication, or both.
  128. unsigned mode_;
  129. /// Attribute data pointer if elsewhere than in the Serializable.
  130. void* ptr_;
  131. };
  132. }