Attribute.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 used for file serialization.
  27. static const unsigned AM_FILE = 0x1;
  28. /// Attribute used for network replication.
  29. static const unsigned AM_NET = 0x2;
  30. /// Attribute used for both file serialization and network replication (default.)
  31. static const unsigned AM_DEFAULT = 0x3;
  32. /// Attribute should use latest data grouping instead of delta update in network replication.
  33. static const unsigned AM_LATESTDATA = 0x4;
  34. /// Attribute should not be shown in the editor.
  35. static const unsigned AM_NOEDIT = 0x8;
  36. class Serializable;
  37. /// Internal helper class for invoking attribute accessors.
  38. class AttributeAccessor : public RefCounted
  39. {
  40. public:
  41. /// Get the attribute.
  42. virtual void Get(Serializable* ptr, Variant& dest) {}
  43. /// %Set the attribute.
  44. virtual void Set(Serializable* ptr, const Variant& src) {}
  45. };
  46. /// Description of an automatically serializable variable.
  47. struct AttributeInfo
  48. {
  49. /// Construct empty.
  50. AttributeInfo() :
  51. type_(VAR_NONE),
  52. offset_(0),
  53. enumNames_(0),
  54. mode_(AM_DEFAULT)
  55. {
  56. }
  57. /// Construct offset attribute.
  58. AttributeInfo(VariantType type, const char* name, unsigned offset, const Variant& defaultValue, unsigned mode) :
  59. type_(type),
  60. name_(name),
  61. offset_(offset),
  62. enumNames_(0),
  63. defaultValue_(defaultValue),
  64. mode_(mode)
  65. {
  66. }
  67. /// Construct offset enum attribute.
  68. AttributeInfo(const char* name, unsigned offset, const String* enumNames, const Variant& defaultValue, unsigned mode) :
  69. type_(VAR_INT),
  70. name_(name),
  71. offset_(offset),
  72. enumNames_(enumNames),
  73. defaultValue_(defaultValue),
  74. mode_(mode)
  75. {
  76. }
  77. /// Construct accessor attribute.
  78. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  79. type_(type),
  80. name_(name),
  81. offset_(0),
  82. enumNames_(0),
  83. accessor_(accessor),
  84. defaultValue_(defaultValue),
  85. mode_(mode)
  86. {
  87. }
  88. /// Construct accessor enum attribute.
  89. AttributeInfo(const char* name, AttributeAccessor* accessor, const String* enumNames, const Variant& defaultValue, unsigned mode) :
  90. type_(VAR_INT),
  91. name_(name),
  92. offset_(0),
  93. enumNames_(enumNames),
  94. accessor_(accessor),
  95. defaultValue_(defaultValue),
  96. mode_(mode)
  97. {
  98. }
  99. /// Attribute type.
  100. VariantType type_;
  101. /// Name.
  102. String name_;
  103. /// Byte offset from start of object.
  104. unsigned offset_;
  105. /// Enum string names.
  106. const String* enumNames_;
  107. /// Helper object for accessor mode.
  108. SharedPtr<AttributeAccessor> accessor_;
  109. /// Default value for network replication.
  110. Variant defaultValue_;
  111. /// Attribute mode: whether to use for serialization, network replication, or both.
  112. unsigned mode_;
  113. };