Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/list.h @ 2170

Last change on this file since 2170 was 2077, checked in by patrick, 20 years ago

/orxonox/trunk/src: making doxygen comments in worldentity, player, world; extending API of worldentity; inserting list.h in world - this version does not compile

File size: 6.7 KB
Line 
1/*!
2  \file list.h
3  \brief Contains a template for a doubly linked list
4*/
5
6#ifndef LIST_H
7#define LIST_H
8
9#include "stdlib.h"
10
11//! An enum to list all the modes available when adding an object to a List
12enum ADDMODE {LIST_ADD_NEXT, LIST_ADD_PREV, LIST_ADD_FIRST, LIST_ADD_LAST};
13//! An enum to list the two searching directions available when removing an object from a List
14enum FINDMODE {LIST_FIND_BW, LIST_FIND_FW};
15
16//! A generic doubly linked list
17template<class T> class List
18{
19  T* object;
20  List<T>* next;
21  List<T>* prev;
22  bool bReference;
23 
24 public:
25  List (T* obj, List<T>* n, List<T>* p, bool bRef);
26  ~List ();
27 
28  int add (T* obj, ADDMODE mode, bool bRef);
29  T* get_object();
30  List<T>* get_next ();
31  List<T>* get_previous ();
32  List<T>* get_last ();
33  List<T>* get_first ();
34  void set_next (List<T>* ptr);
35  void set_prev (List<T>* ptr);
36  int remove (T* obj, FINDMODE mode);
37};
38
39
40/**
41  \brief Standard constructor
42 
43  Call this without any parameters to generate a new List which can be filled with content.
44  DO NOT create a List element that contains an object on your own, you'll lose the data
45  contained in that object and will have trouble removing the list from your memory.
46*/
47template<class T>
48List<T>::List (T* obj = NULL, List<T>* n = NULL, List<T>* p = NULL, bool bRef = false)
49{
50  object = obj;
51  next = n;
52  prev = p;
53  bReference = bRef;
54}
55
56/**
57  \brief Standard destructor
58 
59  Call this on the initially generated base List element to remove the whole List from the memory.
60  You can safely do this to any List element you want without messing up the rest of the List, but keep in mind
61  that the contained object will be deleted as well when bRef had been set to false.
62*/
63template<class T>
64List<T>::~List ()
65{
66  if (object == NULL) // deleted foot node => disband the list
67  {
68    while( next != NULL)
69    {
70      delete next;
71    }
72    while( prev != NULL)
73    {
74      delete prev;
75    }
76  }
77  else
78  {
79    if (prev != NULL) prev->set_next (next);
80    if (next != NULL) next->set_prev (prev);
81    if (!bReference) delete object;
82  }
83}
84 
85/**
86  \brief Add an object to the List
87  \param obj: A pointer to an allocated object
88  \param mode: A Value of ADDMODE (default: LIST_ADD_NEXT)
89  \param bRef: Sets whether the element is serving as a storage point or a simple listing (default: false)
90  \return 0 if the operation succeded, -1 if the element could not be added
91 
92  This adds a new List element to the chain. The mode parameter can be used to specify
93  the location where the element should be added. LIST_ADD_NEXT will add the new element directly
94  after the base element. LIST_ADD_PREV will add the new element directly before the base element.
95  LIST_ADD_FIRST will add the element at the beginning of the List whereas LIST_ADD_LAST will add
96  it to the end of the chain. If the bRef parameter is set to true, the object pointer will not be deleted
97  when the element containing that object is deleted, thus the List can be used for temporary listings as
98  well as permanent data storage.
99*/
100template<class T> 
101int List<T>::add (T* obj, ADDMODE mode = LIST_ADD_NEXT, bool bRef = false)
102{
103  List<T>* p;
104  if( obj == NULL) return -1;
105  switch (mode)
106  {
107    case LIST_ADD_NEXT:
108      p = new List<T>( obj, next, this, bRef);
109      if( next != NULL) next->set_prev (p);
110      next = p;
111      break;
112    case LIST_ADD_PREV:
113      p = new List<T>( obj, this, prev, bRef);
114      if( prev != NULL) prev->set_next (p);
115      prev = p;
116      break;
117    case LIST_ADD_FIRST:
118      if (prev == NULL) prev = new List<T> (obj, this, NULL, bRef);
119      else return prev->add (obj, mode, bRef);
120      break;
121    case LIST_ADD_LAST:
122      if (next == NULL) next = new List<T> (obj, NULL, this, bRef);
123      else return next->add (obj, mode, bRef);
124      break;
125    default:
126        return -1;
127      break;
128  }
129  return 0;
130}
131
132/**
133  \brief Get the next element of the List
134  \return The List element after the current List element
135*/
136template<class T>
137List<T>* List<T>::get_next ()
138{
139  return next;
140}
141 
142/**
143  \brief Get the previous element of the List
144  \return The List element before the current List element
145*/
146template<class T>
147List<T>* List<T>::get_previous ()
148{
149  return prev;
150}
151
152/**
153  \brief Get the last element of the List
154  \return The last List element
155*/
156template<class T>
157List<T>* List<T>::get_last ()
158{
159  if (next == NULL) return this;
160  else return next->get_last();
161}
162
163/**
164  \brief Get the first element of the List
165  \return The first List element
166*/
167template<class T>
168List<T>* List<T>::get_first ()
169{
170  if (prev == NULL) return this;
171  else return prev->get_first();
172}
173
174/**
175  \brief Removes a certain element from the List
176  \param obj: A pointer to the object that should be removed
177  \param mode: A value of FINDMODE
178  \return 0 if the element was found and removed, -1 if the element was not found
179 
180  This searches the part of the List specified with mode for the object in question.
181  When the object is found it is deleted and the List element is removed from the chain.
182  If mode is LIST_FIND_FW all elements AFTER the base element are searched, if mode is
183  LIST_FIND_BW all elements BEFORE the base element are searched. Note that the object
184  contained within the List element is NOT deleted when bRef was set to true.
185*/
186template<class T>
187int List<T>::remove (T* obj, FINDMODE mode = LIST_FIND_FW)
188{
189  if (obj == NULL) return -1;
190  else
191  {
192    switch (mode)
193    {
194      case LIST_FIND_BW:
195        if (prev == NULL) return -1;
196        else
197        {
198          if( prev->get_object() == obj)
199          {
200            delete prev;
201          }
202          else
203          {
204            return prev->remove( obj, mode);
205          }
206        }
207        break;
208      case LIST_FIND_FW:
209        if (next == NULL) return -1;
210        else
211        {
212          if( next->get_object() == obj)
213          {
214            delete next;
215          }
216          else
217          {
218            return next->remove( obj, mode);
219          }
220        }
221        break;
222      default:
223        return -1;
224    }
225  }
226  return 0;
227}
228
229/**
230  \brief Set the next element of a List element
231  \param ptr: A pointer to the new next element
232   
233  Sets the next element of a List element... Better not touch this, it can really mess up a List.
234*/
235template<class T>
236void List<T>::set_next (List<T>* ptr)
237{
238  next = ptr;
239}
240
241/**
242  \brief Set the prev element of a List element
243  \param ptr: A pointer to the new previous element
244   
245  Sets the previous element of a List element... Better not touch this, it can really mess up a List.
246*/
247template<class T>
248void List<T>::set_prev (List<T>* ptr)
249{
250  prev = ptr;
251}
252
253/**
254  \brief Get the pointer to the object the element is containing
255  \return The contained object (will be NULL if called on the base element).
256*/
257template<class T>
258T* List<T>::get_object()
259{
260  return object;
261}
262
263
264
265#endif
266
Note: See TracBrowser for help on using the repository browser.