Fix coverity #29357
[supertux.git] / src / supertux / game_object.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (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, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/game_object.hpp"
18 #include "supertux/object_remove_listener.hpp"
19
20 GameObject::GameObject() :
21   wants_to_die(false),
22   remove_listeners(NULL),
23   name()
24 {
25 }
26
27 GameObject::GameObject(const GameObject& rhs) :
28   wants_to_die(rhs.wants_to_die),
29   remove_listeners(NULL),
30   name(rhs.name)
31 {
32 }
33
34 GameObject::~GameObject()
35 {
36   // call remove listeners (and remove them from the list)
37   RemoveListenerListEntry* entry = remove_listeners;
38   while(entry != NULL) {
39     RemoveListenerListEntry* next = entry->next;
40     entry->listener->object_removed(this);
41     delete entry;
42     entry = next;
43   }
44 }
45
46 void
47 GameObject::add_remove_listener(ObjectRemoveListener* listener)
48 {
49   RemoveListenerListEntry* entry = new RemoveListenerListEntry();
50   entry->next = remove_listeners;
51   entry->listener = listener;
52   remove_listeners = entry;
53 }
54
55 void
56 GameObject::del_remove_listener(ObjectRemoveListener* listener)
57 {
58   RemoveListenerListEntry* entry = remove_listeners;
59   if (entry->listener == listener) {
60     remove_listeners = entry->next;
61     delete entry;
62     return;
63   }
64   RemoveListenerListEntry* next = entry->next;
65   while(next != NULL) {
66     if (next->listener == listener) {
67       entry->next = next->next;
68       delete next;
69       break;
70     }
71     entry = next;
72     next = next->next;
73   }
74 }
75
76 /* EOF */