PListFile.h 4.7 KB

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