-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathoptval.h
72 lines (60 loc) · 1.12 KB
/
optval.h
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
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#include <cstdint>
#include <stdexcept>
class OptVal {
typedef uint64_t value_t;
private:
value_t value;
bool set;
public:
OptVal() :
value(0), set(false)
{
}
OptVal(value_t value) :
value(value), set(true)
{
}
void reset()
{
value = 0;
set = false;
}
operator bool() const {
return set;
}
operator uint64_t() const {
if (set) {
return value;
} else {
throw std::runtime_error("unset optional value unwrapped");
}
}
value_t operator =(value_t rhs) {
set = true;
value = rhs;
return value;
}
value_t operator +=(value_t rhs) {
set = true;
value += rhs;
return value;
}
std::string to_string() const
{
if (set) {
return std::to_string(value);
} else {
return "-";
}
}
};