WSF
WsfTrackList.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 WSFTRACKLIST_HPP
13#define WSFTRACKLIST_HPP
14
15#include <memory>
16#include <unordered_map>
17#include <vector>
18
19#include "UtScriptAccessible.hpp"
20#include "WsfLocalTrack.hpp"
21#include "WsfTrack.hpp"
22#include "WsfTrackId.hpp"
23
36template<class T>
38{
39public:
40 WsfTrackListT() = default;
41
44 virtual ~WsfTrackListT() = default;
45
46 void AddTrack(std::unique_ptr<T> aTrackPtr);
47
52 unsigned int GetTrackCount() const { return static_cast<unsigned int>(mTrackList.size()); }
53
58 T* GetTrackEntry(unsigned int aEntryIndex) { return mTrackList[aEntryIndex].get(); }
59
64 const T* GetTrackEntry(unsigned int aEntryIndex) const { return mTrackList[aEntryIndex].get(); }
65
67 WsfTrackListT<T>* Clone() const { return new WsfTrackListT<T>(*this); }
68
69 bool DeleteTrack(const WsfTrackId& aTrackId);
70
71 unsigned int DeleteTrackEntry(unsigned int aEntryIndex);
72
74
75 T* FindTrack(const WsfTrackId& aTrackId);
76
77 const T* FindTrack(const WsfTrackId& aTrackId) const;
78
79 std::unique_ptr<T> RemoveTrack(const WsfTrackId& aTrackId);
80
81private:
82 using TrackIdMap = std::unordered_map<WsfTrackId, T*, WsfTrackId>;
83
84 std::vector<std::unique_ptr<T>> mTrackList;
85 TrackIdMap mTrackIdMap;
86};
87
90
91template<>
92constexpr const char* UtScriptClassName<WsfTrackList>()
93{
94 return "WsfTrackList";
95}
96template<>
98{
99 return "WsfLocalTrackList";
100}
101
102// **************************************************************************
103// Implementation
104// **************************************************************************
105
106#include <algorithm>
107#include <cassert>
108
109template<class T>
111{
112 for (const auto& trackPtr : aSrc.mTrackList)
113 {
114 AddTrack(ut::clone(trackPtr));
115 }
116}
117
118// virtual
125template<class T>
126void WsfTrackListT<T>::AddTrack(std::unique_ptr<T> aTrackPtr)
127{
128 mTrackIdMap[aTrackPtr->GetTrackId()] = aTrackPtr.get();
129 mTrackList.push_back(std::move(aTrackPtr));
130}
131
132// virtual
138template<class T>
140{
141 bool trackDeleted = false;
142
143 // Find the track, remove its entry in the track list and delete the track object.
144 auto trackIter =
145 std::find_if(mTrackList.begin(),
146 mTrackList.end(),
147 [aTrackId](const std::unique_ptr<T>& aTrackPtr) { return aTrackPtr->GetTrackId() == aTrackId; });
148
149 // Search the track id map before the track is deleted, as the track id may be part of the deleted track.
150 auto idIter = mTrackIdMap.find(aTrackId);
151
152 if (trackIter != mTrackList.end())
153 {
154 mTrackList.erase(trackIter);
155 trackDeleted = true;
156 }
157
158 // Find and remove the entry from the track ID map.
159 // The assertions are to check for consistency between the track list and the ID map.
160 if (idIter != mTrackIdMap.end())
161 {
162 mTrackIdMap.erase(idIter);
163 assert(trackDeleted);
164 }
165 else
166 {
167 assert(!trackDeleted);
168 }
169 return trackDeleted;
170}
171
172// virtual
175template<class T>
177{
178 // Clear out the container that points to all the tracks
179 mTrackList.clear();
180 mTrackIdMap.clear();
181}
182
183// virtual
189template<class T>
191{
192 auto iter = mTrackIdMap.find(aTrackId);
193 if (iter != mTrackIdMap.end())
194 {
195 return iter->second;
196 }
197 return nullptr;
198}
199
200// virtual
206template<class T>
207const T* WsfTrackListT<T>::FindTrack(const WsfTrackId& aTrackId) const
208{
209 return const_cast<WsfTrackListT<T>*>(this)->FindTrack(aTrackId);
210}
211
212// virtual
217template<class T>
218std::unique_ptr<T> WsfTrackListT<T>::RemoveTrack(const WsfTrackId& aTrackId)
219{
220 std::unique_ptr<T> trackPtr{nullptr};
221
222 // Find the track and remove its entry in the track list.
223 auto trackIter =
224 std::find_if(mTrackList.begin(),
225 mTrackList.end(),
226 [aTrackId](const std::unique_ptr<T>& aTrackPtr) { return aTrackPtr->GetTrackId() == aTrackId; });
227 if (trackIter != mTrackList.end())
228 {
229 trackPtr = std::move(*trackIter);
230 mTrackList.erase(trackIter);
231 }
232
233 // Find and remove the entry from the track ID map.
234 // The assertions are to check for consistency between the track list and the ID map.
235 auto idIter = mTrackIdMap.find(aTrackId);
236 if (idIter != mTrackIdMap.end())
237 {
238 mTrackIdMap.erase(idIter);
239 assert(trackPtr != nullptr);
240 }
241 else
242 {
243 assert(trackPtr == nullptr);
244 }
245
246 return trackPtr;
247}
248
252template<class T>
253unsigned int WsfTrackListT<T>::DeleteTrackEntry(unsigned int aEntryIndex)
254{
255 unsigned int nextIndex = static_cast<unsigned int>(mTrackList.size());
256 if (aEntryIndex < static_cast<unsigned int>(mTrackList.size()))
257 {
258 // Directly access the desired element, eliminating the overhead of the find_if call in DeleteTrack.
259 auto trackIter = mTrackList.begin();
260 std::advance(trackIter, aEntryIndex);
261 assert(trackIter != mTrackList.end());
262
263 // Find the entry in the track ID map.
264 auto idIter = mTrackIdMap.find((*trackIter)->GetTrackId());
265 assert(idIter != mTrackIdMap.end());
266
267 // Remove the track from the track list and from the id map.
268 DeleteTrack(idIter->first);
269
270 // If this was the last entry in the list, return "end" as the next element.
271 if (aEntryIndex != static_cast<unsigned int>(mTrackList.size()))
272 {
273 nextIndex = aEntryIndex;
274 }
275 }
276 return nextIndex;
277}
278
279#endif
WsfTrack * trackPtr
Definition WsfScriptPlatformClass.cpp:2845
constexpr const char * UtScriptClassName< WsfTrackList >()
Definition WsfTrackList.hpp:92
constexpr const char * UtScriptClassName< WsfLocalTrackList >()
Definition WsfTrackList.hpp:97
WsfTrackListT< WsfLocalTrack > WsfLocalTrackList
Definition WsfTrackList.hpp:89
WsfTrackListT< WsfTrack > WsfTrackList
Definition WsfTrackList.hpp:88
Definition WsfTrackId.hpp:33
A 'Track List' is a container for a list of 'Track' objects.
Definition WsfTrackList.hpp:38
std::unique_ptr< T > RemoveTrack(const WsfTrackId &aTrackId)
Remove the indicated track from the track list but don't delete the track object itself.
Definition WsfTrackList.hpp:218
T * FindTrack(const WsfTrackId &aTrackId)
Find the requested track.
Definition WsfTrackList.hpp:190
void AddTrack(std::unique_ptr< T > aTrackPtr)
Add a track to the track list.
Definition WsfTrackList.hpp:126
const T * GetTrackEntry(unsigned int aEntryIndex) const
Definition WsfTrackList.hpp:64
virtual ~WsfTrackListT()=default
WsfTrackListT< T > & operator=(const WsfTrackListT< T > &aRhs)=delete
WsfTrackListT< T > * Clone() const
Clone this object.
Definition WsfTrackList.hpp:67
unsigned int DeleteTrackEntry(unsigned int aEntryIndex)
Definition WsfTrackList.hpp:253
const T * FindTrack(const WsfTrackId &aTrackId) const
Find the requested track.
Definition WsfTrackList.hpp:207
bool DeleteTrack(const WsfTrackId &aTrackId)
Delete the indicated track from the track list and delete the track object.
Definition WsfTrackList.hpp:139
void DeleteAllTracks()
Delete all the tracks in the track list.
Definition WsfTrackList.hpp:176
unsigned int GetTrackCount() const
Definition WsfTrackList.hpp:52
WsfTrackListT(const WsfTrackListT< T > &aSrc)
Definition WsfTrackList.hpp:110
T * GetTrackEntry(unsigned int aEntryIndex)
Definition WsfTrackList.hpp:58
WsfTrackListT()=default
Copyrights Multiple, All Rights Reserved