From: g0dil Date: Wed, 6 Jun 2007 08:07:17 +0000 (+0000) Subject: Implement compiler independent dynamic local buffers (Buffer.hh) X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=36dec8105bf99dec12ffa07149f72923a71001e5;p=senf.git Implement compiler independent dynamic local buffers (Buffer.hh) Implement container storage traits (IteratorTraits.hh) git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@249 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Utils/Buffer.hh b/Utils/Buffer.hh new file mode 100644 index 0000000..00021d2 --- /dev/null +++ b/Utils/Buffer.hh @@ -0,0 +1,116 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// 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 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 +# define SENF_SCOPED_BUFFER(type, sym, size) \ + type * sym (static_cast(alloca(size*sizeof(type)))); + +#elif defined(SENF_BUFFER_USE_NEW) + +# include + /** \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 _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 + + +// 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 index 0000000..9dcf070 --- /dev/null +++ b/Utils/IteratorTraits.cti @@ -0,0 +1,49 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// 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 inline template implementation */ + +//#include "IteratorTraits.ih" + +// Custom includes +#include + +#define prefix_ inline +///////////////////////////////cti.p/////////////////////////////////////// + +template +prefix_ std::iterator_traits::pointer senf::storage_iterator(Iterator i) +{ + return boost::addressof(*i); +} + + +///////////////////////////////cti.e/////////////////////////////////////// +#undef prefix_ + + +// 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 index 0000000..6bc00bf --- /dev/null +++ b/Utils/IteratorTraits.hh @@ -0,0 +1,107 @@ +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// 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_IteratorTraits_ +#define HH_IteratorTraits_ 1 + +// Custom includes +#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 + +#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 + struct contiguous_storage_iterator + : public boost::false_type + {}; + + template + struct contiguous_storage_iterator + : public boost::true_type + {}; + +#if defined(SENF_VECTOR_IS_CONTIGUOUS) + template + struct contiguous_storage_iterator< typename std::vector::iterator > + : public boost::true_type + {}; +#endif + +#if defined(SENF_STRING_IS_CONTIGUOUS) + template + struct contiguous_storage_iterator< typename std::basic_string::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 + typename std::iterator_traits::pointer storage_iterator(Iterator i); + +} + +///////////////////////////////hh.e//////////////////////////////////////// +//#include "IteratorTraits.cci" +//#include "IteratorTraits.ct" +//#include "IteratorTraits.cti" +#endif + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// End: diff --git a/Utils/Mainpage.dox b/Utils/Mainpage.dox index 9661e9c..e5b53dd 100644 --- a/Utils/Mainpage.dox +++ b/Utils/Mainpage.dox @@ -32,6 +32,8 @@ namespace senf {
prettyName()
an interface to the C++ demangler of g++ to get formated type names from typeinfo objects
+
\ref SENF_SCOPED_BUFFER
a portable way to efficiently allocate temporary buffers
+ */