Skip to content

Commit c2319e3

Browse files
committed
UPBGE: Implement std::vector remove and add utilities.
Two functions are added to replace frequently used behaviour: remove if an element is found in a list, or add if an element is not present in a list. The functions CM_ListRemoveIfFound and CM_ListAddIfNotFound assume these goal, they are defined in new file CM_List.h.
1 parent 2a66781 commit c2319e3

16 files changed

+92
-94
lines changed

source/gameengine/Common/CM_List.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* ***** BEGIN GPL LICENSE BLOCK *****
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*
18+
* Contributor(s): Tristan Porteries.
19+
*
20+
* ***** END GPL LICENSE BLOCK *****
21+
*/
22+
23+
/** \file CM_List.h
24+
* \ingroup common
25+
*/
26+
27+
#ifndef __CM_LIST_H__
28+
#define __CM_LIST_H__
29+
30+
#include <algorithm>
31+
32+
template <class Item, class List>
33+
inline bool CM_ListRemoveIfFound(List& list, const Item& item)
34+
{
35+
const typename List::iterator it = std::find(list.begin(), list.end(), item);
36+
if (it != list.end()) {
37+
list.erase(it);
38+
return true;
39+
}
40+
return false;
41+
}
42+
43+
template <class Item, class List>
44+
inline bool CM_ListAddIfNotFound(List& list, const Item& item)
45+
{
46+
if (std::find(list.begin(), list.end(), item) == list.end()) {
47+
list.push_back(item);
48+
return true;
49+
}
50+
return false;
51+
}
52+
53+
#endif // __CM_LIST_H__

source/gameengine/Common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ set(SRC
4444
CM_Thread.cpp
4545

4646
CM_Format.h
47+
CM_List.h
4748
CM_Message.h
4849
CM_RefCount.h
4950
CM_Thread.h

source/gameengine/Expressions/intern/BaseListValue.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ EXP_Value *EXP_BaseListValue::FindValue(const std::string& name) const
6464

6565
bool EXP_BaseListValue::SearchValue(EXP_Value *val) const
6666
{
67-
const VectorTypeConstIterator it = std::find(m_pValueArray.begin(), m_pValueArray.end(), val);
68-
if (it != m_pValueArray.end()) {
69-
return true;
70-
}
71-
return false;
67+
return (std::find(m_pValueArray.begin(), m_pValueArray.end(), val) != m_pValueArray.end());
7268
}
7369

7470
void EXP_BaseListValue::Add(EXP_Value *value)

source/gameengine/GameLogic/SCA_EventManager.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "SCA_EventManager.h"
3434
#include "SCA_ISensor.h"
3535

36+
#include "CM_List.h"
3637

3738
SCA_EventManager::SCA_EventManager(SCA_LogicManager* logicmgr, EVENT_MANAGER_TYPE mgrtype)
3839
:m_logicmgr(logicmgr),
@@ -50,23 +51,12 @@ SCA_EventManager::~SCA_EventManager()
5051

5152
bool SCA_EventManager::RegisterSensor(class SCA_ISensor* sensor)
5253
{
53-
if (std::find(m_sensors.begin(), m_sensors.end(), sensor) == m_sensors.end()) {
54-
m_sensors.push_back(sensor);
55-
return true;
56-
}
57-
58-
return false;
54+
return CM_ListAddIfNotFound(m_sensors, sensor);
5955
}
6056

6157
bool SCA_EventManager::RemoveSensor(class SCA_ISensor* sensor)
6258
{
63-
std::vector<SCA_ISensor *>::iterator it = std::find(m_sensors.begin(), m_sensors.end(), sensor);
64-
if (it != m_sensors.end()) {
65-
m_sensors.erase(it);
66-
return true;
67-
}
68-
69-
return false;
59+
return CM_ListRemoveIfFound(m_sensors, sensor);
7060
}
7161

7262
void SCA_EventManager::NextFrame(double curtime, double fixedtime)

source/gameengine/GameLogic/SCA_IActuator.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232

3333
#include "SCA_IActuator.h"
34-
#include "CM_Message.h"
3534

36-
#include <algorithm>
35+
#include "CM_Message.h"
36+
#include "CM_List.h"
3737

3838
SCA_IActuator::SCA_IActuator(SCA_IObject *gameobj, KX_ACTUATOR_TYPE type) :
3939
SCA_ILogicBrick(gameobj),
@@ -153,11 +153,7 @@ void SCA_IActuator::LinkToController(SCA_IController *controller)
153153

154154
void SCA_IActuator::UnlinkController(SCA_IController *controller)
155155
{
156-
std::vector<SCA_IController *>::iterator it = std::find(m_linkedcontrollers.begin(), m_linkedcontrollers.end(), controller);
157-
if (it != m_linkedcontrollers.end()) {
158-
m_linkedcontrollers.erase(it);
159-
}
160-
else {
156+
if (!CM_ListRemoveIfFound(m_linkedcontrollers, controller)) {
161157
CM_LogicBrickWarning(this, "Missing link from actuator " << m_gameobj->GetName() << ":"
162158
<< GetName() << " to controller " << controller->GetParent()->GetName() << ":" << controller->GetName());
163159
}

source/gameengine/GameLogic/SCA_IController.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "EXP_ListWrapper.h"
3636

3737
#include "CM_Message.h"
38+
#include "CM_List.h"
3839

3940
#include <algorithm>
4041

@@ -91,9 +92,7 @@ void SCA_IController::LinkToActuator(SCA_IActuator *actua)
9192

9293
void SCA_IController::UnlinkActuator(SCA_IActuator *actua)
9394
{
94-
std::vector<SCA_IActuator *>::iterator it = std::find(m_linkedactuators.begin(), m_linkedactuators.end(), actua);
95-
if (it != m_linkedactuators.end()) {
96-
m_linkedactuators.erase(it);
95+
if (CM_ListRemoveIfFound(m_linkedactuators, actua)) {
9796
if (IsActive()) {
9897
actua->DecLink();
9998
}
@@ -114,9 +113,7 @@ void SCA_IController::LinkToSensor(SCA_ISensor *sensor)
114113

115114
void SCA_IController::UnlinkSensor(SCA_ISensor *sensor)
116115
{
117-
std::vector<SCA_ISensor *>::iterator it = std::find(m_linkedsensors.begin(), m_linkedsensors.end(), sensor);
118-
if (it != m_linkedsensors.end()) {
119-
m_linkedsensors.erase(it);
116+
if (CM_ListRemoveIfFound(m_linkedsensors, sensor)) {
120117
if (IsActive()) {
121118
sensor->DecLink();
122119
}

source/gameengine/GameLogic/SCA_ISensor.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "SCA_PythonController.h"
4040

4141
#include "CM_Message.h"
42+
#include "CM_List.h"
4243

4344
void SCA_ISensor::ReParent(SCA_IObject *parent)
4445
{
@@ -218,11 +219,7 @@ void SCA_ISensor::LinkToController(SCA_IController *controller)
218219

219220
void SCA_ISensor::UnlinkController(SCA_IController *controller)
220221
{
221-
std::vector<SCA_IController *>::iterator it = std::find(m_linkedcontrollers.begin(), m_linkedcontrollers.end(), controller);
222-
if (it != m_linkedcontrollers.end()) {
223-
m_linkedcontrollers.erase(it);
224-
}
225-
else {
222+
if (!CM_ListRemoveIfFound(m_linkedcontrollers, controller)) {
226223
CM_LogicBrickWarning(this, "missing link from sensor " << m_gameobj->GetName() << ":" << GetName()
227224
<< " to controller " << controller->GetParent()->GetName() << ":" << controller->GetName());
228225
}

source/gameengine/GameLogic/SCA_PythonController.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,7 @@ void SCA_PythonController::SetScriptName(const std::string& name)
135135

136136
bool SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
137137
{
138-
if (std::find(m_triggeredSensors.begin(), m_triggeredSensors.end(), sensor) !=
139-
m_triggeredSensors.end())
140-
{
141-
return true;
142-
}
143-
else {
144-
return false;
145-
}
138+
return (std::find(m_triggeredSensors.begin(), m_triggeredSensors.end(), sensor) != m_triggeredSensors.end());
146139
}
147140

148141
#ifdef WITH_PYTHON

source/gameengine/GameLogic/SCA_TimeEventManager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include "SCA_LogicManager.h"
4141
#include "EXP_FloatValue.h"
4242

43+
#include "CM_List.h"
44+
4345
SCA_TimeEventManager::SCA_TimeEventManager(SCA_LogicManager* logicmgr)
4446
: SCA_EventManager(nullptr, TIME_EVENTMGR)
4547
{
@@ -100,10 +102,7 @@ void SCA_TimeEventManager::AddTimeProperty(EXP_Value* timeval)
100102

101103
void SCA_TimeEventManager::RemoveTimeProperty(EXP_Value* timeval)
102104
{
103-
std::vector<EXP_Value *>::iterator it = std::find(m_timevalues.begin(), m_timevalues.end(), timeval);
104-
if (it != m_timevalues.end()) {
105-
m_timevalues.erase(it);
106-
}
105+
CM_ListRemoveIfFound(m_timevalues, timeval);
107106
}
108107

109108
std::vector<EXP_Value*> SCA_TimeEventManager::GetTimeValues()

source/gameengine/Ketsji/KX_PythonComponentManager.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "KX_PythonComponent.h"
33
#include "KX_GameObject.h"
44

5+
#include "CM_List.h"
6+
57
KX_PythonComponentManager::KX_PythonComponentManager()
68
{
79
}
@@ -18,10 +20,7 @@ void KX_PythonComponentManager::RegisterObject(KX_GameObject *gameobj)
1820

1921
void KX_PythonComponentManager::UnregisterObject(KX_GameObject *gameobj)
2022
{
21-
std::vector<KX_GameObject *>::iterator it = std::find(m_objects.begin(), m_objects.end(), gameobj);
22-
if (it != m_objects.end()) {
23-
m_objects.erase(it);
24-
}
23+
CM_ListRemoveIfFound(m_objects, gameobj);
2524
}
2625

2726
void KX_PythonComponentManager::UpdateComponents()

0 commit comments

Comments
 (0)