WSF
WsfPProxyStructType.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 VIPROXYSTRUCTTYPE_HPP
16#define VIPROXYSTRUCTTYPE_HPP
17
18#include <cassert>
19#include <functional>
20#include <map>
21#include <string.h>
22#include <vector>
23
24#include "UtCast.hpp"
25#include "UtMemory.hpp"
27#include "WsfPProxyType.hpp"
28#include "WsfParseRule.hpp"
29class ProxyWriter;
30
31// A struct type is a proxy type composed of many member proxy types.
32class WsfPProxyStructType : public WsfPProxyType
33{
34public:
35 struct AttrData
36 {
37 AttrData(WsfPProxyType* aTypePtr = nullptr)
38 : mTypePtr(aTypePtr)
39 , mOffset(ut::npos)
40 {
41 }
43 size_t mOffset;
44 };
45
46 class Builder;
47
48 ~WsfPProxyStructType() override;
49 // returns true if the struct instance is in the unset state
50 bool IsUnset(void* aValuePtr) const override;
51 // Sets the struct instance to the 'unset' state
52 void SetUnset(void* aValuePtr) const override
53 {
56 }
57 bool IsInherited(void* aValuePtr) const override
58 {
61 }
62 void SetInherited(void* aValuePtr, bool aIsInherited) const override
63 {
65 if (aIsInherited)
66 {
68 }
69 else
70 {
71 hdr->mStructFlags &= ~WsfPProxyStructHeader::cSF_INHERITED;
72 }
73 }
74
75 bool IsOfBasicType(const std::string& aTypeName) const override
76 {
77 const WsfPProxyStructType* pStruct = this;
78 bool isTypeOf = false;
79
80 while ((pStruct != nullptr) && !isTypeOf)
81 {
82 isTypeOf = (aTypeName == pStruct->mTypeName);
83 pStruct = pStruct->mBasePtr;
84 }
85
86 return isTypeOf;
87 }
88
90
91 void ClearUnset(void* aValuePtr) const override;
92 WsfPProxyType* GetContainedType(size_t aIndex) const override
93 {
94 return (aIndex < mAttrData.size()) ? mAttrData[aIndex].mTypePtr : nullptr;
95 }
96 // Returns a member of a struct by index
97 WsfPProxyValue GetAtIndex(void* aDataPtr, size_t aIndex) const override { return GetMemberValue(aDataPtr, aIndex); }
98 size_t GetAttrCount(void* aPtr) const override;
99
100 void Initialize();
101
102 // Create a struct given a pre-allocated buffer
103 void Construct(void* aValuePtr) const override;
104
105 // Free the struct's memory
106 void Destroy(void* aValuePtr) const override;
107 // virtual WsfPProxyHash Hash(void* aDataPtr) const;
108 size_t MemoryUsage(void* aDataPtr) const override;
109
110private:
111 WsfPProxyStructType(WsfParseStruct* aParseStructPtr,
112 WsfPProxyStructType* aBasePtr,
113 const std::vector<std::string>& aNames,
114 const std::vector<AttrData>& aLocalAttributes,
115 const std::map<std::string, int>& aNameToIndexMap);
116
117 // Usually structs are copy-constructed from the default value
118 void CopyConstructInstance(WsfPProxyStructValue aInstance, WsfPProxyStructValue aSource, int copyFlags) const;
119
120 void ConstructInstance(WsfPProxyStructValue aInstance) const;
121
122 ptrdiff_t GetStructSize() const { return mStructSize; }
123
124 void ConstructP(WsfPProxyStructHeader* aHdr,
125 const WsfPProxyStructValue* aCopyFromPtr = nullptr,
126 int copyFlags = WsfProxy::cCOPY_ALL) const;
127
128 // Returns the number of attributes. To be used prior to initialization.
129 size_t InitGetAttrCount();
130
131 // Initialize attributes that are dynamic, and do not exist a c++ struct
132 void InitializeBase(WsfPProxyStructType* aBasePtr, size_t& aCurrentOffset, size_t& aAttributeIndex);
133
134public:
135 void ConstructAttr(const AttrData& aAttr, void* aValuePtr) const;
136 void DestroyInstance(WsfPProxyStructValue aInstance) const;
137
138 void CopyInstance(WsfPProxyStructValue& aDest, WsfPProxyStructValue& aSrc, int copyFlags) const;
140
141 void Copy(void* aDest, void* aSrc, int copyFlags) const override;
142 WsfPProxyValue GetMemberValue(void* aPtr, size_t aIndex) const;
143 WsfPProxyValue GetAttr(void* aPtr, const std::string& aName) const override;
144 bool SwapAttr(void* aPtr, const WsfPProxyKey& aKey, WsfPProxyValue& aValue) const override;
145
146 // Returns the total number of members
147 size_t GetMemberCount() const { return mAttrData.size(); }
148
149 // Returns the value name associated with the given index, returns an empty string if the index is out of bounds
150 std::string GetNameAtIndex(size_t aIndex) const override;
151 // Returns the index of the member with the name, -1 if none exists with that name
152 size_t GetMemberIndex(const std::string& aName) const override
153 {
154 auto i = mNameToIndex.find(aName);
155 if (i != mNameToIndex.end())
156 {
157 return ut::cast_to_size_t(i->second);
158 }
159 return ut::npos;
160 }
161
162 bool IsTypeOf(const WsfPProxyType* aOtherType) const override
163 {
164 if (this == aOtherType)
165 {
166 return true;
167 }
168 if (!mBasePtr)
169 {
170 return false;
171 }
172 return mBasePtr->IsTypeOf(aOtherType);
173 }
174
175 // Returns a struct instance filled with the default values
177
178 virtual bool IsInitialized() const { return mInitialized; }
179
181 ptrdiff_t mStructSize;
183 WsfPProxyStructType* mBasePtr;
185 // std::vector<WsfPProxyType*> mLocalAttributes;
186 std::vector<AttrData> mLocalAttributes;
187
190 std::vector<AttrData> mAttrData;
191
193 std::vector<size_t> mDynamicAttributes;
194
196 using NameToIndexMap = std::map<std::string, int>;
198 std::vector<std::string> mAllNames;
199
200
202
204 // If true, the struct's contents are always stored inline with the header. (WsfPProxyStructInlineHeader)
205 // If false, the struct's contents are always stored in another location. (WsfPProxyStructExpandedHeader)
208};
209
210class WSF_PARSER_EXPORT WsfPProxyStructType::Builder
211{
212public:
214 : mParseStructPtr(nullptr)
215 , mBasePtr(nullptr)
216 {
217 }
218
219 Builder& SetBaseStruct(WsfPProxyStructType* aBasePtr)
220 {
221 mBasePtr = aBasePtr;
222 if (mBasePtr)
223 {
224 mNameToIndex = mBasePtr->mNameToIndex;
225 mNames = mBasePtr->mAllNames;
226 }
227 return *this;
228 }
229
231 {
232 mParseStructPtr = aParseStruct;
233 return *this;
234 }
235
236 Builder& AddAttribute(const std::string& aName, WsfPProxyType* aType)
237 {
238 if (aType)
239 {
240 mNameToIndex[aName] = ut::cast_to_int(mNames.size());
241 mNames.push_back(aName);
242 mLocalAttributes.emplace_back(aType);
243 }
244 return *this;
245 }
246
247 std::unique_ptr<WsfPProxyStructType> build()
248 {
249 return std::unique_ptr<WsfPProxyStructType>(
250 new WsfPProxyStructType(mParseStructPtr, mBasePtr, mNames, mLocalAttributes, mNameToIndex));
251 }
252
253private:
255 WsfPProxyStructType* mBasePtr;
256 std::vector<std::string> mNames;
257 std::vector<AttrData> mLocalAttributes;
258 std::map<std::string, int> mNameToIndex;
259};
260
261#endif
Definition WsfPProxyKey.hpp:24
Definition WsfPProxyStructType.hpp:211
Builder & SetBaseStruct(WsfPProxyStructType *aBasePtr)
Definition WsfPProxyStructType.hpp:219
Builder & AddAttribute(const std::string &aName, WsfPProxyType *aType)
Definition WsfPProxyStructType.hpp:236
Builder & SetParseStruct(WsfParseStruct *aParseStruct)
Definition WsfPProxyStructType.hpp:230
std::unique_ptr< WsfPProxyStructType > build()
Definition WsfPProxyStructType.hpp:247
Builder()
Definition WsfPProxyStructType.hpp:213
Definition WsfPProxyStructType.hpp:33
std::vector< std::string > mAllNames
Definition WsfPProxyStructType.hpp:198
void ClearUnset(void *aValuePtr) const override
Definition WsfPProxyStructType.cpp:149
size_t MemoryUsage(void *aDataPtr) const override
Definition WsfPProxyStructType.cpp:104
~WsfPProxyStructType() override
Definition WsfPProxyStructType.cpp:36
bool IsInherited(void *aValuePtr) const override
Definition WsfPProxyStructType.hpp:57
bool mEmbedded
Definition WsfPProxyStructType.hpp:206
void DestroyInstance(WsfPProxyStructValue aInstance) const
Definition WsfPProxyStructType.cpp:228
WsfPProxyType * GetContainedType(size_t aIndex) const override
Definition WsfPProxyStructType.hpp:92
void SetInherited(void *aValuePtr, bool aIsInherited) const override
Definition WsfPProxyStructType.hpp:62
void Copy(void *aDest, void *aSrc, int copyFlags) const override
Definition WsfPProxyStructType.cpp:123
void Construct(void *aValuePtr) const override
Definition WsfPProxyStructType.cpp:41
void ConstructAttr(const AttrData &aAttr, void *aValuePtr) const
Definition WsfPProxyStructType.cpp:214
void SetUnset(void *aValuePtr) const override
Definition WsfPProxyStructType.hpp:52
std::vector< size_t > mDynamicAttributes
Indices of attributes that are not in the real struct.
Definition WsfPProxyStructType.hpp:193
WsfPProxyStructType * mBasePtr
Pointer to base struct.
Definition WsfPProxyStructType.hpp:183
void CopyInstance(WsfPProxyStructValue &aDest, WsfPProxyStructValue &aSrc, int copyFlags) const
Definition WsfPProxyStructType.cpp:245
bool IsTypeOf(const WsfPProxyType *aOtherType) const override
Definition WsfPProxyStructType.hpp:162
WsfPProxyStructValue GetInstance(WsfPProxyStructHeader *aHeader) const
Definition WsfPProxyStructType.hpp:139
void Destroy(void *aValuePtr) const override
Definition WsfPProxyStructType.cpp:141
void Initialize()
Definition WsfPProxyStructType.cpp:50
bool SwapAttr(void *aPtr, const WsfPProxyKey &aKey, WsfPProxyValue &aValue) const override
Definition WsfPProxyStructType.cpp:322
bool mInitialized
Definition WsfPProxyStructType.hpp:203
WsfPProxyValue GetAtIndex(void *aDataPtr, size_t aIndex) const override
Definition WsfPProxyStructType.hpp:97
size_t GetMemberIndex(const std::string &aName) const override
Definition WsfPProxyStructType.hpp:152
std::map< std::string, int > NameToIndexMap
Map an attribute name to the index.
Definition WsfPProxyStructType.hpp:196
WsfPProxyValue GetMemberValue(void *aPtr, size_t aIndex) const
Definition WsfPProxyStructType.cpp:269
std::string GetNameAtIndex(size_t aIndex) const override
Definition WsfPProxyStructType.cpp:352
WsfPProxyValue GetAttr(void *aPtr, const std::string &aName) const override
Definition WsfPProxyStructType.cpp:295
WsfParseStruct * mParseStructPtr
Definition WsfPProxyStructType.hpp:207
NameToIndexMap mNameToIndex
Definition WsfPProxyStructType.hpp:197
WsfPProxyStructValue mDefaultStruct
Definition WsfPProxyStructType.hpp:201
WsfPProxyStructValue GetDefaultValue() const
Definition WsfPProxyStructType.hpp:176
std::vector< AttrData > mAttrData
Definition WsfPProxyStructType.hpp:190
size_t GetMemberCount() const
Definition WsfPProxyStructType.hpp:147
bool IsOfBasicType(const std::string &aTypeName) const override
Definition WsfPProxyStructType.hpp:75
bool IsUnset(void *aValuePtr) const override
Definition WsfPProxyStructType.cpp:155
ptrdiff_t mStructSize
Size in bytes of the actual structure.
Definition WsfPProxyStructType.hpp:181
std::vector< AttrData > mLocalAttributes
Attributes for this struct, not including base classes.
Definition WsfPProxyStructType.hpp:186
virtual bool IsInitialized() const
Definition WsfPProxyStructType.hpp:178
size_t GetAttrCount(void *aPtr) const override
Definition WsfPProxyStructType.cpp:290
WsfParseStruct * GetParseStruct()
Definition WsfPProxyStructType.hpp:89
Definition WsfPProxyStructValue.hpp:23
WsfPProxyType(WsfProxy::ValueKind aKind)
Definition WsfPProxyType.hpp:39
std::string mTypeName
Definition WsfPProxyType.hpp:59
Definition WsfPProxyValue.hpp:33
Definition WsfParseRule.hpp:527
@ cCOPY_ALL
Definition WsfPProxyCommon.hpp:104
Definition WsfSensorErrorModel.hpp:21
Definition WsfPProxyStructHeader.hpp:21
@ cSF_INHERITED
Definition WsfPProxyStructHeader.hpp:29
@ cSF_UNSET
Definition WsfPProxyStructHeader.hpp:28
int mStructFlags
Definition WsfPProxyStructHeader.hpp:36
Definition WsfPProxyStructType.hpp:36
size_t mOffset
Definition WsfPProxyStructType.hpp:43
WsfPProxyType * mTypePtr
Definition WsfPProxyStructType.hpp:42
AttrData(WsfPProxyType *aTypePtr=nullptr)
Definition WsfPProxyStructType.hpp:37
Copyrights Multiple, All Rights Reserved