PListFile.h 3.7 KB

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