PListFile.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2015 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 value map.
  96. const PListValueMap& GetValueMap() const;
  97. /// Return value vector.
  98. const PListValueVector& GetValueVector() const;
  99. /// Convert to value map (internal use only).
  100. PListValueMap& ConvertToValueMap();
  101. /// Convert to value vector (internal use only).
  102. PListValueVector& ConvertToValueVector();
  103. private:
  104. /// Reset.
  105. void Reset();
  106. /// Type.
  107. PListValueType type_;
  108. /// Values.
  109. union
  110. {
  111. int int_;
  112. bool bool_;
  113. float float_;
  114. String* string_;
  115. PListValueMap* valueMap_;
  116. PListValueVector* valueVector_;
  117. };
  118. };
  119. /// Property list (plist).
  120. class ATOMIC_API PListFile : public Resource
  121. {
  122. OBJECT(PListFile);
  123. public:
  124. /// Construct.
  125. PListFile(Context* context);
  126. /// Destruct.
  127. virtual ~PListFile();
  128. /// Register object factory.
  129. static void RegisterObject(Context* context);
  130. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  131. virtual bool BeginLoad(Deserializer& source);
  132. /// Return root.
  133. const PListValueMap& GetRoot() const { return root_; }
  134. private:
  135. /// Load dictionary.
  136. bool LoadDict(PListValueMap& dict, const XMLElement& dictElem);
  137. /// Load array.
  138. bool LoadArray(PListValueVector& array, const XMLElement& arrayElem);
  139. /// Load value.
  140. bool LoadValue(PListValue& value, const XMLElement& valueElem);
  141. /// Root dictionary.
  142. PListValueMap root_;
  143. };
  144. }