WSF
WsfEvent.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 WSFEVENT_HPP
13#define WSFEVENT_HPP
14
15#include "wsf_export.h"
16
17#include <functional>
18
19class WsfSimulation;
20
27
29{
30public:
41
44 WsfEvent() = default;
45
49 WsfEvent(double aSimTime, int aPriority = 0)
50 : mSimTime(aSimTime)
51 , mPriority(aPriority)
52 {
53 }
54
56 WsfEvent(const WsfEvent& aSrc) = delete;
57 WsfEvent& operator=(const WsfEvent& aRhs) = delete;
58
59 virtual ~WsfEvent() = default;
60
68
71 double GetTime() const { return mSimTime; }
72
75 int GetPriority() const { return mPriority; }
76
79 bool ShouldExecute() const { return mExecute; }
80
86 void SetTime(double aSimTime) { mSimTime = aSimTime; }
87
95 void SetPriority(int aPriority) { mPriority = aPriority; }
96
99 void SetShouldExecute(bool aExecute) { mExecute = aExecute; }
100
103 WsfSimulation* GetSimulation() const { return mSimulationPtr; }
104
106 void AddedToEventQueue(WsfSimulation& aSimulation) { mSimulationPtr = &aSimulation; }
107
108private:
109 double mSimTime{0.0};
110 WsfSimulation* mSimulationPtr{nullptr};
111 int mPriority{0};
112 bool mExecute{true};
113};
114
117template<typename RT, typename... Args>
119{
120protected:
121 using ExecuteFn = std::function<RT(Args...)>;
122
123 WsfEventAdapterT(double aSimTime, const ExecuteFn& aExecuteFn)
124 : WsfEvent(aSimTime)
125 , mExecuteFn(aExecuteFn)
126 {
127 }
128
129 ~WsfEventAdapterT() override = default;
130
132};
133
139{
140public:
141 WsfOneShotEvent(double aSimTime, const ExecuteFn& aExecuteFn)
142 : WsfEventAdapterT<void>(aSimTime, aExecuteFn)
143 {
144 }
145
147 {
148 mExecuteFn();
149 return cDELETE;
150 }
151};
152
163class WSF_EXPORT WsfRecurringEvent : public WsfEventAdapterT<WsfEvent::EventDisposition, WsfEvent&>
164{
165public:
166 WsfRecurringEvent(double aSimTime, const ExecuteFn& aExecuteFn)
167 : WsfEventAdapterT<WsfEvent::EventDisposition, WsfEvent&>(aSimTime, aExecuteFn)
168 {
169 }
170
171 EventDisposition Execute() override { return mExecuteFn(*this); }
172};
173
174#endif
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
WsfEventAdapterT(double aSimTime, const ExecuteFn &aExecuteFn)
Definition WsfEvent.hpp:123
~WsfEventAdapterT() override=default
ExecuteFn mExecuteFn
Definition WsfEvent.hpp:131
std::function< RT(Args...)> ExecuteFn
Definition WsfEvent.hpp:121
Definition WsfEvent.hpp:29
double GetTime() const
Definition WsfEvent.hpp:71
void AddedToEventQueue(WsfSimulation &aSimulation)
Called when the event is added to the event queue of the specified simulation.
Definition WsfEvent.hpp:106
int GetPriority() const
Definition WsfEvent.hpp:75
virtual EventDisposition Execute()=0
WsfSimulation * GetSimulation() const
Definition WsfEvent.hpp:103
WsfEvent()=default
void SetShouldExecute(bool aExecute)
Definition WsfEvent.hpp:99
virtual ~WsfEvent()=default
bool ShouldExecute() const
Definition WsfEvent.hpp:79
void SetTime(double aSimTime)
Definition WsfEvent.hpp:86
void SetPriority(int aPriority)
Definition WsfEvent.hpp:95
EventDisposition
Definition WsfEvent.hpp:37
@ cRESCHEDULE
Reschedule the event. WsfEvent::SetTime should have been called with the new time.
Definition WsfEvent.hpp:39
@ cDELETE
Delete the event from the event queue.
Definition WsfEvent.hpp:38
WsfEvent(const WsfEvent &aSrc)=delete
Copy constructor and copy assignment deleted to prevent copying.
WsfEvent & operator=(const WsfEvent &aRhs)=delete
WsfEvent(double aSimTime, int aPriority=0)
Definition WsfEvent.hpp:49
WsfOneShotEvent(double aSimTime, const ExecuteFn &aExecuteFn)
Definition WsfEvent.hpp:141
EventDisposition Execute() override
Definition WsfEvent.hpp:146
WsfRecurringEvent(double aSimTime, const ExecuteFn &aExecuteFn)
Definition WsfEvent.hpp:166
EventDisposition Execute() override
Definition WsfEvent.hpp:171
The main controller for a simulation.
Definition WsfSimulation.hpp:109
Copyrights Multiple, All Rights Reserved