#ifndef _GLIBMM_LISTHANDLE_H #define _GLIBMM_LISTHANDLE_H /* Copyright (C) 2002 The gtkmm Development Team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include #include #include namespace Glib { namespace Container_Helpers { #ifndef DOXYGEN_SHOULD_SKIP_THIS /* Create and fill a GList as efficient as possible. * This requires bidirectional iterators. */ template GList* create_list(Bi pbegin, Bi pend, Tr) { GList* head = nullptr; while (pend != pbegin) { // Use & to force a warning if the iterator returns a temporary object. const void* const item = Tr::to_c_type(*&*--pend); head = g_list_prepend(head, const_cast(item)); } return head; } /* Create a GList from a 0-terminated input sequence. * Build it in reverse order and reverse the whole list afterwards, * because appending to the list would be horribly inefficient. */ template GList* create_list(For pbegin, Tr) { GList* head = nullptr; while (*pbegin) { // Use & to force a warning if the iterator returns a temporary object. const void* const item = Tr::to_c_type(*&*pbegin); head = g_list_prepend(head, const_cast(item)); ++pbegin; } return g_list_reverse(head); } /* Convert from any container that supports bidirectional iterators. */ template struct ListSourceTraits { static GList* get_data(const Cont& cont) { return Glib::Container_Helpers::create_list(cont.begin(), cont.end(), Tr()); } static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW; }; /* Convert from a 0-terminated array. The Cont * argument must be a pointer to the first element. */ template struct ListSourceTraits { static GList* get_data(const Cont* array) { return (array) ? Glib::Container_Helpers::create_list(array, Tr()) : nullptr; } static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW; }; template struct ListSourceTraits : ListSourceTraits { }; /* Convert from a 0-terminated array. The Cont argument must be a pointer * to the first element. For consistency, the array must be 0-terminated, * even though the array size is known at compile time. */ template struct ListSourceTraits { static GList* get_data(const Cont* array) { return Glib::Container_Helpers::create_list(array, array + (N - 1), Tr()); } static const Glib::OwnershipType initial_ownership = Glib::OWNERSHIP_SHALLOW; }; template struct ListSourceTraits : ListSourceTraits { }; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ /** * @ingroup ContHelpers */ template class ListHandleIterator { public: using CppType = typename Tr::CppType; using CType = typename Tr::CType; using iterator_category = std::forward_iterator_tag; using value_type = CppType; using difference_type = std::ptrdiff_t; using reference = value_type; using pointer = void; explicit inline ListHandleIterator(const GList* node); inline value_type operator*() const; inline ListHandleIterator& operator++(); inline const ListHandleIterator operator++(int); inline bool operator==(const ListHandleIterator& rhs) const; inline bool operator!=(const ListHandleIterator& rhs) const; private: const GList* node_; }; } // namespace Container_Helpers // TODO: Remove this when we can break glibmm API. /** This is an intermediate type. When a method takes this, or returns this, you * should use a standard C++ container of your choice, such as std::list or * std::vector. * * However, this is not used in new API. We now prefer to just use std::vector, * which is less flexibile, but makes the API clearer. * * @ingroup ContHandles */ template > class ListHandle { public: using CppType = typename Tr::CppType; using CType = typename Tr::CType; using value_type = CppType; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using const_iterator = Glib::Container_Helpers::ListHandleIterator; using iterator = Glib::Container_Helpers::ListHandleIterator; template inline ListHandle(const Cont& container); // Take over ownership of an array created by GTK+ functions. inline ListHandle(GList* glist, Glib::OwnershipType ownership); // Copying clears the ownership flag of the source handle. inline ListHandle(const ListHandle& other); ~ListHandle() noexcept; inline const_iterator begin() const; inline const_iterator end() const; template inline operator std::vector() const; template inline operator std::deque() const; template inline operator std::list() const; template inline void assign_to(Cont& container) const; template inline void copy(Out pdest) const; inline GList* data() const; inline std::size_t size() const; inline bool empty() const; private: GList* plist_; mutable Glib::OwnershipType ownership_; // No copy assignment. ListHandle& operator=(const ListHandle&); }; /***************************************************************************/ /* Inline implementation */ /***************************************************************************/ #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace Container_Helpers { /**** Glib::Container_Helpers::ListHandleIterator<> ************************/ template inline ListHandleIterator::ListHandleIterator(const GList* node) : node_(node) { } template inline typename ListHandleIterator::value_type ListHandleIterator::operator*() const { return Tr::to_cpp_type(static_cast(node_->data)); } template inline ListHandleIterator& ListHandleIterator::operator++() { node_ = node_->next; return *this; } template inline const ListHandleIterator ListHandleIterator::operator++(int) { const ListHandleIterator tmp(*this); node_ = node_->next; return tmp; } template inline bool ListHandleIterator::operator==(const ListHandleIterator& rhs) const { return (node_ == rhs.node_); } template inline bool ListHandleIterator::operator!=(const ListHandleIterator& rhs) const { return (node_ != rhs.node_); } } // namespace Container_Helpers /**** Glib::ListHandle<> ***************************************************/ template template inline ListHandle::ListHandle(const Cont& container) : plist_(Glib::Container_Helpers::ListSourceTraits::get_data(container)), ownership_(Glib::Container_Helpers::ListSourceTraits::initial_ownership) { } template inline ListHandle::ListHandle(GList* glist, Glib::OwnershipType ownership) : plist_(glist), ownership_(ownership) { } template inline ListHandle::ListHandle(const ListHandle& other) : plist_(other.plist_), ownership_(other.ownership_) { other.ownership_ = Glib::OWNERSHIP_NONE; } template ListHandle::~ListHandle() noexcept { if (ownership_ != Glib::OWNERSHIP_NONE) { if (ownership_ != Glib::OWNERSHIP_SHALLOW) { // Deep ownership: release each container element. for (GList* node = plist_; node != nullptr; node = node->next) Tr::release_c_type(static_cast(node->data)); } g_list_free(plist_); } } template inline typename ListHandle::const_iterator ListHandle::begin() const { return Glib::Container_Helpers::ListHandleIterator(plist_); } template inline typename ListHandle::const_iterator ListHandle::end() const { return Glib::Container_Helpers::ListHandleIterator(nullptr); } template template inline ListHandle::operator std::vector() const { #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS return std::vector(this->begin(), this->end()); #else std::vector temp; temp.reserve(this->size()); Glib::Container_Helpers::fill_container(temp, this->begin(), this->end()); return temp; #endif } template template inline ListHandle::operator std::deque() const { #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS return std::deque(this->begin(), this->end()); #else std::deque temp; Glib::Container_Helpers::fill_container(temp, this->begin(), this->end()); return temp; #endif } template template inline ListHandle::operator std::list() const { #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS return std::list(this->begin(), this->end()); #else std::list temp; Glib::Container_Helpers::fill_container(temp, this->begin(), this->end()); return temp; #endif } template template inline void ListHandle::assign_to(Cont& container) const { #ifdef GLIBMM_HAVE_TEMPLATE_SEQUENCE_CTORS container.assign(this->begin(), this->end()); #else Cont temp; Glib::Container_Helpers::fill_container(temp, this->begin(), this->end()); container.swap(temp); #endif } template template inline void ListHandle::copy(Out pdest) const { std::copy(this->begin(), this->end(), pdest); } template inline GList* ListHandle::data() const { return plist_; } template inline std::size_t ListHandle::size() const { return g_list_length(plist_); } template inline bool ListHandle::empty() const { return (plist_ == nullptr); } #endif /* DOXYGEN_SHOULD_SKIP_THIS */ } // namespace Glib #endif /* _GLIBMM_LISTHANDLE_H */