2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
22 * @brief a pointer list
24 * provides a pointer list which is prepared for being used in nested
27 * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\>
29 * @copyright © Copyright 2008 Intra2Net AG
32 #ifndef __ASYNCIO__PTR_LIST_HPP__
33 #define __ASYNCIO__PTR_LIST_HPP__
46 * @brief provides a global instance counter.
48 * The global instance counter must be initialized by the module which uses it.
49 * The main purpose for this is to keep track of the instances when PtrList's are
50 * used as global objects.
52 template< class T, bool globalcounted >
55 static int InstanceCount;
65 virtual ~GlobalCounted()
68 } // eo ~GlobalCounted
70 static int Instances()
74 }; // eo GlobalCounted
78 class GlobalCounted< class T, false >
86 virtual ~GlobalCounted()
88 } // eo ~GlobalCounted
90 }; // eo GlobalCounted
95 * @brief pointer list which supports deletion by set to NULL.
97 * This list can be used in nested iterator loops as long as some conditions
99 * - inside the loops each iterator value is tested for not get NULL.
100 * - cleaning the list is only allowed when the outmost loop exits.
104 * @tparam T type of the pointers.
105 * @tparam globalcounted determine if a global instance counter should be included.
107 template<class T, bool globalcounted= false>
109 : public GlobalCounted< PtrList< T, globalcounted >, globalcounted >
110 , protected std::list<T*>
112 typedef std::list<T*> inherited;
115 typedef GlobalCounted< PtrList< T, globalcounted >, globalcounted > GlobalCountType;
117 typedef typename inherited::iterator iterator;
118 typedef typename inherited::const_iterator const_iterator;
134 * @brief add a new item pointer to the list.
136 * @param item item pointer which should be added
138 void add_item(T* item)
140 typename inherited::iterator it= std::find(inherited::begin(), inherited::end(), item);
141 if (it != inherited::end())
143 // nothing to do since item is already in the list
146 this->push_back(item);
151 * @brief remove an item pointer from the list by setting to NULL.
153 * This sets the pointer in the list to NULL and marks the list as dirty.
155 * @param item the io object which should be removed from the list.
157 void remove_item(T* item)
159 typename inherited::iterator it= std::find(inherited::begin(), inherited::end(), item);
160 if (it == inherited::end())
165 *it = NULL; // forget the pointer
166 Dirty= true; // ..and mark the list as dirty (i.e. has NULL elements)
171 * @brief cleans the list of objects by removing the NULL elements (if any).
173 * @note this function should only be called when it is ensured that no
174 * other functions using iterators of this list.
183 // remove the NULL elements now:
185 std::remove( inherited::begin(), inherited::end(), (T*)NULL),
192 * @brief explicit set the list dirty.
194 * use this to mark the list as dirty, when an item is set to NULL via
195 * a non const iterator.
197 * @note This is really dirty; better use remove_item(T*) in that cases!
207 return inherited::begin();
213 return inherited::end();
217 const_iterator begin() const
219 return inherited::begin();
223 const_iterator end() const
225 return inherited::end();
231 }; // eo class PtrList
234 } // eo namespace Utils
235 } // eo namespace AsyncIo