WSF
WsfVariable.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 WSFVARIABLE_HPP
13#define WSFVARIABLE_HPP
14
15#include "wsf_export.h"
16
17#include <iosfwd>
18#include <string>
19
20#include "UtInput.hpp"
21class UtScriptContext;
22#include "UtScriptData.hpp"
23class WsfComponent;
26#include "WsfStringId.hpp"
27
32
33//-------------------------------------------------------------------------------------------------
39{
40public:
41 // NOTE: The default (member-wise) copy constructor and assignment operator are used.
42
44
46 bool HasDefaultValue() const { return mHasDefaultValue; }
47
49 bool IsReference() const { return !mRefVarNameId.IsNull(); }
50
51protected:
52 bool DefaultOptionPresent(UtInput& aInput);
53
54 std::string ObjectName(WsfComponent* aObjectPtr);
55
56 bool ReadReference(UtInput& aInput);
57
58 bool ResolveReference(const std::string& aVariableName,
59 WsfComponent* aObjectPtr,
60 WsfScriptContext* aContextPtr,
61 const UtScriptData*& aRefValuePtr,
62 WsfScriptContext& aGlobalContext);
63
65
66 bool Initialize(const std::string& aName,
67 WsfComponent* aObjectPtr,
68 WsfScriptContext* aContextPtr,
69 double& aValue,
70 WsfScriptContext& aGlobalContext);
71
72 void ReadValue(UtInput& aInput, double& aValue);
73
74 void ReadValueOfType(UtInput& aInput, UtInput::ValueType aType, double& aValue);
76
78
79 bool Initialize(const std::string& aName,
80 WsfComponent* aObjectPtr,
81 WsfScriptContext* aContextPtr,
82 float& aValue,
83 WsfScriptContext& aGlobalContext);
84
85 void ReadValue(UtInput& aInput, float& aValue);
86
87 void ReadValueOfType(UtInput& aInput, UtInput::ValueType aType, float& aValue);
89
91
92 bool Initialize(const std::string& aName,
93 WsfComponent* aObjectPtr,
94 WsfScriptContext* aContextPtr,
95 int& aValue,
96 WsfScriptContext& aGlobalContext);
97
98 void ReadValue(UtInput& aInput, int& aValue);
99
100 void ReadValueOfType(UtInput& aInput, UtInput::ValueType aType, int& aValue);
102
104
105 bool Initialize(const std::string& aName,
106 WsfComponent* aObjectPtr,
107 WsfScriptContext* aContextPtr,
108 bool& aValue,
109 WsfScriptContext& aGlobalContext);
110
111 void ReadValue(UtInput& aInput, bool& aValue);
113
115
116 bool Initialize(const std::string& aName,
117 WsfComponent* aObjectPtr,
118 WsfScriptContext* aContextPtr,
119 std::string& aValue,
120 WsfScriptContext& aGlobalContext);
121
122 void ReadValue(UtInput& aInput, std::string& aValue);
124
126
127 bool Initialize(const std::string& aName,
128 WsfComponent* aObjectPtr,
129 WsfScriptContext* aContextPtr,
130 WsfStringId& aValue,
131 WsfScriptContext& aGlobalContext);
132
133 void ReadValue(UtInput& aInput, WsfStringId& aValue);
135
138
141};
142
143//-------------------------------------------------------------------------------------------------
147template<typename T>
149{
150public:
151 // NOTE: The default (member-wise) copy constructor and assignment operator are used.
152 // Copying or assigning a 'Initialize'd variable is OK as long as the copy would also
153 // refer to the same script context.
154
155 WsfVariable() = default;
156
157 WsfVariable(const T& aInitialValue)
159 , mValue(aInitialValue)
160 {
161 }
162
163 WsfVariable(const WsfVariable<T>&) = default;
164
167 operator T() const { return mValue; }
168
169 T GetValue() const { return mValue; }
170
171 WsfVariable<T>& operator=(const T& aRhs)
172 {
173 mRefVarNameId.Clear();
174 mHasDefaultValue = false;
175 mValue = aRhs;
176 return *this;
177 }
178
180 {
183 mValue = aRhs.mValue;
184 return *this;
185 }
186
187 T operator+(const T& aRhs) const { return operator T() + aRhs; }
188
189 T operator-(const T& aRhs) const { return operator T() - aRhs; }
190
191 T& operator+=(const T& aRhs)
192 {
193 mRefVarNameId.Clear();
194 mHasDefaultValue = false;
195 mValue = operator T() + aRhs;
196 return mValue;
197 }
198
199 T& operator-=(const T& aRhs)
200 {
201 mRefVarNameId.Clear();
202 mHasDefaultValue = false;
203 mValue = operator T() - aRhs;
204 return mValue;
205 }
206
209 bool Initialize(const std::string& aName,
210 WsfComponent* aObjectPtr,
211 WsfScriptContext* aContextPtr,
212 WsfScriptContext& aGlobalContext)
213 {
214 return WsfVariableBase::Initialize(aName, aObjectPtr, aContextPtr, mValue, aGlobalContext);
215 }
216
218 void ReadValue(UtInput& aInput) { return WsfVariableBase::ReadValue(aInput, mValue); }
219
221 void ReadValueOfType(UtInput& aInput, UtInput::ValueType aType)
222 {
223 return WsfVariableBase::ReadValueOfType(aInput, aType, mValue);
224 }
225
228 void ValueInClosedRange(UtInput& aInput, T aMinValue, T aMaxValue)
229 {
230 if ((!IsReference()) || HasDefaultValue())
231 {
232 aInput.ValueInClosedRange(mValue, aMinValue, aMaxValue);
233 }
234 }
235
238 void ValueGreater(UtInput& aInput, T aMinValue)
239 {
240 if ((!IsReference()) || HasDefaultValue())
241 {
242 aInput.ValueGreater(mValue, aMinValue);
243 }
244 }
245
248 void ValueGreaterOrEqual(UtInput& aInput, T aMinValue)
249 {
250 if ((!IsReference()) || HasDefaultValue())
251 {
252 aInput.ValueGreaterOrEqual(mValue, aMinValue);
253 }
254 }
255
258 void ValueLess(UtInput& aInput, T aMaxValue)
259 {
260 if ((!IsReference()) || HasDefaultValue())
261 {
262 aInput.ValueLess(mValue, aMaxValue);
263 }
264 }
265
268 void ValueLessOrEqual(UtInput& aInput, T aMaxValue)
269 {
270 if ((!IsReference()) || HasDefaultValue())
271 {
272 aInput.ValueLessOrEqual(mValue, aMaxValue);
273 }
274 }
275
276private:
277 T mValue;
278};
279
280// ================================================================================================
281// Explicit specialization of WsfVariable for WsfStringId.
282
283template<>
285{
286public:
287 WsfVariable() = default;
288
289 WsfVariable(const std::string& aInitialValue)
291 , mValue(aInitialValue)
292 {
293 }
294
297 , mValue(aInitialValue)
298 {
299 }
300
301 WsfStringId operator=(const std::string& aValue)
302 {
303 mValue = aValue;
304 return mValue;
305 }
306
308 {
309 mValue = aValue;
310 return mValue;
311 }
312
313 operator WsfStringId() const { return mValue; }
314
315 operator const std::string&() const { return mValue.GetString(); }
316
317 WsfStringId GetId() const { return mValue; }
318
319 const std::string& GetString() const { return mValue.GetString(); }
320
321 WsfStringId GetConstReference() const { return mValue; }
322
325 bool Initialize(const std::string& aName,
326 WsfComponent* aObjectPtr,
327 WsfScriptContext* aContextPtr,
328 WsfScriptContext& aGlobalContext)
329 {
330 return WsfVariableBase::Initialize(aName, aObjectPtr, aContextPtr, mValue, aGlobalContext);
331 }
332
334 void ReadValue(UtInput& aInput) { WsfVariableBase::ReadValue(aInput, mValue); }
335
336private:
337 WsfStringId mValue;
338};
339
340// ================================================================================================
341// A stream inserter is defined for a WsfVariable<std::string> or WsfVariable<WsfStringId> because
342// the normal casting operator did not work (it worked for POD's, but not class types).
343
344std::ostream& operator<<(std::ostream& aOut, const WsfVariable<std::string>& aVariable);
345
346std::ostream& operator<<(std::ostream& aOut, const WsfVariable<WsfStringId>& aVariable);
347
348#endif
ut::script::Data UtScriptData
Definition WsfScriptContext.hpp:47
UtStringId WsfStringId
WsfStringId – the same thing as UtStringId.
Definition WsfStringId.hpp:23
std::ostream & operator<<(std::ostream &aOut, const WsfVariable< std::string > &aVariable)
Definition WsfVariable.cpp:490
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
Definition WsfComponent.hpp:39
Definition WsfScriptContext.hpp:71
Provides the overall management of the scripting capability within the application and scenarios.
Definition WsfScriptManager.hpp:31
void ReadValueOfType(UtInput &aInput, UtInput::ValueType aType, double &aValue)
Definition WsfVariable.cpp:250
bool HasDefaultValue() const
Has a default value been defined for a reference.
Definition WsfVariable.hpp:46
void ReadValue(UtInput &aInput, double &aValue)
Definition WsfVariable.cpp:233
WsfVariableBase()
Definition WsfVariable.cpp:27
bool mHasDefaultValue
'true' if the '/default' option was provided
Definition WsfVariable.hpp:140
bool IsReference() const
Is the variable a reference to a script variable?
Definition WsfVariable.hpp:49
bool Initialize(const std::string &aName, WsfComponent *aObjectPtr, WsfScriptContext *aContextPtr, double &aValue, WsfScriptContext &aGlobalContext)
Definition WsfVariable.cpp:217
WsfStringId mRefVarNameId
The name (ID) of the referenced variable. It is less than zero if the variable is not a reference.
Definition WsfVariable.hpp:137
WsfStringId operator=(const std::string &aValue)
Definition WsfVariable.hpp:301
WsfStringId GetConstReference() const
Definition WsfVariable.hpp:321
WsfVariable(const std::string &aInitialValue)
Definition WsfVariable.hpp:289
WsfVariable(WsfStringId aInitialValue)
Definition WsfVariable.hpp:295
const std::string & GetString() const
Definition WsfVariable.hpp:319
void ReadValue(UtInput &aInput)
Read a value from an input stream.
Definition WsfVariable.hpp:334
WsfStringId operator=(WsfStringId aValue)
Definition WsfVariable.hpp:307
bool Initialize(const std::string &aName, WsfComponent *aObjectPtr, WsfScriptContext *aContextPtr, WsfScriptContext &aGlobalContext)
Definition WsfVariable.hpp:325
WsfStringId GetId() const
Definition WsfVariable.hpp:317
Definition WsfVariable.hpp:149
T operator-(const T &aRhs) const
Definition WsfVariable.hpp:189
void ReadValueOfType(UtInput &aInput, UtInput::ValueType aType)
Read a unitary value from an input stream.
Definition WsfVariable.hpp:221
void ValueLess(UtInput &aInput, T aMaxValue)
Definition WsfVariable.hpp:258
T GetValue() const
Definition WsfVariable.hpp:169
void ValueInClosedRange(UtInput &aInput, T aMinValue, T aMaxValue)
Definition WsfVariable.hpp:228
void ValueLessOrEqual(UtInput &aInput, T aMaxValue)
Definition WsfVariable.hpp:268
T & operator-=(const T &aRhs)
Definition WsfVariable.hpp:199
void ReadValue(UtInput &aInput)
Read a non-unitary value from an input stream.
Definition WsfVariable.hpp:218
void ValueGreater(UtInput &aInput, T aMinValue)
Definition WsfVariable.hpp:238
T & operator+=(const T &aRhs)
Definition WsfVariable.hpp:191
T operator+(const T &aRhs) const
Definition WsfVariable.hpp:187
void ValueGreaterOrEqual(UtInput &aInput, T aMinValue)
Definition WsfVariable.hpp:248
WsfVariable()=default
WsfVariable< T > & operator=(const T &aRhs)
Definition WsfVariable.hpp:171
WsfVariable(const WsfVariable< T > &)=default
bool Initialize(const std::string &aName, WsfComponent *aObjectPtr, WsfScriptContext *aContextPtr, WsfScriptContext &aGlobalContext)
Definition WsfVariable.hpp:209
WsfVariable(const T &aInitialValue)
Definition WsfVariable.hpp:157
WsfVariable< T > & operator=(const WsfVariable< T > &aRhs)
Definition WsfVariable.hpp:179
Copyrights Multiple, All Rights Reserved