PListFile.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright (c) 2008-2017 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 "../Resource/Resource.h"
  24. namespace Atomic
  25. {
  26. class XMLElement;
  27. /// PList value types.
  28. enum PListValueType
  29. {
  30. PLVT_NONE = 0,
  31. PLVT_INT,
  32. PLVT_BOOL,
  33. PLVT_FLOAT,
  34. PLVT_STRING,
  35. PLVT_VALUEMAP,
  36. PLVT_VALUEVECTOR,
  37. };
  38. class PListValue;
  39. /// PList value map.
  40. typedef HashMap<String, PListValue> PListValueMap;
  41. /// Vector of PList value.
  42. typedef Vector<PListValue> PListValueVector;
  43. /// PList value.
  44. class ATOMIC_API PListValue
  45. {
  46. public:
  47. /// Construct.
  48. PListValue();
  49. /// Construct from int.
  50. PListValue(int value);
  51. /// Construct from boolean.
  52. PListValue(bool value);
  53. /// Construct from float.
  54. PListValue(float value);
  55. /// Construct from string.
  56. PListValue(const String& value);
  57. /// Construct from value map.
  58. PListValue(PListValueMap& valueMap);
  59. /// Construct from value vector.
  60. PListValue(PListValueVector& valueVector);
  61. /// Construct from another value.
  62. PListValue(const PListValue& value);
  63. /// Destruct.
  64. ~PListValue();
  65. /// Assign operator.
  66. PListValue& operator =(const PListValue& rhs);
  67. /// Return true if is valid.
  68. operator bool() const { return type_ != PLVT_NONE; }
  69. /// Set int.
  70. void SetInt(int value);
  71. /// Set boolean.
  72. void SetBool(bool value);
  73. /// Set float.
  74. void SetFloat(float value);
  75. /// Set string.
  76. void SetString(const String& value);
  77. /// Set value map.
  78. void SetValueMap(const PListValueMap& valueMap);
  79. /// Set value vector.
  80. void SetValueVector(const PListValueVector& valueVector);
  81. /// Return type.
  82. PListValueType GetType() const { return type_; }
  83. /// Return int.
  84. int GetInt() const;
  85. /// Return boolean.
  86. bool GetBool() const;
  87. /// Return float.
  88. float GetFloat() const;
  89. /// Return string.
  90. const String& GetString() const;
  91. /// Return IntRect, for string type.
  92. IntRect GetIntRect() const;
  93. /// Return IntVector2, for string type.
  94. IntVector2 GetIntVector2() const;
  95. /// Return IntVector3, for string type.
  96. IntVector3 GetIntVector3() const;
  97. /// Return value map.
  98. const PListValueMap& GetValueMap() const;
  99. /// Return value vector.
  100. const PListValueVector& GetValueVector() const;
  101. /// Convert to value map (internal use only).
  102. PListValueMap& ConvertToValueMap();
  103. /// Convert to value vector (internal use only).
  104. PListValueVector& ConvertToValueVector();
  105. private:
  106. /// Reset.
  107. void Reset();
  108. /// Type.
  109. PListValueType type_;
  110. /// Values.
  111. union
  112. {
  113. int int_;
  114. bool bool_;
  115. float float_;
  116. String* string_;
  117. PListValueMap* valueMap_;
  118. PListValueVector* valueVector_;
  119. };
  120. };
  121. /// Property list (plist).
  122. class ATOMIC_API PListFile : public Resource
  123. {
  124. ATOMIC_OBJECT(PListFile, Resource);
  125. public:
  126. /// Construct.
  127. PListFile(Context* context);
  128. /// Destruct.
  129. virtual ~PListFile();
  130. /// Register object factory.
  131. static void RegisterObject(Context* context);
  132. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  133. virtual bool BeginLoad(Deserializer& source);
  134. /// Return root.
  135. const PListValueMap& GetRoot() const { return root_; }
  136. private:
  137. /// Load dictionary.
  138. bool LoadDict(PListValueMap& dict, const XMLElement& dictElem);
  139. /// Load array.
  140. bool LoadArray(PListValueVector& array, const XMLElement& arrayElem);
  141. /// Load value.
  142. bool LoadValue(PListValue& value, const XMLElement& valueElem);
  143. /// Root dictionary.
  144. PListValueMap root_;
  145. };
  146. }