X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FUtils%2FIteratorTraits.hh;fp=senf%2FUtils%2FIteratorTraits.hh;h=bb2fa5a54c9b7b27da23962ef979fec8efd83836;hb=601d1f509f5bb24df167a4dd5a20da67a0af9af8;hp=0000000000000000000000000000000000000000;hpb=164fe477094d42463722584e527a02379ab5d985;p=senf.git diff --git a/senf/Utils/IteratorTraits.hh b/senf/Utils/IteratorTraits.hh new file mode 100644 index 0000000..bb2fa5a --- /dev/null +++ b/senf/Utils/IteratorTraits.hh @@ -0,0 +1,126 @@ +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Stefan Bund +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief IteratorTraits public header */ + +#ifndef HH_SENF_Utils_IteratorTraits_ +#define HH_SENF_Utils_IteratorTraits_ 1 + +// Custom includes +#include +#include +#include + +//#include "IteratorTraits.mpp" +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + +// The following is *not* standard mandated but it *is* mandated by TR1 and I know of no +// implementation for which this is not correct + + /** \brief Check for contiguous mutable storage + + This type trait returns \c true, if \a RandomAccessIterator is an iterator into a contiguous + storage area which may be written to. If this is the case, some algorithms may be optimized + by directly modifying the underlying storage instead of relying on the STL interface. + + \code + // Generic algorithm + template + void do(Iterator i, boost::false_type) + { + // Access the iterator 'i' via the standard STL interface + } + + template + void do(Iterator i, boost::true_type) + { + typename Iterator::pointer p (senf::storage_iterator(i)); + // Manipulate the container by manipulating the data pointed at via 'p' + } + + template + void foo(Iterator i) + { + // ... + do( i, senf::contiguous_storage_iterator() ); + // ... + } + \endcode + + Thie \ref senf::storage_iterator helper function will convert an iterator to a pointer to + the same element the iterator is referencing. + + This trait will return \c true for pointers. Additonally it should be configured to return + true for all standard containers which obey above implementation restrictions. This + typically includes \c std::vector and \c std::basic_string. + + To do so, the template must be specialized for those containers \c iterator type. If + compiling with g++, this is implemented in \ref IteratorTraits.ih. This file should be + extended for further compilers or STL implementations if needed. + */ + template + struct contiguous_storage_iterator + : public boost::false_type + {}; + + /** \brief Check for contiguous mutable storage. Pointer specialization + + See \ref contiguous_storage_iterator. + */ + template + struct contiguous_storage_iterator + : public boost::true_type + {}; + + /** \brief Convert contiguous storage iterator to pointer + + storage_iterator will convert a contiguous storage iterator into a pointer to the same + element in the container. This allows to directly access the containers storage. + + \warning This conversion is only safe if \ref contiguous_storage_iterator::value + is \c true for the given iterator type ! + */ + template + typename std::iterator_traits::pointer storage_iterator(Iterator i); + +} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "IteratorTraits.cci" +//#include "IteratorTraits.ct" +#include "IteratorTraits.cti" +#include "IteratorTraits.ih" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 +// End: