Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2684 was 2636, checked in by patrick, 20 years ago
  • Added a GameLoader to the game. This enables orxonox to load a campaign consisting of multimple worlds and cinematics etc. However, cinematics are not yet implemented.

In the game you can jump from one level to the other by pressing x. Currently there are only two very simple levels defined. (DEBUG_LEVEL_0, DEBUG_LEVEL_1).

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