Attribute.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "SharedPtr.h"
  25. #include "Variant.h"
  26. /// Attribute used only for disk serialization
  27. static const unsigned AM_SERIALIZATION = 0x1;
  28. /// Attribute used only for network replication
  29. static const unsigned AM_NETWORK = 0x2;
  30. /// Attribute used for both (default)
  31. static const unsigned AM_BOTH = 0x3;
  32. class Serializable;
  33. /// Internal helper class for invoking attribute accessors
  34. class AttributeAccessor : public RefCounted
  35. {
  36. public:
  37. /// Get the attribute
  38. virtual Variant Get(Serializable* ptr) { return Variant(); }
  39. /// Set the attribute
  40. virtual void Set(Serializable* ptr, const Variant& value) {}
  41. };
  42. /// Description of an automatically serializable variable
  43. struct AttributeInfo
  44. {
  45. /// Construct empty
  46. AttributeInfo() :
  47. type_(VAR_NONE),
  48. offset_(0),
  49. enumNames_(0),
  50. mode_(AM_BOTH)
  51. {
  52. }
  53. /// Construct offset attribute
  54. AttributeInfo(VariantType type, const char* name, unsigned offset, const Variant& defaultValue, unsigned mode) :
  55. type_(type),
  56. name_(name),
  57. offset_(offset),
  58. enumNames_(0),
  59. defaultValue_(defaultValue),
  60. mode_(mode)
  61. {
  62. }
  63. /// Construct offset enum attribute
  64. AttributeInfo(VariantType type, const char* name, unsigned offset, const String* enumNames, const Variant& defaultValue, unsigned mode) :
  65. type_(type),
  66. name_(name),
  67. offset_(offset),
  68. enumNames_(enumNames),
  69. defaultValue_(defaultValue),
  70. mode_(mode)
  71. {
  72. }
  73. /// Construct accessor attribute
  74. AttributeInfo(VariantType type, const char* name, AttributeAccessor* accessor, const Variant& defaultValue, unsigned mode) :
  75. type_(type),
  76. name_(name),
  77. offset_(0),
  78. enumNames_(0),
  79. accessor_(accessor),
  80. defaultValue_(defaultValue),
  81. mode_(mode)
  82. {
  83. }
  84. /// Attribute type
  85. VariantType type_;
  86. /// Name
  87. String name_;
  88. /// Byte offset from start of object
  89. unsigned offset_;
  90. /// Enum string names
  91. const String* enumNames_;
  92. /// Helper object for accessor mode
  93. SharedPtr<AttributeAccessor> accessor_;
  94. /// Default value for network replication
  95. Variant defaultValue_;
  96. /// Attribute mode: whether to use for serialization, network replication, or both
  97. unsigned mode_;
  98. };