aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/metrics/handle.h
blob: b901f56af7e78b3332b97dd795f6583b68091626 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <cstddef>

namespace vespalib {
namespace metrics {

/**
 * Common implementation of an opaque handle identified only
 * by a (64-bit) integer.  Templated to avoid different concepts
 * sharing a superclass.
 **/
template <typename T>
class Handle {
private:
    size_t _id;
    constexpr Handle() : _id(0) {}
public:
    explicit Handle(size_t id) : _id(id) {}
    size_t id() const { return _id; }

    static const Handle empty_handle;
};

template <typename T>
const Handle<T> Handle<T>::empty_handle;

template <typename T>
bool
operator< (const Handle<T> &a, const Handle<T> &b) noexcept
{
    return a.id() < b.id();
}

template <typename T>
bool
operator> (const Handle<T> &a, const Handle<T> &b) noexcept
{
    return a.id() > b.id();
}

template <typename T>
bool
operator== (const Handle<T> &a, const Handle<T> &b) noexcept
{
    return a.id() == b.id();
}

template <typename T>
bool
operator!= (const Handle<T> &a, const Handle<T> &b) noexcept
{
    return a.id() != b.id();
}

} // namespace vespalib::metrics
} // namespace vespalib