Implement compiler independent dynamic local buffers (Buffer.hh)
g0dil [Wed, 6 Jun 2007 08:07:17 +0000 (08:07 +0000)]
Implement container storage traits (IteratorTraits.hh)

git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@249 270642c3-0616-0410-b53a-bc976706d245

Utils/Buffer.hh [new file with mode: 0644]
Utils/IteratorTraits.cti [new file with mode: 0644]
Utils/IteratorTraits.hh [new file with mode: 0644]
Utils/Mainpage.dox

diff --git a/Utils/Buffer.hh b/Utils/Buffer.hh
new file mode 100644 (file)
index 0000000..00021d2
--- /dev/null
@@ -0,0 +1,116 @@
+// Copyright (C) 2007 
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <g0dil@berlios.de>
+//
+// 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 Buffer public header */
+
+#ifndef HH_Buffer_
+#define HH_Buffer_ 1
+
+// Custom includes
+
+//#include "Buffer.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+#if !defined(SENF_BUFFER_USE_LOCALS) && !defined(SENF_BUFFER_USE_ALLOCA) && !defined(SENF_BUFFER_USE_NEW)
+#   
+#
+#   if defined(__GNUC__)
+#       define SENF_BUFFER_USE_LOCALS 1
+#
+#   // Add other compilers here ...
+#
+#   // dynamic arrays are part of C99. Which is NOT part of C++ 
+#   // but lets try nonetheless ...
+#   elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+#       define SENF_BUFFER_USE_LOCALS 1
+#
+#   endif
+#
+#   if !defined(SENF_BUFFER_USE_LOCALS) && !defined(SENF_BUFFER_USE_ALLOCA)
+#       define SENF_BUFFER_USE_NEW 1
+#   endif
+#
+#endif
+
+#if defined(SENF_BUFFER_USE_LOCALS)
+
+#   define SENF_SCOPED_BUFFER(type, sym, size)  \
+        type sym[size];
+
+#elif defined(SENF_BUFFER_USE_ALLOCA)
+
+#   include <alloca.h>
+#   define SENF_SCOPED_BUFFER(type, sym, size)                          \
+        type * sym (static_cast<type *>(alloca(size*sizeof(type))));
+
+#elif defined(SENF_BUFFER_USE_NEW)
+
+#   include <boost/scoped_array.hpp>
+    /** \brief Allocate a local buffer
+
+        SENF_SCOPED_BUFFER will allocate a local variable named \a sym as a buffer of \a size elements
+        of type \a type. The buffer will \e not be initialized in any way and \a type must be a POD
+        type.
+
+        This macro is used when \a size is a dynamic expression and not a constant value. For constant
+        values, use \c boost::aray (or C++ builtin arrays). Depending on compiler support, this version
+        will try to avoid dynamic memory allocation. The type of the declared local variable \a sym is
+        either pointer to \a type or array of \a type:
+
+        \code
+          #include "Utils/Buffer.hh"
+
+          void foo(std::string const & str)
+          {
+              // create temp copy of str (like c_str() member)
+              SENF_SCOPED_BUFFER(char, buf, str.size()+1);
+              *std::copy(str.begin(), str.end(), buf) = 0;
+
+              // use buf ...
+          }
+        \endcode
+
+        \param type Type of buffer element
+        \param sym Name of local symbol
+        \param size size of the Area to allocate.
+
+        \hideinitializer
+     */
+#   define SENF_SCOPED_BUFFER(type, sym, size)                                                          \
+        boost::scoped_array<type> _senf_scoped_buffer__ ## sym ## __ ## __LINE__ (new type[size]);      \
+        type * sym (_senf_scoped_buffer__ ## sym ## __ ## __LINE__.get());
+
+#endif
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "Buffer.cci"
+//#include "Buffer.ct"
+//#include "Buffer.cti"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// End:
diff --git a/Utils/IteratorTraits.cti b/Utils/IteratorTraits.cti
new file mode 100644 (file)
index 0000000..9dcf070
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright (C) 2007 
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <g0dil@berlios.de>
+//
+// 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 inline template implementation */
+
+//#include "IteratorTraits.ih"
+
+// Custom includes
+#include <boost/utility.hpp>
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+template <class Iterator>
+prefix_ std::iterator_traits<Iterator>::pointer senf::storage_iterator(Iterator i)
+{
+    return boost::addressof(*i);
+}
+
+
+///////////////////////////////cti.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// End:
diff --git a/Utils/IteratorTraits.hh b/Utils/IteratorTraits.hh
new file mode 100644 (file)
index 0000000..6bc00bf
--- /dev/null
@@ -0,0 +1,107 @@
+// Copyright (C) 2007 
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Stefan Bund <g0dil@berlios.de>
+//
+// 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_IteratorTraits_
+#define HH_IteratorTraits_ 1
+
+// Custom includes
+#include <vector>
+#include <string>
+
+//#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
+
+#define SENF_VECTOR_IS_CONTIGUOUS 1
+#define SENF_STRING_IS_CONTIGUOUS 1
+    
+    /** \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.
+        
+        This trait is predefined to return \c true for pointers and for the iterators of \c
+        std::vector and \c std::basic_string (and so for \c std::string and \c std::wstring). This
+        is \e not required by the current standard. It is however required for \c std::vector in the
+        first corrigendum to the standard, TR1. Furthermore almost all implementations for \c
+        std::vector do follow this approach. 
+
+        For \c std::string the case is different, there are libraries which use reference counting
+        and shared ownership for strings, however no library with which SENF has been tested to date
+        has strings of this variety. If SENF is used with such a standard library implementation,
+        this header has to be adjysted to define the preprocessor symbol \c
+        SENF_STRING_IS_CONTIGUOUS accordingly.
+     */
+    template <class RandomAccessIterator>
+    struct contiguous_storage_iterator
+        : public boost::false_type
+    {};
+
+    template <class T>
+    struct contiguous_storage_iterator<T *>
+        : public boost::true_type
+    {};
+
+#if defined(SENF_VECTOR_IS_CONTIGUOUS)
+    template <class T, class Alloc>
+    struct contiguous_storage_iterator< typename std::vector<T,Alloc>::iterator >
+        : public boost::true_type
+    {};
+#endif
+
+#if defined(SENF_STRING_IS_CONTIGUOUS)
+    template <class CharT, class Traits, class Alloc>
+    struct contiguous_storage_iterator< typename std::basic_string<CharT, Traits, Alloc>::iterator >
+        : public boost::true_type
+    {};
+#endif
+
+    /** \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.
+     */
+    template <class Iterator>
+    typename std::iterator_traits<Iterator>::pointer storage_iterator(Iterator i);
+    
+}
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "IteratorTraits.cci"
+//#include "IteratorTraits.ct"
+//#include "IteratorTraits.cti"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// End:
index 9661e9c..e5b53dd 100644 (file)
@@ -32,6 +32,8 @@ namespace senf {
     <dt>prettyName()</dt><dd>an interface to the C++ demangler of g++
     to get formated type names from typeinfo objects</dd>
 
+    <dt>\ref SENF_SCOPED_BUFFER</dt><dd>a portable way to efficiently allocate temporary buffers</dd>
+
     </dl>
  */