WPILibC++  2020.3.2
NetworkTableValue.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 FIRST. 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 <initializer_list>
15 #include <memory>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19 #include <vector>
20 
21 #include <wpi/ArrayRef.h>
22 #include <wpi/StringRef.h>
23 #include <wpi/Twine.h>
24 
25 #include "ntcore_c.h"
26 
27 namespace nt {
28 
29 using wpi::ArrayRef;
30 using wpi::StringRef;
31 using wpi::Twine;
32 
37 class Value final {
38  struct private_init {};
39 
40  public:
41  Value();
42  Value(NT_Type type, uint64_t time, const private_init&);
43  ~Value();
44 
50  NT_Type type() const { return m_val.type; }
51 
57  const NT_Value& value() const { return m_val; }
58 
64  uint64_t last_change() const { return m_val.last_change; }
65 
71  uint64_t time() const { return m_val.last_change; }
72 
83  bool IsValid() const { return m_val.type != NT_UNASSIGNED; }
84 
90  bool IsBoolean() const { return m_val.type == NT_BOOLEAN; }
91 
97  bool IsDouble() const { return m_val.type == NT_DOUBLE; }
98 
104  bool IsString() const { return m_val.type == NT_STRING; }
105 
111  bool IsRaw() const { return m_val.type == NT_RAW; }
112 
118  bool IsRpc() const { return m_val.type == NT_RPC; }
119 
125  bool IsBooleanArray() const { return m_val.type == NT_BOOLEAN_ARRAY; }
126 
132  bool IsDoubleArray() const { return m_val.type == NT_DOUBLE_ARRAY; }
133 
139  bool IsStringArray() const { return m_val.type == NT_STRING_ARRAY; }
140 
153  bool GetBoolean() const {
154  assert(m_val.type == NT_BOOLEAN);
155  return m_val.data.v_boolean != 0;
156  }
157 
163  double GetDouble() const {
164  assert(m_val.type == NT_DOUBLE);
165  return m_val.data.v_double;
166  }
167 
174  assert(m_val.type == NT_STRING);
175  return m_string;
176  }
177 
183  StringRef GetRaw() const {
184  assert(m_val.type == NT_RAW);
185  return m_string;
186  }
187 
193  StringRef GetRpc() const {
194  assert(m_val.type == NT_RPC);
195  return m_string;
196  }
197 
204  assert(m_val.type == NT_BOOLEAN_ARRAY);
205  return ArrayRef<int>(m_val.data.arr_boolean.arr,
206  m_val.data.arr_boolean.size);
207  }
208 
215  assert(m_val.type == NT_DOUBLE_ARRAY);
216  return ArrayRef<double>(m_val.data.arr_double.arr,
217  m_val.data.arr_double.size);
218  }
219 
226  assert(m_val.type == NT_STRING_ARRAY);
227  return m_string_array;
228  }
229 
245  static std::shared_ptr<Value> MakeBoolean(bool value, uint64_t time = 0) {
246  auto val = std::make_shared<Value>(NT_BOOLEAN, time, private_init());
247  val->m_val.data.v_boolean = value;
248  return val;
249  }
250 
259  static std::shared_ptr<Value> MakeDouble(double value, uint64_t time = 0) {
260  auto val = std::make_shared<Value>(NT_DOUBLE, time, private_init());
261  val->m_val.data.v_double = value;
262  return val;
263  }
264 
273  static std::shared_ptr<Value> MakeString(const Twine& value,
274  uint64_t time = 0) {
275  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
276  val->m_string = value.str();
277  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
278  val->m_val.data.v_string.len = val->m_string.size();
279  return val;
280  }
281 
290  template <typename T,
291  typename std::enable_if<std::is_same<T, std::string>::value>::type>
292  static std::shared_ptr<Value> MakeString(T&& value, uint64_t time = 0) {
293  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
294  val->m_string = std::move(value);
295  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
296  val->m_val.data.v_string.len = val->m_string.size();
297  return val;
298  }
299 
308  static std::shared_ptr<Value> MakeRaw(StringRef value, uint64_t time = 0) {
309  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
310  val->m_string = value;
311  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
312  val->m_val.data.v_raw.len = val->m_string.size();
313  return val;
314  }
315 
324  template <typename T,
325  typename std::enable_if<std::is_same<T, std::string>::value>::type>
326  static std::shared_ptr<Value> MakeRaw(T&& value, uint64_t time = 0) {
327  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
328  val->m_string = std::move(value);
329  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
330  val->m_val.data.v_raw.len = val->m_string.size();
331  return val;
332  }
333 
342  static std::shared_ptr<Value> MakeRpc(StringRef value, uint64_t time = 0) {
343  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
344  val->m_string = value;
345  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
346  val->m_val.data.v_raw.len = val->m_string.size();
347  return val;
348  }
349 
358  template <typename T>
359  static std::shared_ptr<Value> MakeRpc(T&& value, uint64_t time = 0) {
360  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
361  val->m_string = std::move(value);
362  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
363  val->m_val.data.v_raw.len = val->m_string.size();
364  return val;
365  }
366 
375  static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<bool> value,
376  uint64_t time = 0);
377 
386  static std::shared_ptr<Value> MakeBooleanArray(
387  std::initializer_list<bool> value, uint64_t time = 0) {
388  return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
389  time);
390  }
391 
400  static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value,
401  uint64_t time = 0);
402 
411  static std::shared_ptr<Value> MakeBooleanArray(
412  std::initializer_list<int> value, uint64_t time = 0) {
413  return MakeBooleanArray(wpi::makeArrayRef(value.begin(), value.end()),
414  time);
415  }
416 
425  static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value,
426  uint64_t time = 0);
427 
436  static std::shared_ptr<Value> MakeDoubleArray(
437  std::initializer_list<double> value, uint64_t time = 0) {
438  return MakeDoubleArray(wpi::makeArrayRef(value.begin(), value.end()), time);
439  }
440 
449  static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value,
450  uint64_t time = 0);
451 
460  static std::shared_ptr<Value> MakeStringArray(
461  std::initializer_list<std::string> value, uint64_t time = 0) {
462  return MakeStringArray(wpi::makeArrayRef(value.begin(), value.end()), time);
463  }
464 
475  static std::shared_ptr<Value> MakeStringArray(
476  std::vector<std::string>&& value, uint64_t time = 0);
477 
480  Value(const Value&) = delete;
481  Value& operator=(const Value&) = delete;
482  friend bool operator==(const Value& lhs, const Value& rhs);
483 
484  private:
485  NT_Value m_val;
486  std::string m_string;
487  std::vector<std::string> m_string_array;
488 };
489 
490 bool operator==(const Value& lhs, const Value& rhs);
491 inline bool operator!=(const Value& lhs, const Value& rhs) {
492  return !(lhs == rhs);
493 }
494 
500 
501 } // namespace nt
502 
503 #endif // NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
nt::Value::IsStringArray
bool IsStringArray() const
Determine if entry value contains a string array.
Definition: NetworkTableValue.h:139
NT_String::str
char * str
String contents (UTF-8).
Definition: ntcore_c.h:113
nt::Value::MakeBoolean
static std::shared_ptr< Value > MakeBoolean(bool value, uint64_t time=0)
Creates a boolean entry value.
Definition: NetworkTableValue.h:245
nt::Value::IsBooleanArray
bool IsBooleanArray() const
Determine if entry value contains a boolean array.
Definition: NetworkTableValue.h:125
nt::Value::GetRaw
StringRef GetRaw() const
Get the entry's raw value.
Definition: NetworkTableValue.h:183
nt::Value::IsRpc
bool IsRpc() const
Determine if entry value contains a rpc definition.
Definition: NetworkTableValue.h:118
nt::Value::MakeString
static std::shared_ptr< Value > MakeString(const Twine &value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:273
nt::NetworkTableValue
Value NetworkTableValue
NetworkTable Value alias for similarity with Java.
Definition: NetworkTableValue.h:499
nt::Value::MakeRpc
static std::shared_ptr< Value > MakeRpc(StringRef value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:342
nt::Value::MakeBooleanArray
static std::shared_ptr< Value > MakeBooleanArray(ArrayRef< bool > value, uint64_t time=0)
Creates a boolean array entry value.
nt::Value::type
NT_Type type() const
Get the data type.
Definition: NetworkTableValue.h:50
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
nt::Value::GetDoubleArray
ArrayRef< double > GetDoubleArray() const
Get the entry's double array value.
Definition: NetworkTableValue.h:214
nt::Value::MakeBooleanArray
static std::shared_ptr< Value > MakeBooleanArray(std::initializer_list< bool > value, uint64_t time=0)
Creates a boolean array entry value.
Definition: NetworkTableValue.h:386
nt::Value::MakeBooleanArray
static std::shared_ptr< Value > MakeBooleanArray(std::initializer_list< int > value, uint64_t time=0)
Creates a boolean array entry value.
Definition: NetworkTableValue.h:411
nt::Value::GetBooleanArray
ArrayRef< int > GetBooleanArray() const
Get the entry's boolean array value.
Definition: NetworkTableValue.h:203
NT_Value
NetworkTables Entry Value.
Definition: ntcore_c.h:123
nt::Value::MakeDouble
static std::shared_ptr< Value > MakeDouble(double value, uint64_t time=0)
Creates a double entry value.
Definition: NetworkTableValue.h:259
wpi::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:447
nt::Value::IsValid
bool IsValid() const
Determine if entry value contains a value or is unassigned.
Definition: NetworkTableValue.h:83
nt::Value::MakeRaw
static std::shared_ptr< Value > MakeRaw(T &&value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:326
nt::Value::time
uint64_t time() const
Get the creation time of the value.
Definition: NetworkTableValue.h:71
nt::Value::MakeRpc
static std::shared_ptr< Value > MakeRpc(T &&value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:359
nt::Value::GetRpc
StringRef GetRpc() const
Get the entry's rpc definition value.
Definition: NetworkTableValue.h:193
nt::Value::IsDoubleArray
bool IsDoubleArray() const
Determine if entry value contains a double array.
Definition: NetworkTableValue.h:132
nt::Value::MakeString
static std::shared_ptr< Value > MakeString(T &&value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:292
nt::Value::IsDouble
bool IsDouble() const
Determine if entry value contains a double.
Definition: NetworkTableValue.h:97
nt::Value::last_change
uint64_t last_change() const
Get the creation time of the value.
Definition: NetworkTableValue.h:64
nt::Value::MakeStringArray
static std::shared_ptr< Value > MakeStringArray(std::initializer_list< std::string > value, uint64_t time=0)
Creates a string array entry value.
Definition: NetworkTableValue.h:460
nt::Value::GetBoolean
bool GetBoolean() const
Get the entry's boolean value.
Definition: NetworkTableValue.h:153
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
nt::Value::GetDouble
double GetDouble() const
Get the entry's double value.
Definition: NetworkTableValue.h:163
nt::Value::IsBoolean
bool IsBoolean() const
Determine if entry value contains a boolean.
Definition: NetworkTableValue.h:90
nt::Value::IsRaw
bool IsRaw() const
Determine if entry value contains a raw.
Definition: NetworkTableValue.h:111
nt::Value::GetStringArray
ArrayRef< std::string > GetStringArray() const
Get the entry's string array value.
Definition: NetworkTableValue.h:225
nt::Value::MakeRaw
static std::shared_ptr< Value > MakeRaw(StringRef value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:308
nt::Value::IsString
bool IsString() const
Determine if entry value contains a string.
Definition: NetworkTableValue.h:104
nt::Value::GetString
StringRef GetString() const
Get the entry's string value.
Definition: NetworkTableValue.h:173
nt::Value
A network table entry value.
Definition: NetworkTableValue.h:37
nt::Value::MakeStringArray
static std::shared_ptr< Value > MakeStringArray(ArrayRef< std::string > value, uint64_t time=0)
Creates a string array entry value.
NT_Type
NT_Type
NetworkTables data types.
Definition: ntcore_c.h:52
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
nt
NetworkTables (ntcore) namespace.
Definition: NetworkTableValue.h:27
nt::Value::MakeDoubleArray
static std::shared_ptr< Value > MakeDoubleArray(std::initializer_list< double > value, uint64_t time=0)
Creates a double array entry value.
Definition: NetworkTableValue.h:436
nt::Value::value
const NT_Value & value() const
Get the data value stored.
Definition: NetworkTableValue.h:57
nt::Value::MakeDoubleArray
static std::shared_ptr< Value > MakeDoubleArray(ArrayRef< double > value, uint64_t time=0)
Creates a double array entry value.