WSF
UtPackSchema.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 UTPACKSCHEMA_HPP
16#define UTPACKSCHEMA_HPP
17
18#include <exception>
19#include <map>
20#include <memory>
21#include <ostream>
22#include <string>
23#include <vector>
24
25#include "WsfUtExport.hpp"
26
27class UtmlObject;
28class UtPackNamespace;
29class UtPackType;
31class UtPackField;
32class UtPackStruct;
33class UtPackListType;
34class UtPackBasicType;
35
36// see UtPack.hpp
37//
38// This file contains classes related to UtPack's schema.
39//
40
41class UtPackSchema;
42
43
44class WSF_UT_EXPORT UtPackException : public std::exception
45{
46public:
47 UtPackException(const std::string& aReason);
48 ~UtPackException() override {}
49 const char* what() const noexcept override;
50 std::string mReason;
51};
52
54{
55public:
56 UtPackPrinter(std::ostream* aStream)
57 : mStreamPtr(aStream)
58 {
59 mIndent = 0;
60 }
61
62 void Begin(const std::string& aType)
63 {
64 WriteIndent();
65 (*mStreamPtr) << aType << " {\n";
66 mIndent += 2;
67 }
68
69 void End()
70 {
71 mIndent -= 2;
72 WriteIndent();
73 (*mStreamPtr) << "}\n";
74 }
75
76 void Field(const std::string& aName)
77 {
78 WriteIndent();
79 (*mStreamPtr) << aName << ": ";
80 }
81 void Value(const std::string& aValue) { (*mStreamPtr) << aValue << '\n'; }
82
83private:
84 void WriteIndent()
85 {
86 for (int i = 0; i < mIndent; ++i)
87 {
88 (*mStreamPtr) << ' ';
89 }
90 }
91 int mIndent;
92 std::ostream* mStreamPtr;
93};
94
95// Contains the schema read from the schema text definitions
97{
98public:
100
101 void Read(UtmlObject& aInput);
102 void Resolve();
103 static std::unique_ptr<UtPackType> ReadType(UtmlObject& aInput, UtPackType& aParentType);
104 UtPackType* FindType(const std::string& aTypeName);
105 const UtPackType* FindType(const std::string& aTypeName) const;
106
107 std::vector<std::string> GetTypeNames() const;
108 // Namespace name is the C++ namespace used when generating code
109 const std::string& GetNamespaceName() const { return mNamespaceName; }
110
111private:
112 std::shared_ptr<UtPackNamespace> mRoot;
113 std::string mNamespaceName;
114};
115
116// Base type for all schema types
118{
119public:
120 using NestedTypesList = std::vector<std::pair<std::string, std::unique_ptr<UtPackType>>>;
121
122 UtPackType(const UtPackType&) = delete;
123 UtPackType& operator=(const UtPackType&) = delete;
124
125 // Destructor must be made public to allow use with unique_ptr
126 virtual ~UtPackType() = default;
127
129 {
130 cENC_NONE, // don't know how to encode the type
131 cENC_LITTLE_ENDIAN, // little endian plain ol' data
132 cENC_VAR_BYTES, // size + byte array
133 cENC_BIT, // a bit inside a struct's bitset
134 cENC_STRUCT, // a struct
135 cENC_LIST // a list
136 };
137
138 const std::string& GetTypeName() const;
140 std::string GetTypePath() const;
141 UtPackType* FindType(const std::string& aTypeName);
142 const UtPackType* FindType(const std::string& aTypeName) const;
143 virtual void Resolve();
144 Encoding GetEncoding() const { return mEncoding; }
145 bool IsStruct() const;
146 bool IsUnion() const;
147 bool IsBasicType() const;
148 bool IsEnum() const;
149 bool IsList() const;
150 const NestedTypesList& GetNestedTypes() const { return mNestedTypes; }
151
152protected:
153 const UtPackType* FindTypeP(const std::string& aTypeName) const;
154 UtPackType(UtPackType* aParentTypePtr);
155 const UtPackType* GetRootType() const;
156 UtPackType* GetRootType();
157 UtPackSchema* GetSchema() const;
158
159 void Read(UtmlObject& aInput);
160 void ReadNestedTypes(UtmlObject& aInput);
161
164 std::string mTypeName;
166};
167
168// A reference to a type
170{
171public:
173 : mContextType(nullptr)
174 , mTypePtr(nullptr)
175 {
176 }
177 UtPackTypeReference(const std::string& aTypeName, UtPackType* aParentType);
178
179 void Resolve();
180 UtPackType* GetType() const { return mTypePtr; }
181
182 bool IsNull() const { return mTypeName.empty(); }
183
184private:
185 std::string mTypeName;
186 UtPackType* mContextType;
187 UtPackType* mTypePtr;
188};
189
190
192// BSD Type classes - Basic type schema
194
197{
198public:
200 : mIsObsolete(false)
201 , mOptionIndex(-1)
202 , mBitIndex(-1)
203 , mIsIndex(false)
204 , mIsTime(false)
205 {
206 }
207
208 bool Read(UtmlObject& aInput, UtPackType& aContainer);
209 UtPackTypeReference& GetType() { return mType; }
210 const UtPackTypeReference& GetType() const { return mType; }
211 std::string GetFieldName() const { return mName; }
212 bool IsOptional() const { return mOptionIndex != -1; }
213 std::string GetDefaultValue() const { return mDefaultValue; }
214 // Optional field bit number, -1 if not optional
215 int GetOptionIndex() const { return mOptionIndex; }
216 // For boolean values only, the bit used to store the value. -1 if none
217 int GetBitIndex() const { return mBitIndex; }
218 // Used for accessing AFSIM platform indices from structs.
219 bool IsIndex() const { return mIsIndex; }
220 // Used for accessing sim times from structs.
221 bool IsTime() const { return mIsTime; }
222
223 bool IsObsolete() const { return mIsObsolete; }
224
225private:
226 std::string mName;
228 bool mIsObsolete;
229 std::string mDefaultValue;
230 short mOptionIndex;
231 short mBitIndex;
232 bool mIsIndex;
233 bool mIsTime;
234};
235
238{
239public:
240 UtPackStruct(UtPackType& aParentType);
241 void Read(UtmlObject& aInput, UtPackType& aContainer);
242 void Resolve() override;
243 const std::vector<UtPackField>& GetFields() const { return mFields; }
244 std::vector<UtPackField> GetAllFields() const;
245
246 int GetBitsetSize() const { return mBitsetSize; }
247 int GetBitsetBytes() const { return (mBitsetSize + 7) / 8; }
249 int GetMessageId() const { return mMessageId; }
250 bool IsMessage() const { return mMessageId != -1; }
251 bool IsVirtual() const { return mMessageId == -2; }
252 const UtPackTypeReference& GetBaseType() const { return mBaseType; }
253
254private:
255 int mBitsetSize;
256 std::vector<UtPackField> mFields;
257 UtPackTypeReference mBaseType;
258 int mMessageId;
259};
260
263{
264public:
265 UtPackUnion(UtPackType& aParentType);
266 void Read(UtmlObject& aInput, UtPackType& aContainer);
267 void Resolve() override;
268 const std::vector<UtPackField>& GetFields() const { return mFields; }
269
270private:
271 std::vector<UtPackField> mFields;
272};
273
276{
277public:
279 : UtPackType(&aParent)
280 {
281 }
282 bool Read(UtmlObject& aInput);
283 void Resolve() override;
284
286};
287
290{
291public:
292 UtPackBasicType(UtPackType& aParent);
293 void Read(UtmlObject& aInput);
294 const std::string& GetCPP_Type() const;
295 int GetSize() const { return mSize; }
296
297private:
298 std::string mCPP_Type;
299 int mSize;
300};
301
304{
305public:
306 UtPackEnumType(UtPackType& aParent);
307 void Read(UtmlObject& aInput);
308 void Resolve() override;
309 std::string GetCPP_Type() const { return mCPP_Type; }
310 std::string GetUnderlyingType() const { return mUnderlyingType; }
311 const std::map<std::string, int>& GetEnumValues() const { return mEnumValues; }
312 const std::map<int, std::string>& GetValueToName() const { return mValueToName; }
313
314private:
315 std::map<std::string, int> mEnumValues;
316 std::map<int, std::string> mValueToName;
317 std::string mCPP_Type;
318 std::string mUnderlyingType;
319};
320
322{
323public:
325 : UtPackType(nullptr)
326 , mSchemaPtr(aSchemaPtr)
327 {
328 mTypeName = "";
329 }
330 void Read(UtmlObject& aInput);
331 UtPackSchema* GetSchema() const { return mSchemaPtr; }
332
333private:
334 UtPackSchema* mSchemaPtr;
335};
336
337#endif
#define WSF_UT_EXPORT
Definition WsfUtExport.hpp:32
A basic type defined in the schema.
Definition UtPackSchema.hpp:290
void Read(UtmlObject &aInput)
Definition UtPackSchema.cpp:382
int GetSize() const
Definition UtPackSchema.hpp:295
UtPackBasicType(UtPackType &aParent)
Definition UtPackSchema.cpp:482
const std::string & GetCPP_Type() const
Definition UtPackSchema.cpp:377
void Resolve() override
Definition UtPackSchema.cpp:453
std::string GetCPP_Type() const
Definition UtPackSchema.hpp:309
const std::map< std::string, int > & GetEnumValues() const
Definition UtPackSchema.hpp:311
UtPackEnumType(UtPackType &aParent)
Definition UtPackSchema.cpp:448
void Read(UtmlObject &aInput)
Definition UtPackSchema.cpp:409
const std::map< int, std::string > & GetValueToName() const
Definition UtPackSchema.hpp:312
std::string GetUnderlyingType() const
Definition UtPackSchema.hpp:310
std::string mReason
Definition UtPackSchema.hpp:50
~UtPackException() override
Definition UtPackSchema.hpp:48
UtPackException(const std::string &aReason)
Definition UtPackSchema.cpp:494
A field in a struct or union.
Definition UtPackSchema.hpp:197
UtPackField()
Definition UtPackSchema.hpp:199
bool IsOptional() const
Definition UtPackSchema.hpp:212
bool IsObsolete() const
Definition UtPackSchema.hpp:223
UtPackTypeReference & GetType()
Definition UtPackSchema.hpp:209
int GetOptionIndex() const
Definition UtPackSchema.hpp:215
int GetBitIndex() const
Definition UtPackSchema.hpp:217
bool IsIndex() const
Definition UtPackSchema.hpp:219
bool IsTime() const
Definition UtPackSchema.hpp:221
const UtPackTypeReference & GetType() const
Definition UtPackSchema.hpp:210
std::string GetDefaultValue() const
Definition UtPackSchema.hpp:213
std::string GetFieldName() const
Definition UtPackSchema.hpp:211
A list type defined in the schema.
Definition UtPackSchema.hpp:276
UtPackListType(UtPackType &aParent)
Definition UtPackSchema.hpp:278
UtPackTypeReference mContainedType
Definition UtPackSchema.hpp:285
Definition UtPackSchema.hpp:322
UtPackNamespace(UtPackSchema *aSchemaPtr)
Definition UtPackSchema.hpp:324
UtPackSchema * GetSchema() const
Definition UtPackSchema.hpp:331
void Begin(const std::string &aType)
Definition UtPackSchema.hpp:62
void Value(const std::string &aValue)
Definition UtPackSchema.hpp:81
void End()
Definition UtPackSchema.hpp:69
void Field(const std::string &aName)
Definition UtPackSchema.hpp:76
UtPackPrinter(std::ostream *aStream)
Definition UtPackSchema.hpp:56
Definition UtPackSchema.hpp:97
std::vector< std::string > GetTypeNames() const
Definition UtPackSchema.cpp:584
static std::unique_ptr< UtPackType > ReadType(UtmlObject &aInput, UtPackType &aParentType)
Definition UtPackSchema.cpp:504
void Resolve()
Definition UtPackSchema.cpp:558
UtPackSchema()
Definition UtPackSchema.cpp:540
UtPackType * FindType(const std::string &aTypeName)
Definition UtPackSchema.cpp:563
void Read(UtmlObject &aInput)
Definition UtPackSchema.cpp:545
const std::string & GetNamespaceName() const
Definition UtPackSchema.hpp:109
A struct is a set of fields.
Definition UtPackSchema.hpp:238
void Read(UtmlObject &aInput, UtPackType &aContainer)
Definition UtPackSchema.cpp:215
UtPackStruct(UtPackType &aParentType)
Definition UtPackSchema.cpp:254
void Resolve() override
Definition UtPackSchema.cpp:261
const UtPackTypeReference & GetBaseType() const
Definition UtPackSchema.hpp:252
int GetBitsetBytes() const
Definition UtPackSchema.hpp:247
bool IsVirtual() const
Definition UtPackSchema.hpp:251
const std::vector< UtPackField > & GetFields() const
Definition UtPackSchema.hpp:243
int GetMessageId() const
Returns -1 if struct is not a message.
Definition UtPackSchema.hpp:249
int GetBitsetSize() const
Definition UtPackSchema.hpp:246
bool IsMessage() const
Definition UtPackSchema.hpp:250
Definition UtPackSchema.hpp:170
UtPackTypeReference()
Definition UtPackSchema.hpp:172
bool IsNull() const
Definition UtPackSchema.hpp:182
UtPackType * GetType() const
Definition UtPackSchema.hpp:180
Definition UtPackSchema.hpp:118
Encoding mEncoding
Definition UtPackSchema.hpp:162
Encoding
Definition UtPackSchema.hpp:129
@ cENC_VAR_BYTES
Definition UtPackSchema.hpp:132
@ cENC_LITTLE_ENDIAN
Definition UtPackSchema.hpp:131
@ cENC_BIT
Definition UtPackSchema.hpp:133
@ cENC_STRUCT
Definition UtPackSchema.hpp:134
@ cENC_NONE
Definition UtPackSchema.hpp:130
@ cENC_LIST
Definition UtPackSchema.hpp:135
const NestedTypesList & GetNestedTypes() const
Definition UtPackSchema.hpp:150
Encoding GetEncoding() const
Definition UtPackSchema.hpp:144
UtPackType & operator=(const UtPackType &)=delete
std::string mTypeName
Definition UtPackSchema.hpp:164
UtPackType * mParentType
Definition UtPackSchema.hpp:163
virtual ~UtPackType()=default
std::vector< std::pair< std::string, std::unique_ptr< UtPackType > > > NestedTypesList
Definition UtPackSchema.hpp:120
NestedTypesList mNestedTypes
Definition UtPackSchema.hpp:165
UtPackType(const UtPackType &)=delete
const std::vector< UtPackField > & GetFields() const
Definition UtPackSchema.hpp:268
void Resolve() override
Definition UtPackSchema.cpp:343
UtPackUnion(UtPackType &aParentType)
Definition UtPackSchema.cpp:338
void Read(UtmlObject &aInput, UtPackType &aContainer)
Definition UtPackSchema.cpp:315
Definition Utml.hpp:87
Template specialization for wsf::comm::Address. Allows usage of Address in std::unordered_map/set.
Definition WsfCommAddress.hpp:185
Copyrights Multiple, All Rights Reserved