3 * @brief a pointer list
5 * provides a pointer list which is prepared for being used in nested
8 * @author Reinhard Pfau \<reinhard.pfau@intra2net.com\>
10 * @copyright © Copyright 2008 Intra2Net AG
12 * @contact info@intra2net.com
16 #ifndef __ASYNCIO__PTR_LIST_HPP__
17 #define __ASYNCIO__PTR_LIST_HPP__
30 * @brief provides a global instance counter.
32 * The global instance counter must be initialized by the module which uses it.
33 * The main purpose for this is to keep track of the instances when PtrList's are
34 * used as global objects.
36 template< class T, bool globalcounted >
39 static int InstanceCount;
49 virtual ~GlobalCounted()
52 } // eo ~GlobalCounted
54 static int Instances()
58 }; // eo GlobalCounted
62 class GlobalCounted< class T, false >
70 virtual ~GlobalCounted()
72 } // eo ~GlobalCounted
74 }; // eo GlobalCounted
79 * @brief pointer list which supports deletion by set to NULL.
81 * This list can be used in nested iterator loops as long as some conditions
83 * - inside the loops each iterator value is tested for not get NULL.
84 * - cleaning the list is only allowed when the outmost loop exits.
88 * @tparam T type of the pointers.
89 * @tparam globalcounted determine if a global instance counter should be included.
91 template<class T, bool globalcounted= false>
93 : public GlobalCounted< PtrList< T, globalcounted >, globalcounted >
94 , protected std::list<T*>
96 typedef std::list<T*> inherited;
99 typedef GlobalCounted< PtrList< T, globalcounted >, globalcounted > GlobalCountType;
101 typedef typename inherited::iterator iterator;
102 typedef typename inherited::const_iterator const_iterator;
118 * @brief add a new item pointer to the list.
120 * @param item item pointer which should be added
122 void add_item(T* item)
124 typename inherited::iterator it= std::find(inherited::begin(), inherited::end(), item);
125 if (it != inherited::end())
127 // nothing to do since item is already in the list
135 * @brief remove an item pointer from the list by setting to NULL.
137 * This sets the pointer in the list to NULL and marks the list as dirty.
139 * @param item the io object which should be removed from the list.
141 void remove_item(T* item)
143 typename inherited::iterator it= std::find(inherited::begin(), inherited::end(), item);
144 if (it == inherited::end())
149 *it = NULL; // forget the pointer
150 Dirty= true; // ..and mark the list as dirty (i.e. has NULL elements)
155 * @brief cleans the list of objects by removing the NULL elements (if any).
157 * @note this function should only be called when it is ensured that no
158 * other functions using iterators of this list.
167 // remove the NULL elements now:
169 std::remove( inherited::begin(), inherited::end(), (T*)NULL),
176 * @brief explicit set the list dirty.
178 * use this to mark the list as dirty, when an item is set to NULL via
179 * a non const iterator.
181 * @note This is really dirty; better use remove_item(T*) in that cases!
191 return inherited::begin();
197 return inherited::end();
201 const_iterator begin() const
203 return inherited::begin();
207 const_iterator end() const
209 return inherited::end();
215 }; // eo class PtrList
218 } // eo namespace Utils
219 } // eo namespace AsyncIo