WSF
WsfEventOutputBase.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 2020 Infoscitex, a DCS 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 WSF_EVENT_OUTPUT_BASE
13#define WSF_EVENT_OUTPUT_BASE
14
15#include "wsf_export.h"
16
17#include <fstream>
18#include <memory>
19
20#include "UtCallback.hpp"
21#include "UtCallbackHolder.hpp"
22#include "UtInput.hpp"
23#include "UtInputBlock.hpp"
24#include "WsfEventResult.hpp"
26#include "WsfSimulation.hpp"
28
29namespace wsf
30{
31namespace event
32{
33namespace output
34{
35struct Data
36{
37 virtual bool ProcessInput(UtInput& aInput);
38 virtual ~Data() = default;
39
40 std::map<std::string, bool> mToggledEvents;
41 std::string mFileName;
42 bool mFlushOutput{false};
44};
45
48{
49public:
51 : mData(aData)
52 {
53 }
54 bool PrepareExtension() override;
55
59 virtual void OnSimulationComplete(double aSimTime) {}
60
61 bool OpenFile(const std::string& aFileName);
62
64 const std::string& GetFileName() const { return mData.mFileName; }
65
66 std::ostream& StreamRef() const { return *mCurrentStream; }
67
68 bool IsEnabled(const std::string& aEventName) const;
69
70 void AddEvent(const std::string& aEventName, std::unique_ptr<UtCallback> aCallbackPtr);
71
77 template<typename RESULT, typename... Args>
78 void AddEvent(const std::string& aEventName, UtCallbackListN<void(Args...)>& aObserver)
79 {
80 auto fn = UtStd::Bind(&SimulationExtension::ObserverCallback<RESULT, Args...>, this);
81 AddEvent(aEventName, aObserver.Connect(fn));
82 }
83
84 void AddEventAlias(const std::string& aAliasEventName, const std::string& aCurrentEventName);
85
86 bool EnableOrDisableEvent(const std::string& aEventName, bool aEnable);
87
88 void RegisterMessagePrinter(WsfStringId aMessageType, const Settings::MessagePrintFunction& aFunction);
89
90 Settings& GetSettings() { return mData.mSettings; }
91
93 virtual void PrintEvent(const Result& aResult) const = 0;
94
96 {
97 public:
99 : mEventOutput(aEventOutput)
100 {
101 mEventOutput.EventEntry();
102 }
103 ~EventGuard() { mEventOutput.EventExit(); }
104
105 private:
106 SimulationExtension& mEventOutput;
107 };
108
109private:
110 void AddedToSimulation() override;
111
112 void EventEntry();
113 void EventExit();
114
116 virtual void EnableOrDisableEventP(const std::string& aEventName, bool aEnable) {}
117
118 void SetStream(std::ostream* aStreamPtr);
119 bool StreamIsOpen() const { return mCurrentStream != nullptr; }
120
122
123
128 template<typename RESULT, typename... Args>
129 void ObserverCallback(Args&&... args)
130 {
131 EventGuard guard(*this);
132 RESULT result(std::forward<Args>(args)..., mData.mSettings);
133 PrintEvent(result);
134 }
135
136 void SimulationStarting();
137
138 void SimulationComplete(double aSimTime);
139
141
142 struct EventData
143 {
144 std::unique_ptr<UtCallback> mCallbackPtr{nullptr};
145 bool mEnabled{false};
146 };
147
148 // The following maps a message name to the event
149 using EventMap = std::map<std::string, EventData>;
150 EventMap mEvents;
151 UtCallbackHolder mCallbacks;
152
153 using EventAliases = std::map<std::string, std::string>;
154 EventAliases mEventAliases;
155 std::ostream* mCurrentStream{nullptr};
156 std::ofstream mFileStream;
157 bool mIsInitialized{false};
158 std::recursive_mutex mMutex;
159 Data& mData;
160};
161
169template<typename DATA, typename SIM_EXTENSION>
171{
172 static_assert(std::is_base_of<Data, DATA>::value, "Template type DATA must be subclass of wsf::event::output::Data");
173 static_assert(std::is_base_of<SimulationExtension, SIM_EXTENSION>::value,
174 "Template type SIM_EXTENSION must be subclass of wsf::event::output::SimulationExtension");
175
176public:
178 : mData(ut::make_unique<DATA>())
179 {
180 }
181 bool ProcessInput(UtInput& aInput) override;
182 void SimulationCreated(WsfSimulation& aSimulation) override;
183
184private:
185 std::unique_ptr<Data> mData;
186};
187
188template<typename DATA, typename SIM_EXTENSION>
190{
191 bool myCommand{false};
192 if (aInput.GetCommand() == GetExtensionName())
193 {
194 myCommand = true;
195 UtInputBlock inputBlock(aInput);
196 inputBlock.ProcessInput(mData.get());
197 }
198 return myCommand;
199}
200
201template<typename DATA, typename SIM_EXTENSION>
203{
204 aSimulation.RegisterExtension(GetExtensionName(), ut::make_unique<SIM_EXTENSION>(dynamic_cast<DATA&>(*mData)));
205}
206
207} // namespace output
208} // namespace event
209} // namespace wsf
210#endif
UtStringId WsfStringId
WsfStringId – the same thing as UtStringId.
Definition WsfStringId.hpp:23
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
const std::string & GetExtensionName() const
Definition WsfExtension.hpp:31
WsfScenarioExtension()
Definition WsfScenarioExtension.cpp:21
WsfSimulationExtension()
Definition WsfSimulationExtension.cpp:23
The main controller for a simulation.
Definition WsfSimulation.hpp:109
void RegisterExtension(const std::string &aName, std::unique_ptr< WsfSimulationExtension > aExtensionPtr)
Definition WsfSimulation.cpp:1554
Definition WsfEventResult.hpp:117
Definition WsfEventResult.hpp:35
std::function< void(double, const WsfMessage &)> MessagePrintFunction
Definition WsfEventResult.hpp:37
bool ProcessInput(UtInput &aInput) override
Definition WsfEventOutputBase.hpp:189
ScenarioExtension()
Definition WsfEventOutputBase.hpp:177
void SimulationCreated(WsfSimulation &aSimulation) override
Definition WsfEventOutputBase.hpp:202
EventGuard(SimulationExtension &aEventOutput)
Definition WsfEventOutputBase.hpp:98
~EventGuard()
Definition WsfEventOutputBase.hpp:103
virtual void OnSimulationComplete(double aSimTime)
Definition WsfEventOutputBase.hpp:59
Settings & GetSettings()
Definition WsfEventOutputBase.hpp:90
void AddEvent(const std::string &aEventName, std::unique_ptr< UtCallback > aCallbackPtr)
Definition WsfEventOutputBase.cpp:202
void AddEvent(const std::string &aEventName, UtCallbackListN< void(Args...)> &aObserver)
Definition WsfEventOutputBase.hpp:78
std::ostream & StreamRef() const
Definition WsfEventOutputBase.hpp:66
SimulationExtension(Data &aData)
Definition WsfEventOutputBase.hpp:50
const std::string & GetFileName() const
Returns the name of the file in which the events are output.
Definition WsfEventOutputBase.hpp:64
virtual void PrintEvent(const Result &aResult) const =0
Print an event result in the desired format. Must be implemented by derived classes.
Definition WsfSensorErrorModel.hpp:21
Definition WsfEventOutputBase.cpp:36
Definition WsfXIO_EventResults.cpp:33
Function definitions for those who need run-time access to the version.
Definition WsfComm.cpp:32
Definition WsfEventOutputBase.hpp:36
std::string mFileName
Definition WsfEventOutputBase.hpp:41
virtual ~Data()=default
virtual bool ProcessInput(UtInput &aInput)
Definition WsfEventOutputBase.cpp:38
Settings mSettings
Definition WsfEventOutputBase.hpp:43
bool mFlushOutput
Definition WsfEventOutputBase.hpp:42
std::map< std::string, bool > mToggledEvents
Definition WsfEventOutputBase.hpp:40
Copyrights Multiple, All Rights Reserved