Attribute.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Ptr.h"
  25. #include "Variant.h"
  26. /// Attribute shown only in the editor, but not serialized.
  27. static const unsigned AM_EDIT = 0x0;
  28. /// Attribute used for file serialization.
  29. static const unsigned AM_FILE = 0x1;
  30. /// Attribute used for network replication.
  31. static const unsigned AM_NET = 0x2;
  32. /// Attribute used for both file serialization and network replication (default.)
  33. static const unsigned AM_DEFAULT = 0x3;
  34. /// Attribute should use latest data grouping instead of delta update in network replication.
  35. static const unsigned AM_LATESTDATA = 0x4;
  36. /// Attribute should not be shown in the editor.
  37. static const unsigned AM_NOEDIT = 0x8;
  38. /// Attribute is a node ID and may need rewriting.
  39. static const unsigned AM_NODEID = 0x10;
  40. /// Attribute is a component ID and may need rewriting.
  41. static const unsigned AM_COMPONENTID = 0x20;
  42. class Serializable;
  43. /// Internal helper class for invoking attribute accessors.
  44. class AttributeAccessor : public RefCounted
  45. {
  46. public:
  47. /// Get the attribute.
  48. virtual void Get(Serializable* ptr, Variant& dest) {}
  49. /// %Set the attribute.
  50. virtual void Set(Serializable* ptr, const Variant& src) {}
  51. };
  52. /// Description of an automatically serializable variable.
  53. struct AttributeInfo
  54. {
  55. /// Construct empty.
  56. AttributeInfo() :
  57. type_(VAR_NONE),
  58. offset_(0),
  59. enumNames_(0),
  60. mode_(AM_DEFAULT)
  61. {
  62. }
  63. /// Construct offset attribute.
  64. AttributeInfo(VariantType type, const char* name, unsigned offset, const Variant& defaultValue, unsigned mode) :
  65. type_(type),
  66. name_(name),
  67. offset_(offset),
  68. enumNames_(0),
  69. defaultValue_(defaultValue),
  70. mode_(mode)
  71. {
  72. }
  73. /// Construct offset enum attribute.
  74. AttributeInfo(const char* name, unsigned offset, const String* enumNames, const Variant& defaultValue, unsigned mode) :
  75. type_(VAR_INT),
  76. name_(name),
  77. offset_(offset),
  78. enumNames_(enumNames),
  79. defaultValue_(defaultValue),
  80. mode_(mode)
  81. {
  82. }
  83. /// Construct accessor attribute.
  84. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  85. type_(type),
  86. name_(name),
  87. offset_(0),
  88. enumNames_(0),
  89. accessor_(accessor),
  90. defaultValue_(defaultValue),
  91. mode_(mode)
  92. {
  93. }
  94. /// Construct accessor enum attribute.
  95. AttributeInfo(const char* name, AttributeAccessor* accessor, const String* enumNames, const Variant& defaultValue, unsigned mode) :
  96. type_(VAR_INT),
  97. name_(name),
  98. offset_(0),
  99. enumNames_(enumNames),
  100. accessor_(accessor),
  101. defaultValue_(defaultValue),
  102. mode_(mode)
  103. {
  104. }
  105. /// Attribute type.
  106. VariantType type_;
  107. /// Name.
  108. String name_;
  109. /// Byte offset from start of object.
  110. unsigned offset_;
  111. /// Enum string names.
  112. const String* enumNames_;
  113. /// Helper object for accessor mode.
  114. SharedPtr<AttributeAccessor> accessor_;
  115. /// Default value for network replication.
  116. Variant defaultValue_;
  117. /// Attribute mode: whether to use for serialization, network replication, or both.
  118. unsigned mode_;
  119. };