7414029d6109e63250d076f3f43f7c997d146f29
[senf.git] / senf / Utils / Buffer.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Buffer public header */
25
26 #ifndef HH_SENF_Utils_Buffer_
27 #define HH_SENF_Utils_Buffer_ 1
28
29 // Custom includes
30 #include <senf/config.hh>
31
32 //#include "Buffer.mpp"
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34
35 #if defined(SENF_BUFFER_USE_LOCALS)
36
37 #   define SENF_SCOPED_BUFFER(type, sym, size)  \
38         type sym[size];
39
40 #elif defined(SENF_BUFFER_USE_ALLOCA)
41
42 #   include <alloca.h>
43 #   define SENF_SCOPED_BUFFER(type, sym, size)                          \
44         type * sym (static_cast<type *>(alloca(size*sizeof(type))));
45
46 #elif defined(SENF_BUFFER_USE_NEW)
47
48 #   include <boost/scoped_array.hpp>
49     /** \brief Allocate a local buffer
50
51         SENF_SCOPED_BUFFER will allocate a local variable named \a sym as a buffer of \a size elements
52         of type \a type. The buffer will \e not be initialized in any way and \a type must be a POD
53         type.
54
55         This macro is used when \a size is a dynamic expression and not a constant value. For constant
56         values, use \c boost::array (or C++ builtin arrays). Depending on compiler support, this version
57         will try to avoid dynamic memory allocation. The type of the declared local variable \a sym is
58         either pointer to \a type or array of \a type:
59
60         \code
61           #include "../Utils/Buffer.hh"
62
63           void foo(std::string const & str)
64           {
65               // create temp copy of str (like c_str() member)
66               SENF_SCOPED_BUFFER(char, buf, str.size()+1);
67               *std::copy(str.begin(), str.end(), buf) = 0;
68
69               // use buf ...
70           }
71         \endcode
72
73         \param type Type of buffer element
74         \param sym Name of local symbol
75         \param size size of the Area to allocate.
76
77         \hideinitializer
78      */
79 #   define SENF_SCOPED_BUFFER(type, sym, size)                                                          \
80         boost::scoped_array<type> _senf_scoped_buffer__ ## sym ## __ ## __LINE__ (new type[size]);      \
81         type * sym (_senf_scoped_buffer__ ## sym ## __ ## __LINE__.get());
82
83 #endif
84
85 //-/////////////////////////////////////////////////////////////////////////////////////////////////
86 //#include "Buffer.cci"
87 //#include "Buffer.ct"
88 //#include "Buffer.cti"
89 #endif
90
91 \f
92 // Local Variables:
93 // mode: c++
94 // fill-column: 100
95 // c-file-style: "senf"
96 // indent-tabs-mode: nil
97 // ispell-local-dictionary: "american"
98 // compile-command: "scons -u test"
99 // comment-column: 40
100 // End: