Attribute.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Copyright (c) 2008-2020 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. /// \file
  23. #pragma once
  24. #include "../Container/FlagSet.h"
  25. #include "../Container/Ptr.h"
  26. #include "../Core/Variant.h"
  27. namespace Urho3D
  28. {
  29. enum AttributeMode
  30. {
  31. /// Attribute shown only in the editor, but not serialized.
  32. AM_EDIT = 0x0,
  33. /// Attribute used for file serialization.
  34. AM_FILE = 0x1,
  35. /// Attribute used for network replication.
  36. AM_NET = 0x2,
  37. /// Attribute used for both file serialization and network replication (default).
  38. AM_DEFAULT = 0x3,
  39. /// Attribute should use latest data grouping instead of delta update in network replication.
  40. AM_LATESTDATA = 0x4,
  41. /// Attribute should not be shown in the editor.
  42. AM_NOEDIT = 0x8,
  43. /// Attribute is a node ID and may need rewriting.
  44. AM_NODEID = 0x10,
  45. /// Attribute is a component ID and may need rewriting.
  46. AM_COMPONENTID = 0x20,
  47. /// Attribute is a node ID vector where first element is the amount of nodes.
  48. AM_NODEIDVECTOR = 0x40,
  49. /// Attribute is readonly. Can't be used with binary serialized objects.
  50. AM_FILEREADONLY = 0x81,
  51. };
  52. URHO3D_FLAGSET(AttributeMode, AttributeModeFlags);
  53. class Serializable;
  54. /// Abstract base class for invoking attribute accessors.
  55. class URHO3D_API AttributeAccessor : public RefCounted
  56. {
  57. public:
  58. /// Get the attribute.
  59. virtual void Get(const Serializable* ptr, Variant& dest) const = 0;
  60. /// Set the attribute.
  61. virtual void Set(Serializable* ptr, const Variant& src) = 0;
  62. };
  63. /// Description of an automatically serializable variable.
  64. struct AttributeInfo
  65. {
  66. /// Construct empty.
  67. AttributeInfo() = default;
  68. /// Construct attribute.
  69. AttributeInfo(VariantType type, const char* name, const SharedPtr<AttributeAccessor>& accessor, const char** enumNames, const Variant& defaultValue, AttributeModeFlags mode) :
  70. type_(type),
  71. name_(name),
  72. enumNames_(enumNames),
  73. accessor_(accessor),
  74. defaultValue_(defaultValue),
  75. mode_(mode)
  76. {
  77. }
  78. /// Get attribute metadata.
  79. const Variant& GetMetadata(const StringHash& key) const
  80. {
  81. auto elem = metadata_.Find(key);
  82. return elem != metadata_.End() ? elem->second_ : Variant::EMPTY;
  83. }
  84. /// Get attribute metadata of specified type.
  85. template <class T> T GetMetadata(const StringHash& key) const
  86. {
  87. return GetMetadata(key).Get<T>();
  88. }
  89. /// Attribute type.
  90. VariantType type_ = VAR_NONE;
  91. /// Name.
  92. String name_;
  93. /// Enum names.
  94. const char** enumNames_ = nullptr;
  95. /// Helper object for accessor mode.
  96. SharedPtr<AttributeAccessor> accessor_;
  97. /// Default value for network replication.
  98. Variant defaultValue_;
  99. /// Attribute mode: whether to use for serialization, network replication, or both.
  100. AttributeModeFlags mode_ = AM_DEFAULT;
  101. /// Attribute metadata.
  102. VariantMap metadata_;
  103. /// Attribute data pointer if elsewhere than in the Serializable.
  104. void* ptr_ = nullptr;
  105. };
  106. /// Attribute handle returned by Context::RegisterAttribute and used to chain attribute setup calls.
  107. /// @nobind
  108. struct AttributeHandle
  109. {
  110. friend class Context;
  111. private:
  112. /// Construct default.
  113. AttributeHandle() = default;
  114. /// Construct from another handle.
  115. AttributeHandle(const AttributeHandle& another) = default;
  116. /// Attribute info.
  117. AttributeInfo* attributeInfo_ = nullptr;
  118. /// Network attribute info.
  119. AttributeInfo* networkAttributeInfo_ = nullptr;
  120. public:
  121. /// Set metadata.
  122. AttributeHandle& SetMetadata(StringHash key, const Variant& value)
  123. {
  124. if (attributeInfo_)
  125. attributeInfo_->metadata_[key] = value;
  126. if (networkAttributeInfo_)
  127. networkAttributeInfo_->metadata_[key] = value;
  128. return *this;
  129. }
  130. };
  131. }