aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/conceptnethandle.h
blob: a96b232290604e1867ac84a8efce25f543dcaa5f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/10/01
 * @version $Id$
 * @file    conceptnetmanager.h
 * @brief   Concept network handle class definition.
 *
 */

#pragma once

#include <string>

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

namespace fsa {

// {{{ class ConceptNet::Handle

/**
 * @class Handle
 * @brief Concept net handle.
 *
 * A Handle looks like a ConceptNet, but copies are cheap; the actual
 * ConceptNet objects are refcounted and Handle copies merely copy the
 * ConceptNet pointer and increment the refcount.
 */
class ConceptNet::Handle {

private:

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

  class RefCountableConceptNet: public ConceptNet, public RefCountable<ConceptNet> {
  public:
    RefCountableConceptNet(const char *fsafile, const char *datafile=NULL, FileAccessMethod fam = FILE_ACCESS_UNDEF) : ConceptNet(fsafile,datafile,fam) {}
  };

  RefCountableConceptNet *_conceptNet; /**< The ConceptNet object itself. */

public:

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

  /**
   * @brief Constructor.
   *
   * @param fsafile %FSA file containing the units, with a perfect has
   *                (used for indexing the data file).
   * @param datafile Concept net data file.
   * @param fam File access mode (read or mmap). If not set, the
   *            global preferred access mode will be used.
   */
  Handle(const char *fsafile, const char *datafile=NULL, FileAccessMethod fam = FILE_ACCESS_UNDEF) :
    _conceptNet(new RefCountableConceptNet(fsafile,datafile,fam))
  {
    _conceptNet->addReference();
  }

  /**
   * @brief Constructor.
   *
   * @param fsafile %FSA file containing the units, with a perfect has
   *                (used for indexing the data file).
   * @param datafile Concept net data file.
   * @param fam File access mode (read or mmap). If not set, the
   *            global preferred access mode will be used.
   */
  Handle(const std::string &fsafile, const std::string &datafile=NULL, FileAccessMethod fam = FILE_ACCESS_UNDEF) :
    _conceptNet(new RefCountableConceptNet(fsafile.c_str(),datafile.c_str(),fam))
  {
    _conceptNet->addReference();
  }

  /**
   * @brief Destructor.
   */
  ~Handle(void)
  {
    _conceptNet->removeReference();
  }

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

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

};

// }}}

} // namespace fsa