Skip to content

Commit 2c61115

Browse files
drug007eagleivg
authored andcommitted
Refactoring: code simplified, comments added.
1 parent 75b413a commit 2c61115

File tree

2 files changed

+57
-62
lines changed

2 files changed

+57
-62
lines changed

src/xrEngine/xr_object_list.cpp

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -224,38 +224,33 @@ void CObjectList::Update(bool bForce)
224224
// Select Crow-Mode
225225
stats.Updated = 0;
226226

227-
Objects& crows = m_crows[0];
228-
229-
{
230-
Objects& crows1 = m_crows[1];
231-
crows.insert(crows.end(), crows1.begin(), crows1.end());
232-
crows1.clear();
233-
}
227+
m_primary_crows.insert(m_primary_crows.end(), m_secondary_crows.begin(), m_secondary_crows.end());
228+
m_secondary_crows.clear();
234229

235230
#if 0
236-
std::sort (crows.begin(), crows.end());
237-
crows.erase (
231+
std::sort (m_own_crows.begin(), m_own_crows.end());
232+
m_own_crows.erase (
238233
std::unique(
239-
crows.begin(),
240-
crows.end()
234+
m_own_crows.begin(),
235+
m_own_crows.end()
241236
),
242-
crows.end()
237+
m_own_crows.end()
243238
);
244239
#else
245240
#ifdef DEBUG
246-
std::sort(crows.begin(), crows.end());
247-
VERIFY(std::unique(crows.begin(), crows.end()) == crows.end());
241+
std::sort(m_primary_crows.begin(), m_primary_crows.end());
242+
VERIFY(std::unique(m_primary_crows.begin(), m_primary_crows.end()) == m_primary_crows.end());
248243
#endif // ifdef DEBUG
249244
#endif
250245

251-
stats.Crows = crows.size();
252-
Objects* workload = 0;
246+
stats.Crows = m_primary_crows.size();
247+
Objects* workload;
253248
if (!psDeviceFlags.test(rsDisableObjectsAsCrows))
254-
workload = &crows;
249+
workload = &m_primary_crows;
255250
else
256251
{
257252
workload = &objects_active;
258-
clear_crow_vec(crows);
253+
clear_crow_vec(m_primary_crows);
259254
}
260255

261256
stats.Update.Begin();
@@ -266,7 +261,7 @@ void CObjectList::Update(bool bForce)
266261
IGameObject** objects = (IGameObject**)_alloca(objects_count * sizeof(IGameObject*));
267262
std::copy(workload->begin(), workload->end(), objects);
268263

269-
crows.clear();
264+
m_primary_crows.clear();
270265

271266
IGameObject** b = objects;
272267
IGameObject** e = objects + objects_count;
@@ -492,66 +487,62 @@ IGameObject* CObjectList::Create(LPCSTR name)
492487
return O;
493488
}
494489

495-
void CObjectList::Destroy(IGameObject* O)
490+
void CObjectList::Destroy(IGameObject* game_obj)
496491
{
497-
if (0 == O)
492+
if (nullptr == game_obj)
498493
return;
499-
net_Unregister(O);
494+
net_Unregister(game_obj);
500495

501496
if (!Device.Paused())
502497
{
503-
if (!m_crows[1].empty())
498+
// if a game is paused list of other crows should be empty - Why?
499+
if (!m_secondary_crows.empty())
504500
{
505-
Msg("assertion !m_crows[1].empty() failed: %d", m_crows[1].size());
506-
507-
Objects::const_iterator i = m_crows[1].begin();
508-
Objects::const_iterator const e = m_crows[1].end();
509-
for (u32 j = 0; i != e; ++i, ++j)
510-
Msg("%d %s", j, (*i)->cName().c_str());
511-
VERIFY(Device.Paused() || m_crows[1].empty());
512-
m_crows[1].clear();
501+
Msg("assertion !m_other_crows.empty() failed: %d", m_secondary_crows.size());
502+
503+
u32 j = 0;
504+
for (auto iter: m_secondary_crows)
505+
Msg("%d %s", j++, iter->cName().c_str());
506+
VERIFY(Device.Paused() || m_secondary_crows.empty());
507+
m_secondary_crows.clear();
513508
}
514509
}
515510
else
516511
{
517-
Objects& crows = m_crows[1];
518-
Objects::iterator const i = std::find(crows.begin(), crows.end(), O);
519-
if (i != crows.end())
520-
{
521-
crows.erase(i);
522-
VERIFY(std::find(crows.begin(), crows.end(), O) == crows.end());
523-
}
512+
// if game is paused remove the object from list of other crows
513+
auto iter = std::find(m_secondary_crows.begin(), m_secondary_crows.end(), game_obj);
514+
if (iter != m_secondary_crows.end())
515+
m_secondary_crows.erase(iter);
524516
}
525517

526-
Objects& crows = m_crows[0];
527-
Objects::iterator _i0 = std::find(crows.begin(), crows.end(), O);
528-
if (_i0 != crows.end())
529518
{
530-
crows.erase(_i0);
531-
VERIFY(std::find(crows.begin(), crows.end(), O) == crows.end());
519+
// Always remove the object from list of own crows. The object may be not a crow.
520+
auto iter = std::find(m_primary_crows.begin(), m_primary_crows.end(), game_obj);
521+
if (iter != m_primary_crows.end())
522+
m_primary_crows.erase(iter);
532523
}
533524

534-
// active/inactive
535-
Objects::iterator _i = std::find(objects_active.begin(), objects_active.end(), O);
536-
if (_i != objects_active.end())
525+
// Remove the object from list of active objects if the object is active,
526+
// either remove it from list of sleeping objects if it is sleeping
527+
// and throw fatal error if the object is neither active nor sleeping
528+
auto iter = std::find(objects_active.begin(), objects_active.end(), game_obj);
529+
if (iter != objects_active.end())
537530
{
538-
objects_active.erase(_i);
539-
VERIFY(std::find(objects_active.begin(), objects_active.end(), O) == objects_active.end());
540-
VERIFY(std::find(objects_sleeping.begin(), objects_sleeping.end(), O) == objects_sleeping.end());
531+
// this object is active, so remove it from the list
532+
objects_active.erase(iter);
533+
// check that the object does not belong to list of sleeping object too
534+
VERIFY(std::find(objects_sleeping.begin(), objects_sleeping.end(), game_obj) == objects_sleeping.end());
541535
}
542536
else
543537
{
544-
Objects::iterator _ii = std::find(objects_sleeping.begin(), objects_sleeping.end(), O);
545-
if (_ii != objects_sleeping.end())
546-
{
547-
objects_sleeping.erase(_ii);
548-
VERIFY(std::find(objects_sleeping.begin(), objects_sleeping.end(), O) == objects_sleeping.end());
549-
}
550-
else
538+
iter = std::find(objects_sleeping.begin(), objects_sleeping.end(), game_obj);
539+
if (iter == objects_sleeping.end())
551540
FATAL("! Unregistered object being destroyed");
541+
objects_sleeping.erase(iter);
542+
552543
}
553544

554-
g_pGamePersistent->ObjectPool.destroy(O);
545+
g_pGamePersistent->ObjectPool.destroy(game_obj);
555546
}
556547

557548
void CObjectList::relcase_register(RELCASE_CALLBACK cb, int* ID)
@@ -586,8 +577,8 @@ bool CObjectList::dump_all_objects()
586577
dump_list(destroy_queue, "destroy_queue");
587578
dump_list(objects_active, "objects_active");
588579
dump_list(objects_sleeping, "objects_sleeping");
589-
dump_list(m_crows[0], "m_crows[0]");
590-
dump_list(m_crows[1], "m_crows[1]");
580+
dump_list(m_primary_crows, "m_own_crows");
581+
dump_list(m_secondary_crows, "m_other_crows");
591582
return false;
592583
}
593584

src/xrEngine/xr_object_list.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ class ENGINE_API CObjectList
3838
Objects destroy_queue;
3939
Objects objects_active;
4040
Objects objects_sleeping;
41-
Objects m_crows[2];
41+
/**
42+
* @brief m_primary_crows - list of items of the primary thread
43+
* @brief m_secondary_crows - list of items of the secondary thread
44+
*/
45+
Objects m_primary_crows, m_secondary_crows;
4246
tid_t m_owner_thread_id;
4347
ObjectUpdateStatistics stats;
4448
u32 statsFrame;
@@ -123,9 +127,9 @@ class ENGINE_API CObjectList
123127
IC Objects& get_crows()
124128
{
125129
if (GetCurrentThreadId() == m_owner_thread_id)
126-
return (m_crows[0]);
130+
return (m_primary_crows);
127131

128-
return (m_crows[1]);
132+
return (m_secondary_crows);
129133
}
130134

131135
static void clear_crow_vec(Objects& o);

0 commit comments

Comments
 (0)