WSF
WsfStableOrderEventManager.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 WSFSTABLEORDEREVENTMANAGER_HPP
13#define WSFSTABLEORDEREVENTMANAGER_HPP
14
15#include "wsf_export.h"
16
17#include <mutex>
18#include <queue>
19#include <vector>
20
21#include "WsfEventManager.hpp"
22
23class WsfEvent;
24
31{
32public:
35 , mStableEvents()
36 {
37 }
38
40
41 typedef std::pair<WsfEvent*, unsigned int> StableEvent;
42
53 virtual void AddEvent(WsfEvent* aEventPtr)
54 {
55 mMutex.lock();
56 mStableEvents.push(std::make_pair<WsfEvent*, unsigned int>(aEventPtr, mNextCount++));
57 mMutex.unlock();
58 }
59
66 {
67 WsfEvent* eventPtr = 0;
68 if (!mStableEvents.empty())
69 {
70 mMutex.lock();
71 eventPtr = mStableEvents.top().first;
72 mMutex.unlock();
73 }
74 return eventPtr;
75 }
76
82 virtual WsfEvent* PopEvent()
83 {
84 WsfEvent* eventPtr = 0;
85 if (!mStableEvents.empty())
86 {
87 mMutex.lock();
88 eventPtr = mStableEvents.top().first;
89 mStableEvents.pop();
90 mMutex.unlock();
91 }
92 return eventPtr;
93 }
94
96 virtual void Reset()
97 {
98 mMutex.lock();
99 while (!mStableEvents.empty())
100 {
101 WsfEvent* eventPtr = mStableEvents.top().first;
102 mStableEvents.pop();
103 delete eventPtr;
104 }
105 mMutex.unlock();
106 mNextCount = 0;
107 }
108
109private:
112
115
123
124 class StableCompare
125 {
126 public:
127 StableCompare()
128 : mDummy(0)
129 {
130 }
131 bool operator()(StableEvent aLhs, StableEvent aRhs) const
132 {
133 if (aLhs.first->GetTime() > aRhs.first->GetTime())
134 {
135 return true;
136 }
137 else if (aLhs.first->GetTime() < aRhs.first->GetTime())
138 {
139 return false;
140 }
141 else
142 {
143 return (aLhs.second > aRhs.second);
144 }
145 }
146
147 private:
148 int mDummy;
149 };
150
151 // std::priority_queue<WsfEvent*, std::vector<WsfEvent*>, StableCompare> mEvents;
152 // std::recursive_mutex mMutex;
153
154 std::priority_queue<StableEvent, std::vector<StableEvent>, StableCompare> mStableEvents;
155 static unsigned int mNextCount;
156};
157
158unsigned int WsfStableOrderEventManager::mNextCount = 0;
159
160#endif
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
virtual void Reset()
Reset the event queue back to an empty state.
Definition WsfEventManager.cpp:57
std::recursive_mutex mMutex
Definition WsfEventManager.hpp:103
WsfEventManager(WsfSimulation &aSimulation)
Definition WsfEventManager.cpp:16
WsfEventManager & operator=(const WsfEventManager &aRhs)=delete
Definition WsfEvent.hpp:29
Definition WsfStableOrderEventManager.hpp:31
std::pair< WsfEvent *, unsigned int > StableEvent
Definition WsfStableOrderEventManager.hpp:41
virtual void AddEvent(WsfEvent *aEventPtr)
Definition WsfStableOrderEventManager.hpp:53
virtual WsfEvent * PopEvent()
Definition WsfStableOrderEventManager.hpp:82
virtual WsfEvent * PeekEvent()
Definition WsfStableOrderEventManager.hpp:65
virtual ~WsfStableOrderEventManager()
Definition WsfStableOrderEventManager.hpp:39
virtual void Reset()
Reset the event queue back to an empty state.
Definition WsfStableOrderEventManager.hpp:96
WsfStableOrderEventManager()
Definition WsfStableOrderEventManager.hpp:33
Copyrights Multiple, All Rights Reserved