1212
1313using std::list;
1414
15- CClientDisplayManager::CClientDisplayManager ()
16- {
17- // Init
18- m_bCanRemoveFromList = true ;
19- }
20-
21- CClientDisplayManager::~CClientDisplayManager ()
22- {
23- RemoveAll ();
24- }
25-
26- CClientDisplay* CClientDisplayManager::Get (unsigned long ulID)
15+ std::shared_ptr<CClientDisplay> CClientDisplayManager::Get (unsigned long ulID)
2716{
2817 // Find the display with the given id
29- list<CClientDisplay*>::const_iterator iter = m_List.begin ();
30- for (; iter != m_List.end (); iter++)
18+ auto iter = m_List.begin ();
19+
20+ for (; iter != m_List.end (); iter++) // Iterate weak_ptr list
3121 {
32- if ((*iter)-> GetID () == ulID)
22+ if (const auto & display = (*iter). lock ()) // Make sure the shared_ptr still exists
3323 {
34- return *iter;
24+ if (display->GetID () == ulID)
25+ {
26+ return display;
27+ }
3528 }
29+
3630 }
3731
3832 return NULL ;
@@ -49,57 +43,38 @@ void CClientDisplayManager::DrawText2D(const char* szCaption, const CVector& vec
4943 static_cast <int >(fResHeight ), rgbaColor, szCaption, fScale , fScale , 0 );
5044}
5145
52- void CClientDisplayManager::AddToList (CClientDisplay* pDisplay )
46+ void CClientDisplayManager::AddToList (const std::shared_ptr< CClientDisplay>& display )
5347{
54- m_List.push_back (pDisplay );
48+ m_List.push_back (display );
5549}
5650
57- void CClientDisplayManager::RemoveAll ()
51+ void CClientDisplayManager::DoPulse ()
5852{
59- // Delete all the items in the list
60- m_bCanRemoveFromList = false ;
61- list<CClientDisplay*>::iterator iter = m_List.begin ();
62- for (; iter != m_List.end (); iter++)
63- {
64- delete *iter;
65- }
53+ // Render all our displays
54+ auto iter = m_List.begin ();
6655
67- // Clear the list
68- m_List.clear ();
69- m_bCanRemoveFromList = true ;
70- }
56+ // Clean up expired weak_ptr
57+ m_List.remove_if ([](const std::weak_ptr<CClientDisplay>& wp) { return wp.expired (); });
7158
72- void CClientDisplayManager::RemoveFromList (CClientDisplay* pDisplay)
73- {
74- if (m_bCanRemoveFromList)
59+ for (; iter != m_List.end (); iter++) // Iterate weak_ptr list
7560 {
76- if (!m_List. empty ())
61+ if (const auto & display = (*iter). lock ()) // Make sure the shared_ptr still exists
7762 {
78- m_List. remove (pDisplay );
63+ display-> Render ( );
7964 }
8065 }
8166}
8267
83- void CClientDisplayManager::DoPulse ( )
68+ std::shared_ptr<CClientVectorGraphicDisplay> CClientDisplayManager::CreateVectorGraphicDisplay (CClientVectorGraphic* svg )
8469{
85- // Render all our displays
86- m_bCanRemoveFromList = false ;
87- list<CClientDisplay*>::iterator iter = m_List.begin ();
88- while (iter != m_List.end ())
89- {
90- CClientDisplay* pObject = *iter;
91- if (pObject->IsExpired ())
92- {
93- // Delete it and remove it from the list
94- delete pObject;
95- iter = m_List.erase (iter);
96- }
97- else
98- {
99- ++iter;
100- pObject->Render ();
101- }
102- }
70+ auto display = std::make_shared<CClientVectorGraphicDisplay>(svg);
71+ AddToList (display);
72+ return display;
73+ }
10374
104- m_bCanRemoveFromList = true ;
75+ std::shared_ptr<CClientTextDisplay> CClientDisplayManager::CreateTextDisplay (int ID)
76+ {
77+ auto display = std::make_shared<CClientTextDisplay>(ID);
78+ AddToList (display);
79+ return display;
10580}
0 commit comments