-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.h
118 lines (101 loc) · 2.09 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* MIT License
* Copyright (c) 2019 Anthony Rabine
*/
#ifndef VALUE_H
#define VALUE_H
#include <string>
#include <cstdint>
/*****************************************************************************/
/**
* @brief General purpose any Value
* The currently supported values are:
* \li a double
* \li a string
* \li an integer
* \li a boolean
* \li a null value
*
* Examples of usage:
* - JSON base class
* - JavaScript C++ wrapper API
*
*/
class Value
{
public:
enum Type
{
INVALID,
INTEGER,
DOUBLE,
BOOLEAN,
STRING,
NULL_VAL
};
Value(std::int32_t value);
Value(std::int64_t value);
Value(double value);
Value(const char *value);
Value(const std::string &value);
Value(bool value);
Value(const Value &value);
Value(); // default constructor creates an invalid value!
~Value();
Value &operator = (Value const &rhs);
bool IsValid() const
{
return mType != INVALID;
}
bool IsNull() const
{
return mType == NULL_VAL;
}
Type GetType() const
{
return mType;
}
std::int32_t GetInteger() const
{
return static_cast<int32_t>(mIntegerValue);
}
std::int64_t GetInteger64() const
{
return mIntegerValue;
}
double GetDouble() const
{
return mDoubleValue;
}
bool GetBool() const
{
return mBoolValue;
}
std::string GetString() const
{
return mStringValue;
}
bool IsJsonString() const
{
return mJsonString;
}
void SetJsonString(bool enable)
{
mJsonString = enable;
}
void SetNull()
{
mType = NULL_VAL;
}
private:
Type mType;
std::int64_t mIntegerValue;
double mDoubleValue;
std::string mStringValue;
bool mBoolValue;
bool mJsonString;
};
#endif // VALUE_H
//=============================================================================
// End of file Value.h
//=============================================================================