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