2
0

Attribute.h 4.6 KB

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