switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Buffer.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Buffer public header */
30
31 #ifndef HH_SENF_Utils_Buffer_
32 #define HH_SENF_Utils_Buffer_ 1
33
34 // Custom includes
35 #include <senf/config.hh>
36
37 //#include "Buffer.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 #if defined(SENF_BUFFER_USE_LOCALS)
41
42 #   define SENF_SCOPED_BUFFER(type, sym, size)  \
43         type sym[size];
44
45 #elif defined(SENF_BUFFER_USE_ALLOCA)
46
47 #   include <alloca.h>
48 #   define SENF_SCOPED_BUFFER(type, sym, size)                          \
49         type * sym (static_cast<type *>(alloca(size*sizeof(type))));
50
51 #elif defined(SENF_BUFFER_USE_NEW)
52
53 #   include <boost/scoped_array.hpp>
54     /** \brief Allocate a local buffer
55
56         SENF_SCOPED_BUFFER will allocate a local variable named \a sym as a buffer of \a size elements
57         of type \a type. The buffer will \e not be initialized in any way and \a type must be a POD
58         type.
59
60         This macro is used when \a size is a dynamic expression and not a constant value. For constant
61         values, use \c boost::array (or C++ builtin arrays). Depending on compiler support, this version
62         will try to avoid dynamic memory allocation. The type of the declared local variable \a sym is
63         either pointer to \a type or array of \a type:
64
65         \code
66           #include "../Utils/Buffer.hh"
67
68           void foo(std::string const & str)
69           {
70               // create temp copy of str (like c_str() member)
71               SENF_SCOPED_BUFFER(char, buf, str.size()+1);
72               *std::copy(str.begin(), str.end(), buf) = 0;
73
74               // use buf ...
75           }
76         \endcode
77
78         \param type Type of buffer element
79         \param sym Name of local symbol
80         \param size size of the Area to allocate.
81
82         \hideinitializer
83      */
84 #   define SENF_SCOPED_BUFFER(type, sym, size)                                                          \
85         boost::scoped_array<type> _senf_scoped_buffer__ ## sym ## __ ## __LINE__ (new type[size]);      \
86         type * sym (_senf_scoped_buffer__ ## sym ## __ ## __LINE__.get());
87
88 #endif
89
90 //-/////////////////////////////////////////////////////////////////////////////////////////////////
91 //#include "Buffer.cci"
92 //#include "Buffer.ct"
93 //#include "Buffer.cti"
94 #endif
95
96 \f
97 // Local Variables:
98 // mode: c++
99 // fill-column: 100
100 // c-file-style: "senf"
101 // indent-tabs-mode: nil
102 // ispell-local-dictionary: "american"
103 // compile-command: "scons -u test"
104 // comment-column: 40
105 // End: