2 #ifndef _GLIBMM_LISTHANDLE_H
3 #define _GLIBMM_LISTHANDLE_H
7 /* Copyright (C) 2002 The gtkmm Development Team
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glib/glist.h>
25 #include <glibmm/containerhandle_shared.h>
31 namespace Container_Helpers
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 /* Create and fill a GList as efficient as possible.
37 * This requires bidirectional iterators.
39 template <class Bi, class Tr>
40 GList* create_list(Bi pbegin, Bi pend, Tr)
46 // Use & to force a warning if the iterator returns a temporary object.
47 const void *const item = Tr::to_c_type(*&*--pend);
48 head = g_list_prepend(head, const_cast<void*>(item));
54 /* Create a GList from a 0-terminated input sequence.
55 * Build it in reverse order and reverse the whole list afterwards,
56 * because appending to the list would be horribly inefficient.
58 template <class For, class Tr>
59 GList* create_list(For pbegin, Tr)
65 // Use & to force a warning if the iterator returns a temporary object.
66 const void *const item = Tr::to_c_type(*&*pbegin);
67 head = g_list_prepend(head, const_cast<void*>(item));
71 return g_list_reverse(head);
75 /* Convert from any container that supports bidirectional iterators.
77 template <class Tr, class Cont>
78 struct ListSourceTraits
80 static GList* get_data(const Cont& cont)
81 { return Glib::Container_Helpers::create_list(cont.begin(), cont.end(), Tr()); }
83 static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW;
86 /* Convert from a 0-terminated array. The Cont
87 * argument must be a pointer to the first element.
89 template <class Tr, class Cont>
90 struct ListSourceTraits<Tr,Cont*>
92 static GList* get_data(const Cont* array)
93 { return (array) ? Glib::Container_Helpers::create_list(array, Tr()) : 0; }
95 static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW;
98 template <class Tr, class Cont>
99 struct ListSourceTraits<Tr,const Cont*> : ListSourceTraits<Tr,Cont*>
102 /* Convert from a 0-terminated array. The Cont argument must be a pointer
103 * to the first element. For consistency, the array must be 0-terminated,
104 * even though the array size is known at compile time.
106 template <class Tr, class Cont, size_t N>
107 struct ListSourceTraits<Tr,Cont[N]>
109 static GList* get_data(const Cont* array)
110 { return Glib::Container_Helpers::create_list(array, array + (N - 1), Tr()); }
112 static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW;
115 template <class Tr, class Cont, size_t N>
116 struct ListSourceTraits<Tr,const Cont[N]> : ListSourceTraits<Tr,Cont[N]>
119 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
123 * @ingroup ContHelpers
124 * If a method takes this as an argument, or has this as a return type, then you can use a standard
125 * container such as std::list or std::vector.
128 class ListHandleIterator
131 typedef typename Tr::CppType CppType;
132 typedef typename Tr::CType CType;
134 typedef std::forward_iterator_tag iterator_category;
135 typedef CppType value_type;
136 typedef ptrdiff_t difference_type;
137 typedef value_type reference;
138 typedef void pointer;
140 explicit inline ListHandleIterator(const GList* node);
142 inline value_type operator*() const;
143 inline ListHandleIterator<Tr> & operator++();
144 inline const ListHandleIterator<Tr> operator++(int);
146 inline bool operator==(const ListHandleIterator<Tr>& rhs) const;
147 inline bool operator!=(const ListHandleIterator<Tr>& rhs) const;
153 } // namespace Container_Helpers
157 * @ingroup ContHandles
159 template < class T, class Tr = Glib::Container_Helpers::TypeTraits<T> >
163 typedef typename Tr::CppType CppType;
164 typedef typename Tr::CType CType;
166 typedef CppType value_type;
167 typedef size_t size_type;
168 typedef ptrdiff_t difference_type;
170 typedef Glib::Container_Helpers::ListHandleIterator<Tr> const_iterator;
171 typedef Glib::Container_Helpers::ListHandleIterator<Tr> iterator;
173 template <class Cont> inline
174 ListHandle(const Cont& container);
176 // Take over ownership of an array created by GTK+ functions.
177 inline ListHandle(GList* glist, Glib::OwnershipType ownership);
179 // Copying clears the ownership flag of the source handle.
180 inline ListHandle(const ListHandle<T,Tr>& other);
184 inline const_iterator begin() const;
185 inline const_iterator end() const;
187 template <class U> inline operator std::vector<U>() const;
188 template <class U> inline operator std::deque<U>() const;
189 template <class U> inline operator std::list<U>() const;
191 template <class Cont> inline
192 void assign_to(Cont& container) const;
194 template <class Out> inline
195 void copy(Out pdest) const;
197 inline GList* data() const;
198 inline size_t size() const;
199 inline bool empty() const;
203 mutable Glib::OwnershipType ownership_;
205 // No copy assignment.
206 ListHandle<T,Tr>& operator=(const ListHandle<T,Tr>&);
210 /***************************************************************************/
211 /* Inline implementation */
212 /***************************************************************************/
214 #ifndef DOXYGEN_SHOULD_SKIP_THIS
216 namespace Container_Helpers
219 /**** Glib::Container_Helpers::ListHandleIterator<> ************************/
221 template <class Tr> inline
222 ListHandleIterator<Tr>::ListHandleIterator(const GList* node)
227 template <class Tr> inline
228 typename ListHandleIterator<Tr>::value_type ListHandleIterator<Tr>::operator*() const
230 return Tr::to_cpp_type(static_cast<typename Tr::CTypeNonConst>(node_->data));
233 template <class Tr> inline
234 ListHandleIterator<Tr>& ListHandleIterator<Tr>::operator++()
240 template <class Tr> inline
241 const ListHandleIterator<Tr> ListHandleIterator<Tr>::operator++(int)
243 const ListHandleIterator<Tr> tmp (*this);
248 template <class Tr> inline
249 bool ListHandleIterator<Tr>::operator==(const ListHandleIterator<Tr>& rhs) const
251 return (node_ == rhs.node_);
254 template <class Tr> inline
255 bool ListHandleIterator<Tr>::operator!=(const ListHandleIterator<Tr>& rhs) const
257 return (node_ != rhs.node_);
260 } // namespace Container_Helpers
263 /**** Glib::ListHandle<> ***************************************************/
265 template <class T, class Tr>
266 template <class Cont>
268 ListHandle<T,Tr>::ListHandle(const Cont& container)
270 plist_ (Glib::Container_Helpers::ListSourceTraits<Tr,Cont>::get_data(container)),
271 ownership_ (Glib::Container_Helpers::ListSourceTraits<Tr,Cont>::initial_ownership)
274 template <class T, class Tr> inline
275 ListHandle<T,Tr>::ListHandle(GList* glist, Glib::OwnershipType ownership)
278 ownership_ (ownership)
281 template <class T, class Tr> inline
282 ListHandle<T,Tr>::ListHandle(const ListHandle<T,Tr>& other)
284 plist_ (other.plist_),
285 ownership_ (other.ownership_)
287 other.ownership_ = Glib::OWNERSHIP_NONE;
290 template <class T, class Tr>
291 ListHandle<T,Tr>::~ListHandle()
293 if(ownership_ != Glib::OWNERSHIP_NONE)
295 if(ownership_ != Glib::OWNERSHIP_SHALLOW)
297 // Deep ownership: release each container element.
298 for(GList* node = plist_; node != 0; node = node->next)
299 Tr::release_c_type(static_cast<typename Tr::CTypeNonConst>(node->data));
305 template <class T, class Tr> inline
306 typename ListHandle<T,Tr>::const_iterator ListHandle<T,Tr>::begin() const
308 return Glib::Container_Helpers::ListHandleIterator<Tr>(plist_);
311 template <class T, class Tr> inline
312 typename ListHandle<T,Tr>::const_iterator ListHandle<T,Tr>::end() const
314 return Glib::Container_Helpers::ListHandleIterator<Tr>(0);
317 template <class T, class Tr>
320 ListHandle<T,Tr>::operator std::vector<U>() const
322 #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
323 return std::vector<U>(this->begin(), this->end());
326 temp.reserve(this->size());
327 Glib::Container_Helpers::fill_container(temp, this->begin(), this->end());
332 template <class T, class Tr>
335 ListHandle<T,Tr>::operator std::deque<U>() const
337 #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
338 return std::deque<U>(this->begin(), this->end());
341 Glib::Container_Helpers::fill_container(temp, this->begin(), this->end());
346 template <class T, class Tr>
349 ListHandle<T,Tr>::operator std::list<U>() const
351 #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
352 return std::list<U>(this->begin(), this->end());
355 Glib::Container_Helpers::fill_container(temp, this->begin(), this->end());
360 template <class T, class Tr>
361 template <class Cont>
363 void ListHandle<T,Tr>::assign_to(Cont& container) const
365 #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS
366 container.assign(this->begin(), this->end());
369 Glib::Container_Helpers::fill_container(temp, this->begin(), this->end());
370 container.swap(temp);
374 template <class T, class Tr>
377 void ListHandle<T,Tr>::copy(Out pdest) const
379 std::copy(this->begin(), this->end(), pdest);
382 template <class T, class Tr> inline
383 GList* ListHandle<T,Tr>::data() const
388 template <class T, class Tr> inline
389 size_t ListHandle<T,Tr>::size() const
391 return g_list_length(plist_);
394 template <class T, class Tr> inline
395 bool ListHandle<T,Tr>::empty() const
397 return (plist_ == 0);
400 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
405 #endif /* _GLIBMM_LISTHANDLE_H */