WSF
WsfComponentFactoryList.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 WSFCOMPONENTFACTORYLIST_HPP
13#define WSFCOMPONENTFACTORYLIST_HPP
14
15#include <memory>
16
17#include "UtLog.hpp"
19#include "WsfScenario.hpp"
20
53
54template<typename PARENT_TYPE>
56{
57public:
58 typedef std::vector<std::unique_ptr<WsfComponentFactoryBase>> FactoryList;
60
62 {
63 public:
65 Iterator(const WsfScenario& aScenario)
66 : mFactoryList(aScenario.GetComponentFactoryList())
67 {
68 mIndex = 0;
69 mSize = mFactoryList.size();
70 mParentRole = cCOMPONENT_ROLE<PARENT_TYPE>();
71
72 // If the first factory is defined but doesn't have the desired role, we must advance
73 // the iterator to the dereferencing operator will return the first entry with the
74 // desired role (if one exists).
75 if ((mIndex != mSize) && (mParentRole != mFactoryList[mIndex]->GetParentRole()))
76 {
77 AdvanceP();
78 }
79 }
80
83 FactoryType* operator*() const { return static_cast<FactoryType*>(mFactoryList[mIndex].get()); }
84
88 {
89 AdvanceP();
90 return *this;
91 }
92
94 bool AtEnd() const { return mIndex >= mSize; }
95
96 private:
98 void AdvanceP()
99 {
100 ++mIndex;
101 while ((mIndex < mSize) && (mParentRole != mFactoryList[mIndex]->GetParentRole()))
102 {
103 ++mIndex;
104 }
105 }
106
107 const FactoryList& mFactoryList;
108 size_t mSize;
109 size_t mIndex;
110 int mParentRole;
111 };
112
115
116
117 static void PreInput(const WsfScenario& aScenario, PARENT_TYPE& aParent)
118 {
119 for (Iterator iter(aScenario); !iter.AtEnd(); ++iter)
120 {
121 (*iter)->PreInput(aParent);
122 }
123 }
124
125 static bool ProcessInput(const WsfScenario& aScenario, UtInput& aInput, PARENT_TYPE& aParent)
126 {
127 for (Iterator iter(aScenario); !iter.AtEnd(); ++iter)
128 {
129 if ((*iter)->ProcessInput(aInput, aParent))
130 {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 static bool ProcessAddOrEditCommand(const WsfScenario& aScenario, UtInput& aInput, PARENT_TYPE& aParent, bool aIsAdding)
138 {
139 for (Iterator iter(aScenario); !iter.AtEnd(); ++iter)
140 {
141 if ((*iter)->ProcessAddOrEditCommand(aInput, aParent, aIsAdding))
142 {
143 return true;
144 }
145 }
146 return false;
147 }
148
149 static void ProcessDeleteCommand(const WsfScenario& aScenario, UtInput& aInput, PARENT_TYPE& aParent)
150 {
151 std::string componentType(aInput.GetCommand());
152 for (Iterator iter(aScenario); !iter.AtEnd(); ++iter)
153 {
154 if ((*iter)->ProcessDeleteCommand(aInput, aParent))
155 {
156 return;
157 }
158 }
159 throw UtInput::BadValue(aInput, "Unknown object type to delete: " + componentType);
160 }
161
162 static bool PreInitialize(const WsfScenario& aScenario, double aSimTime, PARENT_TYPE& aParent)
163 {
164 bool ok(true);
165 for (Iterator iter(aScenario); !iter.AtEnd(); ++iter)
166 {
167 if (!(*iter)->PreInitialize(aSimTime, aParent))
168 {
169 ok = false;
170 auto out = ut::log::error() << "Component factory pre-initialization failed.";
171 out.AddNote() << "Component: " << aParent.GetName();
172 }
173 }
174 return ok;
175 }
176
177};
178
179#endif
bool ok
Definition WsfScriptWeaponClass.cpp:211
Definition WsfComponentFactoryList.hpp:62
Iterator & operator++()
Definition WsfComponentFactoryList.hpp:87
bool AtEnd() const
Return true if the end-of-list has been reached.
Definition WsfComponentFactoryList.hpp:94
Iterator(const WsfScenario &aScenario)
An iterator for iterating over the factories that apply to the class PARENT_TYPE.
Definition WsfComponentFactoryList.hpp:65
FactoryType * operator*() const
Definition WsfComponentFactoryList.hpp:83
Definition WsfComponentFactoryList.hpp:56
std::vector< std::unique_ptr< WsfComponentFactoryBase > > FactoryList
Definition WsfComponentFactoryList.hpp:58
static void ProcessDeleteCommand(const WsfScenario &aScenario, UtInput &aInput, PARENT_TYPE &aParent)
Definition WsfComponentFactoryList.hpp:149
static bool PreInitialize(const WsfScenario &aScenario, double aSimTime, PARENT_TYPE &aParent)
Definition WsfComponentFactoryList.hpp:162
static bool ProcessInput(const WsfScenario &aScenario, UtInput &aInput, PARENT_TYPE &aParent)
Definition WsfComponentFactoryList.hpp:125
static void PreInput(const WsfScenario &aScenario, PARENT_TYPE &aParent)
Definition WsfComponentFactoryList.hpp:117
WsfComponentFactory< PARENT_TYPE > FactoryType
Definition WsfComponentFactoryList.hpp:59
static bool ProcessAddOrEditCommand(const WsfScenario &aScenario, UtInput &aInput, PARENT_TYPE &aParent, bool aIsAdding)
Definition WsfComponentFactoryList.hpp:137
A template that defines a component factory for a given parent class.
Definition WsfComponentFactory.hpp:54
Contains the data required to create a simulation, and acts as the entry point for input file process...
Definition WsfScenario.hpp:111
Definition WsfComponentRoles.hpp:60
Copyrights Multiple, All Rights Reserved