WSF
WsfObjectTypeList.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#ifndef WSFOBJECTTYPELIST_HPP
13#define WSFOBJECTTYPELIST_HPP
14
15#include "wsf_export.h"
16
17#include <algorithm>
18#include <cassert>
19#include <map>
20#include <memory>
21#include <set>
22#include <string>
23#include <vector>
24
25#include "WsfObject.hpp"
27#include "WsfStringId.hpp"
28#include "WsfTypes.hpp"
29
39
40template<class T>
42{
43public:
44 typedef T ObjectType;
45 typedef std::vector<T*> ObjectPtrList;
46
47protected:
48 template<typename DEFAULT_TYPE>
50 {
51 return new DEFAULT_TYPE;
52 }
53
55 {
56 WsfObject* objectPtr = new ObjectType();
57 return objectPtr;
58 }
59
60 template<typename FACTORY_LIST>
61 static WsfObject* FactoryCreateFunction(void* aListPtr, const std::string& aTypeName)
62 {
63 const FACTORY_LIST& aList = *((FACTORY_LIST*)aListPtr);
64 for (typename FACTORY_LIST::const_iterator i = aList.begin(); i != aList.end(); ++i)
65 {
66 WsfObject* newObjectPtr = (*i)(aTypeName);
67 if (newObjectPtr != nullptr)
68 {
69 return newObjectPtr;
70 }
71 }
72 return nullptr;
73 }
74
75public:
76 WsfObjectTypeList(WsfScenario& aScenario, unsigned int aFlags, const std::string& aBlockName)
77 : WsfObjectTypeListBase(aFlags, aBlockName, &aScenario)
78 {
79 }
80
81 WsfObjectTypeList(WsfScenario& aScenario, const std::string& aBlockName)
82 : WsfObjectTypeListBase(0, aBlockName, &aScenario)
83 {
84 }
85
91 bool Add(WsfStringId aId, std::unique_ptr<T> aDefinitionPtr)
92 {
93 static_assert(std::is_base_of<WsfObject, T>::value, "Add requires a WsfObject");
94 return AddP(aId, std::move(aDefinitionPtr));
95 }
96
102 bool AddCoreType(WsfStringId aId, std::unique_ptr<T> aDefinitionPtr)
103 {
104 static_assert(std::is_base_of<WsfObject, T>::value, "AddCoreType requires a WsfObject");
105 if (AddP(aId, std::move(aDefinitionPtr)))
106 {
107 mCoreTypes.insert(aId);
108 return true;
109 }
110 return false;
111 }
112
118 T* Clone(WsfStringId aId) const override
119 {
120 static_assert(std::is_base_of<WsfObject, ObjectType>::value, "Type does not support Clone()");
121 return static_cast<T*>(CloneP(aId));
122 }
123
126
127 T* Find(WsfStringId aId) const override { return static_cast<ObjectType*>(FindP(aId)); }
128
131 {
132 ObjectPtrList typePtrVec;
133 for (const auto& typeEntry : mCoreTypes)
134 {
135 auto it = mTypeMap.find(typeEntry);
136 if (it != std::end(mTypeMap))
137 {
138 typePtrVec.push_back(static_cast<ObjectType*>(it->second.get()));
139 }
140 }
141
142 return typePtrVec;
143 }
144
145 void GetCurrentTypes(ObjectPtrList& aListOfPtrs) const
146 {
147 aListOfPtrs.clear();
148
149 for (TypeMap::const_iterator iter = mTypeMap.begin(); iter != mTypeMap.end(); ++iter)
150 {
151 aListOfPtrs.push_back(static_cast<ObjectType*>(iter->second.get()));
152 }
153 }
154
156 void GetCurrentUserTypes(ObjectPtrList& aListOfPtrs) const
157 {
158 aListOfPtrs.clear();
159
160 for (TypeMap::const_iterator iter = mTypeMap.begin(); iter != mTypeMap.end(); ++iter)
161 {
162 if (mCoreTypes.find(iter->first) == mCoreTypes.end())
163 {
164 aListOfPtrs.push_back(static_cast<ObjectType*>(iter->second.get()));
165 }
166 }
167 }
168
169 bool IsType(WsfStringId aTypeName) const { return Find(aTypeName) != nullptr; }
170
173 {
175 : mIsCommandProcessed(false)
176 , mObjectTypePtr(nullptr)
177 {
178 }
179
180 operator bool() const { return mIsCommandProcessed; }
184 };
185
192
193 template<typename FACTORY_LIST>
194 void SetObjectFactory(const FACTORY_LIST* aFactoryListPtr)
195 {
196 mFactoryPtr = (void*)aFactoryListPtr;
197 // mFactoryCreateFunction = &WsfObjectTypeList<T>::FactoryCreateFunction<FACTORY_LIST>;
199 assert(mCreateSingularTypeFunction == nullptr);
200 }
201
202 template<typename DEFAULT_TYPE>
207
208 // =================================================================================================
213 virtual LoadResult LoadType(UtInput& aInput)
214 {
215 LoadResult result;
216 WsfObject* loadedObjectPtr = nullptr;
217 result.mIsCommandProcessed = LoadTypeP(aInput, loadedObjectPtr);
218 result.mObjectTypePtr = static_cast<ObjectType*>(loadedObjectPtr);
219 return result;
220 }
221
222 bool ProcessInput(UtInput& aInput) override { return LoadType(aInput); }
223
224 virtual bool InitializeType(ObjectType* aTypePtr) { return true; }
225
229
230 bool LoadNamedComponent(UtInput& aInput, WsfPlatform& aPlatform, bool aIsAdding, int aRole)
231 {
232 return LoadComponentP(aInput, aPlatform, aIsAdding, aRole, true, true);
233 }
234
235 bool LoadUnnamedComponent(UtInput& aInput, WsfPlatform& aPlatform, bool aIsAdding, int aRole)
236 {
237 return LoadComponentP(aInput, aPlatform, aIsAdding, aRole, false, true);
238 }
239
240 bool LoadUnnamedComponentWithoutEdit(UtInput& aInput, WsfPlatform& aPlatform, bool aIsAdding, int aRole)
241 {
242 return LoadComponentP(aInput, aPlatform, aIsAdding, aRole, false, false);
243 }
244
245 bool DeleteNamedComponent(UtInput& aInput, WsfPlatform& aPlatform, int aRole)
246 {
247 return DeleteComponentP(aInput, aPlatform, aRole, true);
248 }
249
250 bool DeleteUnnamedComponent(UtInput& aInput, WsfPlatform& aPlatform, int aRole)
251 {
252 return DeleteComponentP(aInput, aPlatform, aRole, false);
253 }
254
255
256protected:
257 bool InitializeTypeP(WsfObject* aObjectPtr) override { return InitializeType(static_cast<ObjectType*>(aObjectPtr)); }
258
259private:
260 // Copy constructor is declared but not defined in order to prevent its use.
261 WsfObjectTypeList(const WsfObjectTypeList& aSrc) = delete;
262 // Assignment operator is declared but not defined in order to prevent its use.
263 WsfObjectTypeList& operator=(const WsfObjectTypeList& aRhs) = delete;
264};
265
266#endif
UtStringId WsfStringId
WsfStringId – the same thing as UtStringId.
Definition WsfStringId.hpp:23
TypeMap mTypeMap
Definition WsfObjectTypeListBase.hpp:156
bool DeleteComponentP(UtInput &aInput, WsfPlatform &aPlatform, int aRole, bool aIsNamed)
Definition WsfObjectTypeList.cpp:304
virtual WsfObject * FindP(WsfStringId aId) const
Definition WsfObjectTypeList.cpp:136
bool AddP(WsfStringId aId, std::unique_ptr< WsfObject > aDefinitionPtr)
Definition WsfObjectTypeList.cpp:84
WsfObject * CloneP(WsfStringId aId) const
Definition WsfObjectTypeList.cpp:118
unsigned int mFlags
Definition WsfObjectTypeListBase.hpp:163
bool LoadTypeP(UtInput &aInput, WsfObject *&aLoadedObjectPtr)
Definition WsfObjectTypeList.cpp:164
std::set< WsfStringId > mCoreTypes
Definition WsfObjectTypeListBase.hpp:161
WsfObject *(* mCreateSingularTypeFunction)()
Definition WsfObjectTypeListBase.hpp:167
WsfObject *(* mFactoryCreateFunction)(void *aListPtr, const std::string &aTypeName)
Definition WsfObjectTypeListBase.hpp:168
@ cSINGULAR_BASE_TYPE
Definition WsfObjectTypeListBase.hpp:101
void * mFactoryPtr
Definition WsfObjectTypeListBase.hpp:170
bool LoadComponentP(UtInput &aInput, WsfPlatform &aPlatform, bool aIsAdding, int aRole, bool aIsNamed, bool aIsEditable)
Definition WsfObjectTypeList.cpp:350
WsfObject *(* mFactoryCreateDefault)()
Definition WsfObjectTypeListBase.hpp:169
WsfObjectTypeListBase(const WsfObjectTypeListBase &)=delete
Definition WsfObjectTypeList.hpp:42
ObjectPtrList GetCoreTypes() const
Returns a list of core types, i.e. types added via AddCoreType.
Definition WsfObjectTypeList.hpp:130
T * Find(WsfStringId aId) const override
Definition WsfObjectTypeList.hpp:127
void SetObjectFactory(const FACTORY_LIST *aFactoryListPtr)
Definition WsfObjectTypeList.hpp:194
bool LoadNamedComponent(UtInput &aInput, WsfPlatform &aPlatform, bool aIsAdding, int aRole)
Definition WsfObjectTypeList.hpp:230
void GetCurrentTypes(ObjectPtrList &aListOfPtrs) const
Definition WsfObjectTypeList.hpp:145
static WsfObject * FactoryCreateFunction(void *aListPtr, const std::string &aTypeName)
Definition WsfObjectTypeList.hpp:61
virtual LoadResult LoadType(UtInput &aInput)
Definition WsfObjectTypeList.hpp:213
std::vector< SensorErrorModelBase * > ObjectPtrList
Definition WsfObjectTypeList.hpp:45
bool AddCoreType(WsfStringId aId, std::unique_ptr< T > aDefinitionPtr)
Definition WsfObjectTypeList.hpp:102
bool DeleteUnnamedComponent(UtInput &aInput, WsfPlatform &aPlatform, int aRole)
Definition WsfObjectTypeList.hpp:250
SensorErrorModelBase ObjectType
Definition WsfObjectTypeList.hpp:44
void SetSingularBaseType()
Definition WsfObjectTypeList.hpp:186
bool DeleteNamedComponent(UtInput &aInput, WsfPlatform &aPlatform, int aRole)
Definition WsfObjectTypeList.hpp:245
static WsfObject * CreateFunction()
Definition WsfObjectTypeList.hpp:54
static WsfObject * CreateDefaultFunction()
Definition WsfObjectTypeList.hpp:49
bool InitializeTypeP(WsfObject *aObjectPtr) override
Definition WsfObjectTypeList.hpp:257
bool ProcessInput(UtInput &aInput) override
Definition WsfObjectTypeList.hpp:222
void GetCurrentUserTypes(ObjectPtrList &aListOfPtrs) const
Returns the list of types defined by the user (no core / builtin types).
Definition WsfObjectTypeList.hpp:156
bool LoadUnnamedComponent(UtInput &aInput, WsfPlatform &aPlatform, bool aIsAdding, int aRole)
Definition WsfObjectTypeList.hpp:235
bool Add(WsfStringId aId, std::unique_ptr< T > aDefinitionPtr)
Definition WsfObjectTypeList.hpp:91
WsfObjectTypeList(WsfScenario &aScenario, const std::string &aBlockName)
Definition WsfObjectTypeList.hpp:81
bool LoadUnnamedComponentWithoutEdit(UtInput &aInput, WsfPlatform &aPlatform, bool aIsAdding, int aRole)
Definition WsfObjectTypeList.hpp:240
void SetObjectFactoryDefault()
Definition WsfObjectTypeList.hpp:203
T * Clone(WsfStringId aId) const override
Definition WsfObjectTypeList.hpp:118
virtual bool InitializeType(ObjectType *aTypePtr)
Definition WsfObjectTypeList.hpp:224
WsfObjectTypeList(WsfScenario &aScenario, unsigned int aFlags, const std::string &aBlockName)
Definition WsfObjectTypeList.hpp:76
bool IsType(WsfStringId aTypeName) const
Definition WsfObjectTypeList.hpp:169
Definition WsfObject.hpp:40
Platforms represent a entity within the simulation.
Definition WsfPlatform.hpp:115
Contains the data required to create a simulation, and acts as the entry point for input file process...
Definition WsfScenario.hpp:111
ObjectType * mObjectTypePtr
The object loaded. Null if no object was loaded.
Definition WsfObjectTypeList.hpp:183
LoadResult()
Definition WsfObjectTypeList.hpp:174
bool mIsCommandProcessed
Definition WsfObjectTypeList.hpp:181
Copyrights Multiple, All Rights Reserved