summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/vespa/vespalib/util/varholder.h
blob: 26f8f57839ac999764164b9e3191835f6e58b1c4 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/util/noncopyable.hpp>
#include <vespa/vespalib/util/sync.h>

namespace vespalib
{


template <typename T>
class VarHolder : public noncopyable
{

    T			_v;
    vespalib::Lock      _lock;

public:
    VarHolder(void)
        : _v(),
          _lock()
    {
    }

    explicit
    VarHolder(const T &v)
        : _v(v),
          _lock()
    {
    }

    ~VarHolder(void)
    {
    }

    void
    set(const T &v)
    {
        T old;
        {
            vespalib::LockGuard guard(_lock);
            old = _v;
            _v = v;
        }
    }

    void
    clear(void)
    {
        set(T());
    }

    T
    get(void) const
    {
        vespalib::LockGuard guard(_lock);
        return _v;
    }
};

}  // namespace vespalib