PListFile.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2014 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.h"
  24. namespace Urho3D
  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. class PListValueMap : public HashMap<String, PListValue>
  40. {
  41. public:
  42. PListValue& operator [](const String& key);
  43. const PListValue& operator [](const String& key) const;
  44. };
  45. typedef Vector<PListValue> PListValueVector;
  46. class URHO3D_API PListValue
  47. {
  48. public:
  49. // Construct.
  50. PListValue();
  51. // Construct from int.
  52. PListValue(int value);
  53. // Construct from boolean.
  54. PListValue(bool value);
  55. // Construct from float.
  56. PListValue(float value);
  57. // Construct from string.
  58. PListValue(const String& value);
  59. // Construct from value map.
  60. PListValue(PListValueMap& valueMap);
  61. // Construct from value vector.
  62. 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. 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 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 URHO3D_API PListFile : public Resource
  123. {
  124. OBJECT(PListFile);
  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, XMLElement valueElem);
  143. /// Root dictionary.
  144. PListValueMap root_;
  145. };
  146. }