Attribute.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Urho3D
  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 URHO3D_API AttributeAccessor : public RefCounted
  50. {
  51. public:
  52. /// Get the attribute.
  53. virtual void Get(const Serializable* ptr, Variant& dest) const = 0;
  54. /// Set the attribute.
  55. virtual void Set(Serializable* ptr, const Variant& src) = 0;
  56. };
  57. /// Description of an automatically serializable variable.
  58. struct AttributeInfo
  59. {
  60. /// Construct empty.
  61. AttributeInfo() { }
  62. /// Construct attribute.
  63. AttributeInfo(VariantType type, const char* name, SharedPtr<AttributeAccessor> accessor, const char** enumNames, const Variant& defaultValue, unsigned mode) :
  64. type_(type),
  65. name_(name),
  66. enumNames_(enumNames),
  67. accessor_(accessor),
  68. defaultValue_(defaultValue),
  69. mode_(mode)
  70. {
  71. }
  72. /// Get attribute metadata.
  73. const Variant& GetMetadata(const StringHash& key) const
  74. {
  75. auto elem = metadata_.Find(key);
  76. return elem != metadata_.End() ? elem->second_ : Variant::EMPTY;
  77. }
  78. /// Get attribute metadata of specified type.
  79. template <class T> T GetMetadata(const StringHash& key) const
  80. {
  81. return GetMetadata(key).Get<T>();
  82. }
  83. /// Attribute type.
  84. VariantType type_ = VAR_NONE;
  85. /// Name.
  86. String name_;
  87. /// Enum names.
  88. const char** enumNames_ = nullptr;
  89. /// Helper object for accessor mode.
  90. SharedPtr<AttributeAccessor> accessor_;
  91. /// Default value for network replication.
  92. Variant defaultValue_;
  93. /// Attribute mode: whether to use for serialization, network replication, or both.
  94. unsigned mode_ = AM_DEFAULT;
  95. /// Attribute metadata.
  96. VariantMap metadata_;
  97. /// Attribute data pointer if elsewhere than in the Serializable.
  98. void* ptr_ = nullptr;
  99. };
  100. /// Attribute handle returned by Context::RegisterAttribute and used to chain attribute setup calls.
  101. struct AttributeHandle
  102. {
  103. friend class Context;
  104. private:
  105. /// Construct default.
  106. AttributeHandle() = default;
  107. /// Construct from another handle.
  108. AttributeHandle(const AttributeHandle& another) = default;
  109. /// Attribute info.
  110. AttributeInfo* attributeInfo_ = nullptr;
  111. /// Network attribute info.
  112. AttributeInfo* networkAttributeInfo_ = nullptr;
  113. public:
  114. /// Set metadata.
  115. AttributeHandle& SetMetadata(StringHash key, const Variant& value)
  116. {
  117. if (attributeInfo_)
  118. attributeInfo_->metadata_[key] = value;
  119. if (networkAttributeInfo_)
  120. networkAttributeInfo_->metadata_[key] = value;
  121. return *this;
  122. }
  123. };
  124. }