GL Studio C++ Runtime API
tlist.h
Go to the documentation of this file.
1 /*! \file
2  \brief A templated list, disti::TList.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2017 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 #ifndef _TLIST_H
41 #define _TLIST_H
42 
43 #include "disti_include.h"
44 
45 namespace disti
46 {
47 /** A templated list class */
48 template<class T, const int deleteOnRemove = 0>
49 class TList
50 {
51 public:
52  /** List Item */
53  class ListItem
54  {
55  public:
56  ListItem* _next;
57  ListItem* _prev;
58  T _data;
59 
60  public:
61  ListItem( T& data )
62  : _next( 0 )
63  , _prev( 0 )
64  , _data( data )
65  {
66  }
67  virtual ~ListItem()
68  {
69  //FIXTHIS if (deleteOnRemove)
70  // We need to cleanup delete _data;
71  }
72  ListItem* Next()
73  {
74  return _next;
75  }
76  ListItem* Prev()
77  {
78  return _prev;
79  }
80  void Next( ListItem* next )
81  {
82  _next = next;
83  }
84  void Prev( ListItem* prev )
85  {
86  _prev = prev;
87  }
88 
89  T& Data()
90  {
91  return _data;
92  }
93 
94  void Data( T& data )
95  {
96  _data = data;
97  }
98  };
99 
100 private:
101  ListItem* _head;
102  ListItem* _tail;
103  int _count;
104 
105 public:
106  TList()
107  : _head( 0 )
108  , _tail( 0 )
109  , _count( 0 )
110  {
111  }
112  virtual ~TList()
113  {
114  while( _head )
115  Remove( _head );
116  }
117 
118  void InsertAfter( ListItem* where, T& val )
119  {
120  ListItem* newItem = new ListItem( val );
121 
122  // List is empty, where doesn't matter
123  if( _tail == 0 && _head == 0 )
124  {
125  _tail = _head = newItem;
126  }
127  else
128  {
129  if( where == _tail )
130  {
131  _tail = newItem;
132  }
133  newItem->Prev( where );
134  newItem->Next( where->Next() );
135  if( where->Next() )
136  where->Next()->Prev( newItem );
137  where->Next( newItem );
138  }
139 
140  _count++;
141  }
142 
143  /*void Add( T &val )
144  {
145  InsertAfter(_tail, val);
146  }
147  */
148  void Add( T val )
149  {
150  InsertAfter( _tail, val );
151  }
152 
153  void InsertBefore( ListItem* where, T& val )
154  {
155  ListItem* newItem = new ListItem( val );
156 
157  // List is empty, where doesn't matter
158  if( _tail == 0 && _head == 0 )
159  {
160  _tail = _head = newItem;
161  }
162  else
163  {
164  if( where == _head )
165  {
166  _head = newItem;
167  }
168  newItem->Next( where );
169  newItem->Prev( where->Prev() );
170  if( where->Prev() )
171  where->Prev()->Next( newItem );
172  where->Prev( newItem );
173  }
174  _count++;
175  }
176  ListItem* First()
177  {
178  return _head;
179  }
180  ListItem* Last()
181  {
182  return _tail;
183  }
184  int Count()
185  {
186  return _count;
187  }
188  void Remove( ListItem* where )
189  {
190  if( !where || _count <= 0 )
191  return;
192 
193  if( where == _head )
194  {
195  _head = where->Next();
196  }
197  if( where == _tail )
198  {
199  _tail = where->Prev();
200  }
201 
202  if( where->Prev() )
203  where->Prev()->Next( where->Next() );
204  if( where->Next() )
205  where->Next()->Prev( where->Prev() );
206 
207  delete where;
208  _count--;
209  }
210 };
211 
212 } // namespace disti
213 #endif
A file for all GL Studio files to include.
Definition: tlist.h:49
Definition: bmpimage.h:46
Definition: tlist.h:53