~ubuntu-branches/ubuntu/quantal/aqsis/quantal

« back to all changes in this revision

Viewing changes to include/aqsis/util/autobuffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-08-06 04:53:26 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806045326-z6xeaaao62idxcc6
Tags: 1.6.0-0ubuntu1
* New upstream release
* debian/control:
  - changed name of lib package to libaqsis1 instead of aqsis-libsc2a
  - changed name of dev package to libaqsis-dev instead of aqsis-libs-dev
  - Added aqsis-data package
  - Revised summary text according to that specified by the RISpec (Pixar)
* Moved examples installation from aqsis.install to aqsis-data.install
* debian/rules: 
  - added content to binary-indep target
* debian/rules: added explicit name of mime file to force dh_installmime
  to generate postinst and prerm scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright (C) 1997 - 2007, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.org
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library 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 GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
/** \file
 
21
 *
 
22
 * \brief Declare a variable sized buffer class containing an internal
 
23
 * statically-sized array for commonly allocated array sizes.
 
24
 *
 
25
 * \author Chris Foster [ chris42f (at) gmail (dot) com ]
 
26
 */
 
27
 
 
28
#ifndef AUTOBUFFER_H_INCLUDED
 
29
#define AUTOBUFFER_H_INCLUDED
 
30
 
 
31
#include <aqsis/aqsis.h>
 
32
 
 
33
#include <boost/scoped_array.hpp>
 
34
 
 
35
namespace Aqsis {
 
36
 
 
37
/** \brief Variable size buffer class which allocates on the stack if possible.
 
38
 *
 
39
 * Sometimes we want to allocate an array on the program stack, but the size of
 
40
 * that array isn't known at compile time.  Unfortunately there is no way to do
 
41
 * this portably in standard C++.
 
42
 *
 
43
 * However, there is often a rough bound on the amount of space required and we
 
44
 * can allocate this expected amount on the stack, and fall back to using the
 
45
 * heap if necessary.  The CqAutoBuffer class implements this strategy.
 
46
 *
 
47
 * This idea has been implemented in other software (undoubtably many times);
 
48
 * stlsoft::auto_buffer one such example and the one from which this class
 
49
 * takes its name.  Something like this will probably make its way into boost
 
50
 * at some stage...
 
51
 *
 
52
 * Note also that this is a portable approximate replacement for the alloca()
 
53
 * function.
 
54
 *
 
55
 * T - type to hold in the buffer
 
56
 * defaultBufSize - Size of the internal buffer allocated with the object
 
57
 *                  (allocated on the stack whenever the buffer instance itself
 
58
 *                  is).  Any runtime requested sizes which are larger than
 
59
 *                  this will result in a heap allocation.
 
60
 */
 
61
template<typename T, int defaultBufSize>
 
62
class CqAutoBuffer
 
63
{
 
64
        public:
 
65
                /** \brief Construct an autobuffer
 
66
                 *
 
67
                 * \param size - size of the array of T.
 
68
                 */
 
69
                CqAutoBuffer(TqInt size);
 
70
 
 
71
                /** \brief Construct an autobuffer and initialize elements.
 
72
                 *
 
73
                 * \param size - size of the array of T.
 
74
                 * \param defaultVal - value to initialalize the buffer with.
 
75
                 */
 
76
                CqAutoBuffer(TqInt size, const T& defaultVal);
 
77
 
 
78
                /// Array indexing operators
 
79
                T& operator[](TqInt i);
 
80
                const T& operator[](TqInt i) const;
 
81
 
 
82
                /// Get a pointer to the start of the underlying buffer.
 
83
                T* get();
 
84
                const T* get() const;
 
85
 
 
86
                /// Get the buffer size
 
87
                TqInt size() const;
 
88
        private:
 
89
                /// Fixed-size array of the given expected size.
 
90
                T m_defaultBuf[defaultBufSize];
 
91
                /// Heap-allocated array, used if we need more than defaultBufSize of memory.
 
92
                boost::scoped_array<T> m_heapBuf;
 
93
                /// Pointer to the buffer in use.
 
94
                T* m_buf;
 
95
                /// Size of the buffer.
 
96
                TqInt m_size;
 
97
};
 
98
 
 
99
 
 
100
 
 
101
//==============================================================================
 
102
// Implementation details
 
103
//==============================================================================
 
104
 
 
105
template<typename T, int defaultBufSize>
 
106
inline CqAutoBuffer<T, defaultBufSize>::CqAutoBuffer(TqInt size)
 
107
        : m_heapBuf(size < defaultBufSize ? 0 : new T[size]),
 
108
        m_buf(size < defaultBufSize ? m_defaultBuf : m_heapBuf.get()),
 
109
        m_size(size)
 
110
{ }
 
111
 
 
112
template<typename T, int defaultBufSize>
 
113
inline CqAutoBuffer<T, defaultBufSize>::CqAutoBuffer(TqInt size, const T& defaultVal)
 
114
        : m_heapBuf(size < defaultBufSize ? 0 : new T[size]),
 
115
        m_buf(size < defaultBufSize ? m_defaultBuf : m_heapBuf.get()),
 
116
        m_size(size)
 
117
{
 
118
        for(TqInt i = 0; i < size; ++i)
 
119
                m_buf[i] = defaultVal;
 
120
}
 
121
 
 
122
/// Indexing
 
123
template<typename T, int defaultBufSize>
 
124
inline T& CqAutoBuffer<T, defaultBufSize>::operator[](TqInt i)
 
125
{
 
126
        return m_buf[i];
 
127
}
 
128
template<typename T, int defaultBufSize>
 
129
inline const T& CqAutoBuffer<T, defaultBufSize>::operator[](TqInt i) const
 
130
{
 
131
        return m_buf[i];
 
132
}
 
133
 
 
134
/// Get at the underlying buffer.
 
135
template<typename T, int defaultBufSize>
 
136
inline T* CqAutoBuffer<T, defaultBufSize>::get()
 
137
{
 
138
        return m_buf;
 
139
}
 
140
template<typename T, int defaultBufSize>
 
141
inline const T* CqAutoBuffer<T, defaultBufSize>::get() const
 
142
{
 
143
        return m_buf;
 
144
}
 
145
 
 
146
template<typename T, int defaultBufSize>
 
147
inline TqInt CqAutoBuffer<T, defaultBufSize>::size() const
 
148
{
 
149
        return m_size;
 
150
}
 
151
 
 
152
 
 
153
} // namespace Aqsis
 
154
 
 
155
#endif // AUTOBUFFER_H_INCLUDED