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