3
* ====================================================================
4
* Licensed to the Apache Software Foundation (ASF) under one
5
* or more contributor license agreements. See the NOTICE file
6
* distributed with this work for additional information
7
* regarding copyright ownership. The ASF licenses this file
8
* to you under the Apache License, Version 2.0 (the
9
* "License"); you may not use this file except in compliance
10
* with the License. You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing,
15
* software distributed under the License is distributed on an
16
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
* KIND, either express or implied. See the License for the
18
* specific language governing permissions and limitations
20
* ====================================================================
24
#ifndef SVN_CXXHL_PRIVATE_APRWRAP_ARRAY_H
25
#define SVN_CXXHL_PRIVATE_APRWRAP_ARRAY_H
29
#include <apr_tables.h>
32
#include "svn_private_config.h"
35
namespace subversion {
40
* Proxy for an APR array.
42
* This class does not own the array. The array's lifetime is tied to
43
* its pool. The caller is responsible for making sure that the
44
* array's lifetime is longer than this proxy object's.
46
template<typename T> class Array
50
typedef int size_type;
53
* Create and proxy a new APR array allocated from @a pool.
54
* Reserve space for @a nelts array elements.
56
explicit Array(const Pool& pool, size_type nelts = 0) throw()
57
: m_array(apr_array_make(pool.get(), nelts, sizeof(value_type)))
61
* Create a new proxy for the APR array @a array.
63
explicit Array(apr_array_header_t* array)
66
if (m_array->elt_size != sizeof(value_type))
67
throw std::invalid_argument(
68
_("APR array element size does not match template parameter"));
72
* @return The wrapped APR array.
74
apr_array_header_t* array() const throw()
80
* @return the number of elements in the wrapped APR array.
82
size_type size() const throw()
84
return m_array->nelts;
88
* @return An immutable reference to the array element at @a index.
90
const value_type& operator[](size_type index) const throw()
92
return APR_ARRAY_IDX(m_array, index, value_type);
96
* @return An immutable reference to the array element at @a index.
97
* Like operator[] but perfoms a range check on the index.
99
const value_type& at(size_type index) const
101
if (index < 0 || index >= size())
102
throw std::out_of_range(_("APR array index is out of range"));
103
return (*this)[index];
107
* @return A mutable reference to the array element at @a index.
109
value_type& operator[](size_type index) throw()
111
return APR_ARRAY_IDX(m_array, index, value_type);
115
* @return A mutable reference to the array element at @a index.
116
* Like operator[] but perfoms a range check on the index.
118
value_type& at(size_type index)
120
if (index < 0 || index >= size())
121
throw std::out_of_range(_("APR array index is out of range"));
122
return (*this)[index];
126
* Push @a value onto the end of the APR array.
128
void push(const value_type& value) throw()
130
APR_ARRAY_PUSH(m_array, value_type) = value;
134
* Pop a value from the end of the array.
135
* @return A pointer to the value that was removed, or @c NULL if
136
* the array was empty.
138
value_type* pop() throw()
140
return static_cast<value_type*>(apr_array_pop(m_array));
144
* Abstract base class for mutable iteration callback functors.
149
* Called by Array::iterate for every value in the array.
150
* @return @c false to terminate the iteration, @c true otherwise.
152
virtual bool operator() (value_type& value) = 0;
156
* Iterate over all the values pairs in the array, invoking
157
* @a callback for each one.
159
void iterate(Iteration& callback)
161
for (size_type n = 0; n < size(); ++n)
162
if (!callback((*this)[n]))
167
* Abstract base class for immutable iteration callback functors.
169
struct ConstIteration
172
* Called by Array::iterate for every value in the array.
173
* @return @c false to terminate the iteration, @c true otherwise.
175
virtual bool operator() (const value_type& value) = 0;
179
* Iterate over all the values pairs in the array, invoking
180
* @a callback for each one.
182
void iterate(ConstIteration& callback) const
184
for (size_type n = 0; n < size(); ++n)
185
if (!callback((*this)[n]))
190
apr_array_header_t* const m_array; ///< The wrapperd APR array.
195
* Proxy for an immutable APR array.
198
class ConstArray : private Array<T>
200
typedef Array<T> inherited;
203
typedef typename inherited::value_type value_type;
204
typedef typename inherited::size_type size_type;
207
* Create a new proxy for the APR array wrapped by @a that.
209
ConstArray(const ConstArray& that) throw()
214
* Create a new proxy for the APR array wrapped by @a that.
216
explicit ConstArray(const inherited& that) throw()
221
* Create a new proxy for the APR array @a array.
223
explicit ConstArray(const apr_array_header_t* array)
224
: inherited(const_cast<apr_array_header_t*>(array))
228
* @return The wrapped APR array.
230
const apr_array_header_t* array() const throw()
232
return inherited::array();
236
* @return The number of elements in the wrapped APR array.
238
size_type size() const throw()
240
return inherited::size();
244
* @return An immutable reference to the array element at @a index.
246
const value_type& operator[](size_type index) const throw()
248
return inherited::operator[](index);
252
* @return An immutable reference to the array element at @a index.
253
* Like operator[] but perfoms a range check on the index.
255
const value_type& at(size_type index) const
257
return inherited::at(index);
261
* Abstract base class for immutable iteration callback functors.
263
typedef typename inherited::ConstIteration Iteration;
266
* Iterate over all the values pairs in the array, invoking
267
* @a callback for each one.
269
void iterate(Iteration& callback) const
271
inherited::iterate(callback);
277
} // namespace subversion
278
} // namespace apache
280
#endif // SVN_CXXHL_PRIVATE_APRWRAP_HASH_H