aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/fsahandle.h
blob: d104d275a37b75183ab2cca80ff6f87b00a0e8b2 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/09/07
 * @version $Id$
 * @file    fsamanager.h
 * @brief   FSA handle class definition.
 *
 */

#pragma once

#include <string>

#include "refcountable.h"
#include <vespa/fsa/fsa.h>

namespace fsa {

// {{{ FSA::Handle

/**
 * @class Handle
 * @brief FSA accessor.
 *
 * A Handle looks like an FSA, but copies are cheap; the actual FSA
 * objects are refcounted and Handle copies merely copy the FSA pointer
 * and increment the refcount.
 */
class FSA::Handle {

private:

  /**
   * @brief Unimplemented private default constructor.
   */
  Handle();
  /**
   * @brief Unimplemented private assignment operator.
   */
  Handle& operator=(const Handle&);

  class RefCountableFSA: public FSA, public RefCountable<FSA> {
  public:
    RefCountableFSA(const char *file, FileAccessMethod fam = FILE_ACCESS_UNDEF) : FSA(file,fam) {}
  };

  RefCountableFSA *_fsa; /**< The FSA object itself. */

  /**
   * @brief Get a pointer to the referred FSA object.
   *
   * @return pointer to the referred FSA object.
   */
  const FSA* getFSA() const
  {
    return _fsa;
  }

public:

  /**
   * @brief Copy constructor.
   *
   * Duplicate a handle (and add new reference to the FSA object.
   *
   * @param h Reference to handle to duplicate.
   */
  Handle(const Handle& h) : _fsa(h._fsa)
  {
    _fsa->addReference();
  }

  /**
   * @brief Constructor.
   *
   * Create a new FSA object (loaded from file) and add reference.
   *
   * @param file Name of the file containing the automaton.
   * @param fam File access mode (read or mmap). If not set, the
   *            global preferred access mode will be used.
   */
  Handle(const char *file, FileAccessMethod fam = FILE_ACCESS_UNDEF) :
    _fsa(new RefCountableFSA(file,fam))
  {
    _fsa->addReference();
  }

  /**
   * @brief Constructor.
   *
   * Create a new FSA object (loaded from file) and add reference.
   *
   * @param file Name of the file containing the automaton.
   * @param fam File access mode (read or mmap). If not set, the
   *            global preferred access mode will be used.
   */
  Handle(const std::string &file, FileAccessMethod fam = FILE_ACCESS_UNDEF) :
    _fsa(new RefCountableFSA(file.c_str(),fam))
  {
    _fsa->addReference();
  }

  /**
   * @brief Destructor.
   *
   * Remove reference to the FSA object.
   */
  ~Handle(void)
  {
    _fsa->removeReference();
  }

  /**
   * @brief Dereference operator, provides access to Metadata
   *        methods.
   *
   * @return Reference to the Metadata object.
   */
  const FSA& operator*() const { return *_fsa; }

  /**
   * @brief Dereference operator, provides access to Metadata
   *        methods.
   *
   * @return Pointer the Metadata object.
   */
  const FSA* operator->() const { return _fsa; }

  /**
   * @brief Check if %FSA was properly constructed.
   *
   * @return true iff underlying %FSA was properly constructed.
   */
  bool isOk(void) const
  {
    return _fsa->isOk();
  }

  /**
   * @brief Get the fsa library version used for building this %FSA.
   *
   * @return fsa library version.
   */
  uint32_t version(void) const
  {
    return _fsa->version();
  }

  /**
   * @brief Get the serial number of the %FSA.
   *
   * @return Serial number.
   */
  uint32_t serial(void) const
  {
    return _fsa->serial();
  }

  /**
   * @brief Check is the automaton has perfect hash built in.
   *
   * Returns true if the automaton was built with a perfect hash included.
   *
   * @return True if the automaton has perfect hash.
   */
  bool hasPerfectHash() const
  {
    return _fsa->hasPerfectHash();
  }

  /**
   * @brief Get iterator pointing to the beginning of the fsa.
   *
   * @return iterator pointing to the first string in the fsa.
   */
  FSA::iterator begin() const { return FSA::iterator(_fsa); }

  /**
   * @brief Get iterator pointing past the end of the fsa.
   *
   * @return iterator pointing past the last string in the fsa.
   */
  FSA::iterator end() const { return FSA::iterator(_fsa,true); }

};

// }}}

} // namespace fsa