Attribute.h 3.7 KB

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