aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/metadatahandle.h
blob: c4e562ac408bf71ee8c7c3fad7c3171b18f5dc7f (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
// 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    metadatamanager.h
 * @brief   Metadata handle class definition.
 *
 */

#pragma once

#include <string>

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

namespace fsa {

// {{{ class MetaData::Handle

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

private:

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

  class RefCountableMetaData: public MetaData, public RefCountable<MetaData> {
  public:
    RefCountableMetaData(const char *datafile, FileAccessMethod fam = FILE_ACCESS_UNDEF) : MetaData(datafile,fam) {}
  };

  RefCountableMetaData *_metaData; /**< The MetaData object itself. */

public:

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

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

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

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

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

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

  /**
   * @brief Proxy methods
   */
  uint32_t user(unsigned int idx) const
  {
    return _metaData->user(idx);
  }
};

// }}}

} // namespace fsa