aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/componentguard.h
blob: 6f251ac4856a7c138c4ddc9bdc8b2000ca7c3784 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vespa/vespalib/util/generationhandler.h>
#include <memory>

namespace search {

/**
 * General class for guarding a component that is using an underlying generation handler.
 **/
template <typename T>
class ComponentGuard
{
private:
    using Guard = vespalib::GenerationHandler::Guard;
    using Component = std::shared_ptr<T>;
    Component  _component;
    Guard      _generationGuard;
public:
    ComponentGuard();
    ComponentGuard(ComponentGuard &&);
    ComponentGuard & operator = (ComponentGuard &&);
    ComponentGuard(const ComponentGuard &);
    ComponentGuard & operator = (const ComponentGuard &);
    virtual ~ComponentGuard();
    /**
     * Creates a guard for the shared pointer of the given component.
     **/
    ComponentGuard(const Component & component);
    const T * get()          const { return _component.get(); }

    const Component & getSP() const { return _component; }
    const T * operator -> () const { return _component.get(); }
    const T & operator * ()  const { return *_component.get(); }
    T * get()                      { return _component.get(); }
    T * operator -> ()             { return _component.get(); }
    T & operator * ()              { return *_component.get(); }
    bool valid()             const { return _component.get() != NULL; }
};

}