WPILibC++  unspecified
NetworkTableValue.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015-2018. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
9 #define NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
10 
11 #include <stdint.h>
12 
13 #include <cassert>
14 #include <memory>
15 #include <string>
16 #include <type_traits>
17 #include <utility>
18 #include <vector>
19 
20 #include <llvm/ArrayRef.h>
21 #include <llvm/StringRef.h>
22 #include <llvm/Twine.h>
23 
24 #include "ntcore_c.h"
25 
26 namespace nt {
27 
28 using llvm::ArrayRef;
29 using llvm::StringRef;
30 using llvm::Twine;
31 
35 class Value final {
36  struct private_init {};
37 
38  public:
39  Value();
40  Value(NT_Type type, uint64_t time, const private_init&);
41  ~Value();
42 
47  NT_Type type() const { return m_val.type; }
48 
53  const NT_Value& value() const { return m_val; }
54 
59  uint64_t last_change() const { return m_val.last_change; }
60 
65  uint64_t time() const { return m_val.last_change; }
66 
76  bool IsValid() const { return m_val.type != NT_UNASSIGNED; }
77 
82  bool IsBoolean() const { return m_val.type == NT_BOOLEAN; }
83 
88  bool IsDouble() const { return m_val.type == NT_DOUBLE; }
89 
94  bool IsString() const { return m_val.type == NT_STRING; }
95 
100  bool IsRaw() const { return m_val.type == NT_RAW; }
101 
106  bool IsRpc() const { return m_val.type == NT_RPC; }
107 
112  bool IsBooleanArray() const { return m_val.type == NT_BOOLEAN_ARRAY; }
113 
118  bool IsDoubleArray() const { return m_val.type == NT_DOUBLE_ARRAY; }
119 
124  bool IsStringArray() const { return m_val.type == NT_STRING_ARRAY; }
125 
137  bool GetBoolean() const {
138  assert(m_val.type == NT_BOOLEAN);
139  return m_val.data.v_boolean != 0;
140  }
141 
146  double GetDouble() const {
147  assert(m_val.type == NT_DOUBLE);
148  return m_val.data.v_double;
149  }
150 
156  assert(m_val.type == NT_STRING);
157  return m_string;
158  }
159 
164  StringRef GetRaw() const {
165  assert(m_val.type == NT_RAW);
166  return m_string;
167  }
168 
173  StringRef GetRpc() const {
174  assert(m_val.type == NT_RPC);
175  return m_string;
176  }
177 
183  assert(m_val.type == NT_BOOLEAN_ARRAY);
184  return ArrayRef<int>(m_val.data.arr_boolean.arr,
185  m_val.data.arr_boolean.size);
186  }
187 
193  assert(m_val.type == NT_DOUBLE_ARRAY);
194  return ArrayRef<double>(m_val.data.arr_double.arr,
195  m_val.data.arr_double.size);
196  }
197 
203  assert(m_val.type == NT_STRING_ARRAY);
204  return m_string_array;
205  }
206 
221  static std::shared_ptr<Value> MakeBoolean(bool value, uint64_t time = 0) {
222  auto val = std::make_shared<Value>(NT_BOOLEAN, time, private_init());
223  val->m_val.data.v_boolean = value;
224  return val;
225  }
226 
234  static std::shared_ptr<Value> MakeDouble(double value, uint64_t time = 0) {
235  auto val = std::make_shared<Value>(NT_DOUBLE, time, private_init());
236  val->m_val.data.v_double = value;
237  return val;
238  }
239 
247  static std::shared_ptr<Value> MakeString(const Twine& value,
248  uint64_t time = 0) {
249  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
250  val->m_string = value.str();
251  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
252  val->m_val.data.v_string.len = val->m_string.size();
253  return val;
254  }
255 
263 #ifdef _MSC_VER
264  template <typename T,
265  typename = std::enable_if_t<std::is_same<T, std::string>>>
266 #else
267  template <typename T,
268  typename std::enable_if<std::is_same<T, std::string>::value>::type>
269 #endif
270  static std::shared_ptr<Value> MakeString(T&& value, uint64_t time = 0) {
271  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
272  val->m_string = std::move(value);
273  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
274  val->m_val.data.v_string.len = val->m_string.size();
275  return val;
276  }
277 
285  static std::shared_ptr<Value> MakeRaw(StringRef value, uint64_t time = 0) {
286  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
287  val->m_string = value;
288  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
289  val->m_val.data.v_raw.len = val->m_string.size();
290  return val;
291  }
292 
300 #ifdef _MSC_VER
301  template <typename T,
302  typename = std::enable_if_t<std::is_same<T, std::string>>>
303 #else
304  template <typename T,
305  typename std::enable_if<std::is_same<T, std::string>::value>::type>
306 #endif
307  static std::shared_ptr<Value> MakeRaw(T&& value, uint64_t time = 0) {
308  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
309  val->m_string = std::move(value);
310  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
311  val->m_val.data.v_raw.len = val->m_string.size();
312  return val;
313  }
314 
322  static std::shared_ptr<Value> MakeRpc(StringRef value, uint64_t time = 0) {
323  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
324  val->m_string = value;
325  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
326  val->m_val.data.v_raw.len = val->m_string.size();
327  return val;
328  }
329 
337  template <typename T>
338  static std::shared_ptr<Value> MakeRpc(T&& value, uint64_t time = 0) {
339  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
340  val->m_string = std::move(value);
341  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
342  val->m_val.data.v_raw.len = val->m_string.size();
343  return val;
344  }
345 
353  static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value,
354  uint64_t time = 0);
355 
363  static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value,
364  uint64_t time = 0);
365 
373  static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value,
374  uint64_t time = 0);
375 
376  // Note: This function moves the values out of the vector.
377  static std::shared_ptr<Value> MakeStringArray(
378  std::vector<std::string>&& value, uint64_t time = 0);
379 
382  Value(const Value&) = delete;
383  Value& operator=(const Value&) = delete;
384  friend bool operator==(const Value& lhs, const Value& rhs);
385 
386  private:
387  NT_Value m_val;
388  std::string m_string;
389  std::vector<std::string> m_string_array;
390 };
391 
392 bool operator==(const Value& lhs, const Value& rhs);
393 inline bool operator!=(const Value& lhs, const Value& rhs) {
394  return !(lhs == rhs);
395 }
396 
398 typedef Value NetworkTableValue;
399 
400 } // namespace nt
401 
402 #endif // NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
static std::shared_ptr< Value > MakeDouble(double value, uint64_t time=0)
Creates a double entry value.
Definition: NetworkTableValue.h:234
bool IsValid() const
Determine if entry value contains a value or is unassigned.
Definition: NetworkTableValue.h:76
static std::shared_ptr< Value > MakeRpc(T &&value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:338
NetworkTables Entry Value.
Definition: ntcore_c.h:110
static std::shared_ptr< Value > MakeString(const Twine &value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:247
bool IsBooleanArray() const
Determine if entry value contains a boolean array.
Definition: NetworkTableValue.h:112
static std::shared_ptr< Value > MakeRpc(StringRef value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:322
bool IsString() const
Determine if entry value contains a string.
Definition: NetworkTableValue.h:94
static std::shared_ptr< Value > MakeRaw(StringRef value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:285
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:15
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
ArrayRef< std::string > GetStringArray() const
Get the entry&#39;s string array value.
Definition: NetworkTableValue.h:202
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
static std::shared_ptr< Value > MakeBoolean(bool value, uint64_t time=0)
Creates a boolean entry value.
Definition: NetworkTableValue.h:221
static std::shared_ptr< Value > MakeRaw(T &&value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:307
StringRef GetRpc() const
Get the entry&#39;s rpc definition value.
Definition: NetworkTableValue.h:173
static std::shared_ptr< Value > MakeBooleanArray(ArrayRef< int > value, uint64_t time=0)
Creates a boolean array entry value.
Definition: Value.cpp:45
ArrayRef< int > GetBooleanArray() const
Get the entry&#39;s boolean array value.
Definition: NetworkTableValue.h:182
static std::shared_ptr< Value > MakeStringArray(ArrayRef< std::string > value, uint64_t time=0)
Creates a string array entry value.
Definition: Value.cpp:63
Definition: IEntryNotifier.h:16
uint64_t time() const
Get the creation time of the value.
Definition: NetworkTableValue.h:65
bool IsDoubleArray() const
Determine if entry value contains a double array.
Definition: NetworkTableValue.h:118
ArrayRef< double > GetDoubleArray() const
Get the entry&#39;s double array value.
Definition: NetworkTableValue.h:192
static std::shared_ptr< Value > MakeString(T &&value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:270
bool IsBoolean() const
Determine if entry value contains a boolean.
Definition: NetworkTableValue.h:82
NT_Type type() const
Get the data type.
Definition: NetworkTableValue.h:47
bool IsRpc() const
Determine if entry value contains a rpc definition.
Definition: NetworkTableValue.h:106
const NT_Value & value() const
Get the data value stored.
Definition: NetworkTableValue.h:53
static std::shared_ptr< Value > MakeDoubleArray(ArrayRef< double > value, uint64_t time=0)
Creates a double array entry value.
Definition: Value.cpp:54
double GetDouble() const
Get the entry&#39;s double value.
Definition: NetworkTableValue.h:146
StringRef GetRaw() const
Get the entry&#39;s raw value.
Definition: NetworkTableValue.h:164
bool IsRaw() const
Determine if entry value contains a raw.
Definition: NetworkTableValue.h:100
bool IsDouble() const
Determine if entry value contains a double.
Definition: NetworkTableValue.h:88
bool GetBoolean() const
Get the entry&#39;s boolean value.
Definition: NetworkTableValue.h:137
bool IsStringArray() const
Determine if entry value contains a string array.
Definition: NetworkTableValue.h:124
char * str
String contents (UTF-8).
Definition: ntcore_c.h:100
uint64_t last_change() const
Get the creation time of the value.
Definition: NetworkTableValue.h:59
A network table entry value.
Definition: NetworkTableValue.h:35
StringRef GetString() const
Get the entry&#39;s string value.
Definition: NetworkTableValue.h:155
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42