-
Notifications
You must be signed in to change notification settings - Fork 5
/
value.h
73 lines (52 loc) · 1.1 KB
/
value.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
73
#ifndef __value_h__
#define __value_h__
#include <string>
#include <cstdint>
// hold a string and number value.
struct value {
public:
std::string string;
int32_t number = 0;
// empty token treated as 0.
value() : status(valid)
{}
value(const value &) = default;
value(value &&) = default;
value(int32_t n) :
string(std::to_string(n)),
number(n),
status(valid)
{}
value(const std::string &s) : string(s)
{}
value(std::string &&s) : string(std::move(s))
{}
value &operator=(const value&) = default;
value &operator=(value &&) = default;
int32_t to_number() {
if (status == unknown)
scan_number();
if (status == valid) return number;
expect_number();
}
int32_t to_number(int default_value) noexcept {
if (status == unknown)
scan_number();
if (status == valid) return number;
return default_value;
}
bool is_number() noexcept {
if (status == unknown)
scan_number();
return status == valid;
}
private:
[[noreturn]] void expect_number() const;
void scan_number() noexcept;
mutable enum {
unknown,
valid,
invalid
} status = unknown;
};
#endif