blob: e4cdc6bb74006729cb0b9b2279e7fcbbfcb8c013 (
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 Yahoo. 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; }
};
}
|