Attribute.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 AttributeAccessor : public RefCounted
  46. {
  47. public:
  48. /// Get the attribute.
  49. virtual void Get(Serializable* ptr, Variant& dest) {}
  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. name_(0),
  60. offset_(0),
  61. enumNames_(0),
  62. mode_(AM_DEFAULT),
  63. ptr_(0)
  64. {
  65. }
  66. /// Construct offset attribute.
  67. AttributeInfo(VariantType type, const char* name, unsigned offset, const Variant& defaultValue, unsigned mode) :
  68. type_(type),
  69. name_(name),
  70. offset_(offset),
  71. enumNames_(0),
  72. defaultValue_(defaultValue),
  73. mode_(mode),
  74. ptr_(0)
  75. {
  76. }
  77. /// Construct offset enum attribute.
  78. AttributeInfo(const char* name, unsigned offset, const char** enumNames, const Variant& defaultValue, unsigned mode) :
  79. type_(VAR_INT),
  80. name_(name),
  81. offset_(offset),
  82. enumNames_(enumNames),
  83. defaultValue_(defaultValue),
  84. mode_(mode),
  85. ptr_(0)
  86. {
  87. }
  88. /// Construct accessor attribute.
  89. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  90. type_(type),
  91. name_(name),
  92. offset_(0),
  93. enumNames_(0),
  94. accessor_(accessor),
  95. defaultValue_(defaultValue),
  96. mode_(mode),
  97. ptr_(0)
  98. {
  99. }
  100. /// Construct accessor enum attribute.
  101. AttributeInfo(const char* name, AttributeAccessor* accessor, const char** enumNames, const Variant& defaultValue, unsigned mode) :
  102. type_(VAR_INT),
  103. name_(name),
  104. offset_(0),
  105. enumNames_(enumNames),
  106. accessor_(accessor),
  107. defaultValue_(defaultValue),
  108. mode_(mode),
  109. ptr_(0)
  110. {
  111. }
  112. /// Attribute type.
  113. VariantType type_;
  114. /// Name.
  115. const char* name_;
  116. /// Byte offset from start of object.
  117. unsigned offset_;
  118. /// Enum names.
  119. const char** enumNames_;
  120. /// Helper object for accessor mode.
  121. SharedPtr<AttributeAccessor> accessor_;
  122. /// Default value for network replication.
  123. Variant defaultValue_;
  124. /// Attribute mode: whether to use for serialization, network replication, or both.
  125. unsigned mode_;
  126. /// Attribute data pointer if elsewhere than in the Serializable.
  127. void* ptr_;
  128. };
  129. }