Move include files in debian packge into 'senf' subdirectory
[senf.git] / Utils / Buffer.hh
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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_Buffer_
27 #define HH_Buffer_ 1
28
29 // Custom includes
30
31 //#include "Buffer.mpp"
32 ///////////////////////////////hh.p////////////////////////////////////////
33
34 #if !defined(SENF_BUFFER_USE_LOCALS) && !defined(SENF_BUFFER_USE_ALLOCA) && !defined(SENF_BUFFER_USE_NEW)
35 #   
36 #
37 #   if defined(__GNUC__)
38 #       define SENF_BUFFER_USE_LOCALS 1
39 #
40 #   // Add other compilers here ...
41 #
42 #   // dynamic arrays are part of C99. Which is NOT part of C++ 
43 #   // but lets try nonetheless ...
44 #   elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
45 #       define SENF_BUFFER_USE_LOCALS 1
46 #
47 #   endif
48 #
49 #   if !defined(SENF_BUFFER_USE_LOCALS) && !defined(SENF_BUFFER_USE_ALLOCA)
50 #       define SENF_BUFFER_USE_NEW 1
51 #   endif
52 #
53 #endif
54
55 #if defined(SENF_BUFFER_USE_LOCALS)
56
57 #   define SENF_SCOPED_BUFFER(type, sym, size)  \
58         type sym[size];
59
60 #elif defined(SENF_BUFFER_USE_ALLOCA)
61
62 #   include <alloca.h>
63 #   define SENF_SCOPED_BUFFER(type, sym, size)                          \
64         type * sym (static_cast<type *>(alloca(size*sizeof(type))));
65
66 #elif defined(SENF_BUFFER_USE_NEW)
67
68 #   include <boost/scoped_array.hpp>
69     /** \brief Allocate a local buffer
70
71         SENF_SCOPED_BUFFER will allocate a local variable named \a sym as a buffer of \a size elements
72         of type \a type. The buffer will \e not be initialized in any way and \a type must be a POD
73         type.
74
75         This macro is used when \a size is a dynamic expression and not a constant value. For constant
76         values, use \c boost::aray (or C++ builtin arrays). Depending on compiler support, this version
77         will try to avoid dynamic memory allocation. The type of the declared local variable \a sym is
78         either pointer to \a type or array of \a type:
79
80         \code
81           #include "../Utils/Buffer.hh"
82
83           void foo(std::string const & str)
84           {
85               // create temp copy of str (like c_str() member)
86               SENF_SCOPED_BUFFER(char, buf, str.size()+1);
87               *std::copy(str.begin(), str.end(), buf) = 0;
88
89               // use buf ...
90           }
91         \endcode
92
93         \param type Type of buffer element
94         \param sym Name of local symbol
95         \param size size of the Area to allocate.
96
97         \hideinitializer
98      */
99 #   define SENF_SCOPED_BUFFER(type, sym, size)                                                          \
100         boost::scoped_array<type> _senf_scoped_buffer__ ## sym ## __ ## __LINE__ (new type[size]);      \
101         type * sym (_senf_scoped_buffer__ ## sym ## __ ## __LINE__.get());
102
103 #endif
104
105 ///////////////////////////////hh.e////////////////////////////////////////
106 //#include "Buffer.cci"
107 //#include "Buffer.ct"
108 //#include "Buffer.cti"
109 #endif
110
111 \f
112 // Local Variables:
113 // mode: c++
114 // fill-column: 100
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // comment-column: 40
120 // End: