summaryrefslogtreecommitdiff
path: root/subversion/bindings/cxx/src/aprwrap/hash.hpp
blob: 8272803ebc829c80128ee07c8ff233ef81cd8b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
 * @copyright
 * ====================================================================
 *    Licensed to the Apache Software Foundation (ASF) under one
 *    or more contributor license agreements.  See the NOTICE file
 *    distributed with this work for additional information
 *    regarding copyright ownership.  The ASF licenses this file
 *    to you under the Apache License, Version 2.0 (the
 *    "License"); you may not use this file except in compliance
 *    with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing,
 *    software distributed under the License is distributed on an
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *    KIND, either express or implied.  See the License for the
 *    specific language governing permissions and limitations
 *    under the License.
 * ====================================================================
 * @endcopyright
 */

#ifndef SVNXX_PRIVATE_APRWRAP_HASH_H
#define SVNXX_PRIVATE_APRWRAP_HASH_H

#include <apr_hash.h>
#include "pool.hpp"

namespace apache {
namespace subversion {
namespace svnxx {
namespace apr {

// Template forward declaration
template<typename T, typename V, apr_ssize_t KeySize = APR_HASH_KEY_STRING>
class Hash;

/**
 * Proxy for an APR hash table.
 * This is a template specialization for the default hash type.
 */
template<>
class Hash<void, void>
{
public:
  struct Iteration;

  /**
   * Iterate over all the key-value pairs in the hash table, invoking
   * @a callback for each pair.
   * Uses @a scratch_pool for temporary allocations.
   *
   * FIXME: should use std::function instead.
   */
  void iterate(Iteration& callback, const pool& scratch_pool);

public:
  /**
   * Proxy for a key in an APR hash table.
   * This is a template specialization for the default hash key type.
   */
  class Key
  {
  public:
    typedef const void* key_type;
    typedef apr_ssize_t size_type;

    /**
     * Public constructor. Uses the template @a Size parameter.
     */
    Key(key_type key) throw()
      : m_key(key), m_size(APR_HASH_KEY_STRING)
      {}

    /**
     * Get the value of the key.
     */
    key_type get() const throw() { return m_key; }

    /**
     * Get the size of the key.
     */
    size_type size() const throw() { return m_size; }

  protected:
    /**
     * Constructor used by the generic template, specializations and
     * hash table wrapper. Does not make assumptions about the key size.
     */
    Key(key_type key, size_type size) throw()
      : m_key(key), m_size(size)
      {}

    /**
     * The hash table wrapper must be able to call the protected constructor.
     */
    friend void Hash::iterate(Hash::Iteration&, const pool&);

  private:
    const key_type m_key;       ///< Immutable reference to the key
    const size_type m_size;     ///< The size of the key
  };

public:
  typedef Key::key_type key_type;
  typedef void* value_type;
  typedef unsigned int size_type;

  /**
   * Create and proxy a new APR hash table in @a result_pool.
   */
  explicit Hash(const pool& result_pool) throw()
    : m_hash(apr_hash_make(result_pool.get()))
    {}

  /**
   * Create and proxy a new APR hash table in @a result_pool, using @a
   * hash_func as the hash function.
   */
  explicit Hash(const pool& result_pool, apr_hashfunc_t hash_func) throw()
    : m_hash(apr_hash_make_custom(result_pool.get(), hash_func))
    {}

  /**
   * Create a proxy for the APR hash table @a hash.
   */
  explicit Hash(apr_hash_t* hash)
    : m_hash(hash)
    {}

  /**
   * Return the wrapped APR hash table.
   */
  apr_hash_t* hash() const throw()
    {
      return m_hash;
    }

  /**
   * Return the number of key-value pairs in the wrapped hash table.
   */
  size_type size() const throw()
    {
      return apr_hash_count(m_hash);
    }

  /**
   * Set @a key = @a value in the wrapped hash table.
   */
  void set(const Key& key, value_type value) throw()
    {
      apr_hash_set(m_hash, key.get(), key.size(), value);
    }

  /**
   * Retrieve the value associated with @a key.
   */
  value_type get(const Key& key) const throw()
    {
      return apr_hash_get(m_hash, key.get(), key.size());
    }

  /**
   * Delete the entry for @a key.
   */
  void del(const Key& key) throw()
    {
      apr_hash_set(m_hash, key.get(), key.size(), NULL);
    }

  /**
   * Abstract base class for iteration callback functors.
   */
  struct Iteration
  {
    /**
     * Called by Hash::iterate for every key-value pair in the hash table.
     * @return @c false to terminate the iteration, @c true otherwise.
     */
    virtual bool operator() (const Key& key, value_type value) = 0;
  };

protected:
  typedef const void* const_value_type;

  /**
   * Set @a key = @a value in the wrapped hash table.  Overloaded for
   * deroved template instantiations with constant values; for
   * example, Hash<char, const char>.
   */
  void set(const Key& key, const_value_type const_value) throw()
    {
      set(key, const_cast<value_type>(const_value));
    }

private:
  apr_hash_t* const m_hash;     ///< The wrapped APR hash table.
};


/**
 * Proxy for an APR hash table: the template.
 *
 * This class does not own the hash table. The hash table's lifetime
 * is tied to its pool. The caller is responsible for making sure that
 * the hash table's lifetime is longer than this proxy object's.
 */
template<typename K, typename V, apr_ssize_t KeySize>
class Hash : private Hash<void, void>
{
  typedef Hash<void, void> inherited;

public:
  /**
   * Proxy for a key in an APR hash table.
   *
   * This class does not own the key; it is the caller's responsibility
   * to make sure that the key's lifetime is longer than the proxy
   * object's.
   */
  class Key : private inherited::Key
  {
    typedef Hash<void, void>::Key inherited;

    /**
     * The wrapper must be able to call the private constructor and
     * convert references to the base class.
     */
    friend class Hash;

  public:
    typedef const K* key_type;
    typedef inherited::size_type size_type;

    Key(key_type key) throw()
      : inherited(key, KeySize)
      {}

    /**
     * Get the value of the key.
     */
    key_type get() const throw()
      {
        return static_cast<key_type>(inherited::get());
      }

    /**
     * Get the size of the key.
     */
    size_type size() const throw()
      {
        return inherited::size();
      }

  private:
    /**
     * Conversion constructor used by the derived iteration class.
     */
    explicit Key(const inherited& that) throw()
      : inherited(that)
      {}
  };

public:
  typedef typename Key::key_type key_type;
  typedef V* value_type;

  /**
   * Create and proxy a new APR hash table allocated from @a result_pool.
   */
  explicit Hash(const pool& result_pool) throw()
    : inherited(result_pool)
    {}

  /**
   * Create and proxy a new APR hash table allocated from @a result_pool,
   * using @a hash_func as the hash function.
   */
  explicit Hash(const pool& result_pool, apr_hashfunc_t hash_func) throw()
    : inherited(result_pool, hash_func)
    {}

  /**
   * Create a proxy for the APR hash table @a hash.
   */
  explicit Hash(apr_hash_t* hash)
    : inherited(hash)
    {}

  /**
   * @return The wrapped APR hash table.
   */
  apr_hash_t* hash() const throw()
    {
      return inherited::hash();
    }

  /**
   * Return the number of key-value pairs in the wrapped hash table.
   */
  size_type size() const throw()
    {
      return inherited::size();
    }

  /**
   * Set @a key = @a value in the wrapped hash table.
   */
  void set(const Key& key, value_type value) throw()
    {
      inherited::set(inherited::Key(key), value);
    }

  /**
   * Retrieve the value associated with @a key.
   */
  value_type get(const Key& key) const throw()
    {
      return static_cast<value_type>(inherited::get(inherited::Key(key)));
    }

  /**
   * Delete the entry for @a key.
   */
  void del(const Key& key) throw()
    {
      inherited::del(inherited::Key(key));
    }

  /**
   * Abstract base class for iteration callback functors.
   */
  struct Iteration : protected inherited::Iteration
  {
    /**
     * Called by Hash::iterate for every key-value pair in the hash table.
     * @return @c false to terminate the iteration, @c true otherwise.
     */
    virtual bool operator() (const Key& key, value_type value) = 0;

  private:
    friend void Hash::iterate(Iteration& callback, const pool& scratch_pool);

    /**
     * Implementation of the derived virtual operator().
     * Adapts the callback to the instantiated types.
     */
    virtual bool operator() (const inherited::Key& raw_key,
                             inherited::value_type raw_value)
      {
        return (*this)(Key(raw_key), static_cast<value_type>(raw_value));
      }
  };

  /**
   * Iterate over all the key-value pairs in the hash table, invoking
   * @a callback for each pair.
   * Uses @a scratch_pool for temporary allocations.
   */
  void iterate(Iteration& callback, const pool& scratch_pool)
    {
      inherited::iterate(callback, scratch_pool);
    }
};


} // namespace apr
} // namespace svnxx
} // namespace subversion
} // namespace apache

#endif // SVNXX_PRIVATE_APRWRAP_HASH_H