WSF
Utml.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11// ****************************************************************************
12// Updated by Infoscitex, a DCS Company.
13// ****************************************************************************
14
15#ifndef UTQML_HPP
16#define UTQML_HPP
17#include <cassert>
18#include <exception>
19#include <iosfwd>
20#include <map>
21#include <string>
22#include <vector>
23
24#include "WsfUtExport.hpp"
25
26
27class UtTextDocument;
28
29/* 'Utml' is a simple text file format and associated DOM which looks like a simplified QML
30 see http://en.wikipedia.org/wiki/QML
31 - There is no javascript
32 - There are no special value types
33 - There are no lists, aka, '[ ... ]'
34
35 Example:
36
37 // comments are like this
38 // Define an object of type 'Widget'
39 // Object types must begin with a capital letter
40 Widget {
41 // Any number of named properties can be set
42 // properties begin with a lower-case letter
43 id: foo
44 width: 5 meters
45 info: "arbitrary strings
46 can be inserted between
47 quotes. Embed quotes like ""this"" "
48 stuff: single line text can be used without quotes
49
50 // Objects may contain other objects, not connected to any property
51 Part {
52 id: part1
53 }
54 Part {
55 id: part2
56 }
57
58 object_property: Part {
59 // property values can be objects too
60 id: part3
61 }
62
63 }
64
65*/
66
67class UtmlParserP;
68
69class WSF_UT_EXPORT UtmlParseError : public std::exception
70{
71public:
72 UtmlParseError(const std::string& aError);
73 ~UtmlParseError() override;
74 const char* what() const noexcept override;
75
76 std::string mError;
77};
78
79// An object in a UTML document, or a simple value.
80// if IsContainer() is true, this represents an object with this syntax:
81// <TypeName> {
82// property_name: property value
83// <...sub-objects and properties...>
84// }
85// if IsContainer() is false, this represents a single property value
87{
88public:
89 typedef std::map<std::string, UtmlObject> PropertyMap;
90
92 : mContainerData(nullptr)
93 {
94 }
95 UtmlObject(const std::string& aValue);
96 UtmlObject(const UtmlObject& aRhs);
97 UtmlObject& operator=(const UtmlObject& aRhs);
98
100 static UtmlObject makeContainer(const std::string& aType);
102 static UtmlObject parse(const std::string& aContainerType, const std::string& aText);
103
105 static UtmlObject parseFile(const std::string& aFilePath);
106
107 ~UtmlObject();
108
111 bool IsValue() const { return mContainerData == nullptr; }
114 bool IsContainer() const { return mContainerData != nullptr; }
115
117 const std::string& GetValue() const
118 {
119 assert(!IsContainer());
120 return mText;
121 }
122
124 void SetValue(const std::string& aValue)
125 {
126 assert(!IsContainer());
127 mText = aValue;
128 }
129
133 void SetValueLines(const std::vector<std::string>& aLines);
134
136 std::vector<std::string> GetValueLines() const;
137
138 bool GetBoolValue() const;
139
140 void SetBoolValue(bool aValue);
141
143
144
149
151 const std::string& GetType() const
152 {
153 assert(IsContainer());
154 return mText;
155 }
156
158 std::map<std::string, UtmlObject>& GetProperties()
159 {
160 assert(mContainerData);
161 return mContainerData->mProperties;
162 }
163
165 const std::map<std::string, UtmlObject>& GetProperties() const
166 {
167 assert(mContainerData);
168 return mContainerData->mProperties;
169 }
170
171 UtmlObject* FindProperty(const std::string& aName) const;
172
175 std::string PropertyValue(const std::string& aPropertyName, const std::string& aDefaultValue) const;
176
178 bool Contains(const std::string& aPropertyName) const;
179
182 UtmlObject& operator[](const std::string& aName);
183
186 {
187 assert(IsContainer());
188 mContainerData->mObjects.push_back(new UtmlObject(aRhs));
189 return *this;
190 }
191
193 int GetObjectCount() const { return mContainerData ? (int)mContainerData->mObjects.size() : 0; }
194#undef GetObject // Avoid conflict with Windows macro
196 UtmlObject& GetObject(int aIndex)
197 {
198 assert(GetObjectCount() > aIndex && aIndex >= 0);
199 return *mContainerData->mObjects[aIndex];
200 }
201
202 const UtmlObject& GetObject(int aIndex) const
203 {
204 assert(GetObjectCount() > aIndex && aIndex >= 0);
205 return *mContainerData->mObjects[aIndex];
206 }
207
210 UtmlObject* FirstObjectByType(const std::string& aTypeName) const;
211
213 void GiveProperty(const std::string& aName, UtmlObject& aObject);
215 void GiveObject(UtmlObject& aObject);
216
218
220 void Swap(UtmlObject& aRhs)
221 {
222 std::swap(mText, aRhs.mText);
223 std::swap(mContainerData, aRhs.mContainerData);
224 }
225
226 std::string ToText() const;
227
228 bool operator==(const UtmlObject& aRhs) const;
229
230 bool operator!=(const UtmlObject& aRhs) const { return !this->operator==(aRhs); }
231
232private:
233 std::vector<UtmlObject*>& GetObjects() { return mContainerData->mObjects; }
234
235 struct ContainerData
236 {
237 ContainerData();
238 ~ContainerData();
239 ContainerData(const ContainerData& aSrc);
240
241 bool operator==(const ContainerData& aRhs) const
242 {
243 if (this == &aRhs)
244 {
245 return true;
246 }
247 if (mObjects.size() != aRhs.mObjects.size())
248 {
249 return false;
250 }
251 if (mProperties != aRhs.mProperties)
252 {
253 return false;
254 }
255 for (size_t i = 0; i < mObjects.size(); ++i)
256 {
257 if (*mObjects[i] != *aRhs.mObjects[i])
258 {
259 return false;
260 }
261 }
262 return true;
263 }
264
265 bool operator!=(const ContainerData& aRhs) const { return !this->operator==(aRhs); }
266
267 std::map<std::string, UtmlObject> mProperties;
268 std::vector<UtmlObject*> mObjects;
269
270 private:
271 ContainerData& operator=(const ContainerData&); // no imp
272 };
273 std::string mText; // value or type
274 ContainerData* mContainerData;
275};
276
277
280{
281public:
282 UtmlParser(UtTextDocument& aDocument);
283 ~UtmlParser();
284 bool Parse(UtmlObject& aContainer);
285
286private:
287 UtmlParser& operator=(const UtmlParser&); // not implemented
288 UtmlParserP* mImp;
289 UtTextDocument* mDocumentPtr;
290};
291
294{
295public:
297 : mIndent(2)
298 {
299 }
300 void Write(const UtmlObject& aObject, std::ostream& aStream);
301 void WriteRoot(const UtmlObject& aObject, std::ostream& aStream);
302
303 void WriteFile(const UtmlObject& aObject, const std::string& aFileName, bool aObjectIsImplicitRoot);
305
306private:
307};
308
309#endif
bool operator!=(int aId, const WsfStringInt &aRhs)
Not equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:75
bool operator==(int aId, const WsfStringInt &aRhs)
Equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:69
#define WSF_UT_EXPORT
Definition WsfUtExport.hpp:32
bool Contains(std::vector< int > &aQueryTargetIds, const WsfTrack &aShooterTrack)
Definition WsfWeaponThreatProcessor.cpp:1462
Definition Utml.hpp:87
const UtmlObject & GetObject(int aIndex) const
returns the object at the specified index
Definition Utml.hpp:202
UtmlObject & operator+=(const UtmlObject &aRhs)
Add a sub-object to this object.
Definition Utml.hpp:185
bool IsValue() const
Definition Utml.hpp:111
int GetObjectCount() const
Returns the number of objects in the container.
Definition Utml.hpp:193
const std::map< std::string, UtmlObject > & GetProperties() const
Returns the list of properties on the object.
Definition Utml.hpp:165
std::map< std::string, UtmlObject > & GetProperties()
Returns the list of properties on the object.
Definition Utml.hpp:158
UtmlObject()
Definition Utml.hpp:91
const std::string & GetValue() const
To be used when IsContainer() is false, the value as a string.
Definition Utml.hpp:117
bool operator!=(const UtmlObject &aRhs) const
Definition Utml.hpp:230
void Swap(UtmlObject &aRhs)
}
Definition Utml.hpp:220
void SetValue(const std::string &aValue)
Assign the value. Not valid for containers.
Definition Utml.hpp:124
bool IsContainer() const
Definition Utml.hpp:114
std::map< std::string, UtmlObject > PropertyMap
Definition Utml.hpp:89
const std::string & GetType() const
}
Definition Utml.hpp:151
std::string mError
Definition Utml.hpp:76
UtmlParseError(const std::string &aError)
Definition Utml.cpp:664
const char * what() const noexcept override
Definition Utml.cpp:659
UtmlParser(UtTextDocument &aDocument)
Definition Utml.cpp:626
bool Parse(UtmlObject &aContainer)
Definition Utml.cpp:633
UtmlWriter()
Definition Utml.hpp:296
int mIndent
Definition Utml.hpp:304
void swap(WsfRoute &aLhs, WsfRoute &aRhs)
Definition WsfRoute.hpp:294
Copyrights Multiple, All Rights Reserved