14 #include <type_traits>
17 #include "llvm/ArrayRef.h"
18 #include "llvm/StringRef.h"
29 struct private_init {};
33 Value(NT_Type type,
const private_init&);
36 NT_Type type()
const {
return m_val.type; }
37 const NT_Value& value()
const {
return m_val; }
38 unsigned long long last_change()
const {
return m_val.last_change; }
43 bool IsBoolean()
const {
return m_val.type == NT_BOOLEAN; }
44 bool IsDouble()
const {
return m_val.type == NT_DOUBLE; }
45 bool IsString()
const {
return m_val.type == NT_STRING; }
46 bool IsRaw()
const {
return m_val.type == NT_RAW; }
47 bool IsRpc()
const {
return m_val.type == NT_RPC; }
48 bool IsBooleanArray()
const {
return m_val.type == NT_BOOLEAN_ARRAY; }
49 bool IsDoubleArray()
const {
return m_val.type == NT_DOUBLE_ARRAY; }
50 bool IsStringArray()
const {
return m_val.type == NT_STRING_ARRAY; }
55 bool GetBoolean()
const {
56 assert(m_val.type == NT_BOOLEAN);
57 return m_val.data.v_boolean != 0;
59 double GetDouble()
const {
60 assert(m_val.type == NT_DOUBLE);
61 return m_val.data.v_double;
64 assert(m_val.type == NT_STRING);
68 assert(m_val.type == NT_RAW);
72 assert(m_val.type == NT_RPC);
76 assert(m_val.type == NT_BOOLEAN_ARRAY);
78 m_val.data.arr_boolean.size);
81 assert(m_val.type == NT_DOUBLE_ARRAY);
83 m_val.data.arr_double.size);
86 assert(m_val.type == NT_STRING_ARRAY);
87 return m_string_array;
90 static std::shared_ptr<Value> MakeBoolean(
bool value) {
91 auto val = std::make_shared<Value>(NT_BOOLEAN, private_init());
92 val->m_val.data.v_boolean = value;
95 static std::shared_ptr<Value> MakeDouble(
double value) {
96 auto val = std::make_shared<Value>(NT_DOUBLE, private_init());
97 val->m_val.data.v_double = value;
100 static std::shared_ptr<Value> MakeString(
StringRef value) {
101 auto val = std::make_shared<Value>(NT_STRING, private_init());
102 val->m_string = value;
103 val->m_val.
data.v_string.str =
const_cast<char*
>(val->m_string.c_str());
104 val->m_val.data.v_string.len = val->m_string.size();
108 template <
typename T,
typename = std::enable_if_t<std::is_same<T, std::
string>>>
110 template <
typename T,
111 typename std::enable_if<std::is_same<T, std::string>::value>::type>
113 static std::shared_ptr<Value> MakeString(T&& value) {
114 auto val = std::make_shared<Value>(NT_STRING, private_init());
115 val->m_string = std::move(value);
116 val->m_val.data.v_string.str =
const_cast<char*
>(val->m_string.c_str());
117 val->m_val.data.v_string.len = val->m_string.size();
120 static std::shared_ptr<Value> MakeRaw(
StringRef value) {
121 auto val = std::make_shared<Value>(NT_RAW, private_init());
122 val->m_string = value;
123 val->m_val.
data.v_raw.str =
const_cast<char*
>(val->m_string.c_str());
124 val->m_val.data.v_raw.len = val->m_string.size();
128 template <
typename T,
typename = std::enable_if_t<std::is_same<T, std::
string>>>
130 template <
typename T,
131 typename std::enable_if<std::is_same<T, std::string>::value>::type>
133 static std::shared_ptr<Value> MakeRaw(T&& value) {
134 auto val = std::make_shared<Value>(NT_RAW, private_init());
135 val->m_string = std::move(value);
136 val->m_val.data.v_raw.str =
const_cast<char*
>(val->m_string.c_str());
137 val->m_val.data.v_raw.len = val->m_string.size();
140 static std::shared_ptr<Value> MakeRpc(
StringRef value) {
141 auto val = std::make_shared<Value>(NT_RPC, private_init());
142 val->m_string = value;
143 val->m_val.
data.v_raw.str =
const_cast<char*
>(val->m_string.c_str());
144 val->m_val.data.v_raw.len = val->m_string.size();
147 template <
typename T>
148 static std::shared_ptr<Value> MakeRpc(T&& value) {
149 auto val = std::make_shared<Value>(NT_RPC, private_init());
150 val->m_string = std::move(value);
151 val->m_val.data.v_raw.str =
const_cast<char*
>(val->m_string.c_str());
152 val->m_val.data.v_raw.len = val->m_string.size();
156 static std::shared_ptr<Value> MakeBooleanArray(
ArrayRef<int> value);
161 static std::shared_ptr<Value> MakeStringArray(
162 std::vector<std::string>&& value);
166 friend bool operator==(
const Value& lhs,
const Value& rhs);
170 std::string m_string;
171 std::vector<std::string> m_string_array;
174 bool operator==(
const Value& lhs,
const Value& rhs);
175 inline bool operator!=(
const Value& lhs,
const Value& rhs) {
176 return !(lhs == rhs);
181 #endif // NT_VALUE_H_
NetworkTables Entry Value.
Definition: ntcore_c.h:82
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:106
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:54
NetworkTables Entry Value.
Definition: nt_Value.h:28
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39