PListFile.h 4.7 KB

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