WPILibC++  2018.4.1-20180924031742-1200-g1aa8446
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
NetworkTableValue.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 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 <memory>
15 #include <string>
16 #include <type_traits>
17 #include <utility>
18 #include <vector>
19 
20 #include <wpi/ArrayRef.h>
21 #include <wpi/StringRef.h>
22 #include <wpi/Twine.h>
23 
24 #include "ntcore_c.h"
25 
26 namespace nt {
27 
28 using wpi::ArrayRef;
29 using wpi::StringRef;
30 using wpi::Twine;
31 
36 class Value final {
37  struct private_init {};
38 
39  public:
40  Value();
41  Value(NT_Type type, uint64_t time, const private_init&);
42  ~Value();
43 
49  NT_Type type() const { return m_val.type; }
50 
56  const NT_Value& value() const { return m_val; }
57 
63  uint64_t last_change() const { return m_val.last_change; }
64 
70  uint64_t time() const { return m_val.last_change; }
71 
82  bool IsValid() const { return m_val.type != NT_UNASSIGNED; }
83 
89  bool IsBoolean() const { return m_val.type == NT_BOOLEAN; }
90 
96  bool IsDouble() const { return m_val.type == NT_DOUBLE; }
97 
103  bool IsString() const { return m_val.type == NT_STRING; }
104 
110  bool IsRaw() const { return m_val.type == NT_RAW; }
111 
117  bool IsRpc() const { return m_val.type == NT_RPC; }
118 
124  bool IsBooleanArray() const { return m_val.type == NT_BOOLEAN_ARRAY; }
125 
131  bool IsDoubleArray() const { return m_val.type == NT_DOUBLE_ARRAY; }
132 
138  bool IsStringArray() const { return m_val.type == NT_STRING_ARRAY; }
139 
152  bool GetBoolean() const {
153  assert(m_val.type == NT_BOOLEAN);
154  return m_val.data.v_boolean != 0;
155  }
156 
162  double GetDouble() const {
163  assert(m_val.type == NT_DOUBLE);
164  return m_val.data.v_double;
165  }
166 
173  assert(m_val.type == NT_STRING);
174  return m_string;
175  }
176 
182  StringRef GetRaw() const {
183  assert(m_val.type == NT_RAW);
184  return m_string;
185  }
186 
192  StringRef GetRpc() const {
193  assert(m_val.type == NT_RPC);
194  return m_string;
195  }
196 
203  assert(m_val.type == NT_BOOLEAN_ARRAY);
204  return ArrayRef<int>(m_val.data.arr_boolean.arr,
205  m_val.data.arr_boolean.size);
206  }
207 
214  assert(m_val.type == NT_DOUBLE_ARRAY);
215  return ArrayRef<double>(m_val.data.arr_double.arr,
216  m_val.data.arr_double.size);
217  }
218 
225  assert(m_val.type == NT_STRING_ARRAY);
226  return m_string_array;
227  }
228 
244  static std::shared_ptr<Value> MakeBoolean(bool value, uint64_t time = 0) {
245  auto val = std::make_shared<Value>(NT_BOOLEAN, time, private_init());
246  val->m_val.data.v_boolean = value;
247  return val;
248  }
249 
258  static std::shared_ptr<Value> MakeDouble(double value, uint64_t time = 0) {
259  auto val = std::make_shared<Value>(NT_DOUBLE, time, private_init());
260  val->m_val.data.v_double = value;
261  return val;
262  }
263 
272  static std::shared_ptr<Value> MakeString(const Twine& value,
273  uint64_t time = 0) {
274  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
275  val->m_string = value.str();
276  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
277  val->m_val.data.v_string.len = val->m_string.size();
278  return val;
279  }
280 
289 #ifdef _MSC_VER
290  template <typename T,
291  typename = std::enable_if_t<std::is_same<T, std::string>>>
292 #else
293  template <typename T,
294  typename std::enable_if<std::is_same<T, std::string>::value>::type>
295 #endif
296  static std::shared_ptr<Value> MakeString(T&& value, uint64_t time = 0) {
297  auto val = std::make_shared<Value>(NT_STRING, time, private_init());
298  val->m_string = std::move(value);
299  val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
300  val->m_val.data.v_string.len = val->m_string.size();
301  return val;
302  }
303 
312  static std::shared_ptr<Value> MakeRaw(StringRef value, uint64_t time = 0) {
313  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
314  val->m_string = value;
315  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
316  val->m_val.data.v_raw.len = val->m_string.size();
317  return val;
318  }
319 
328 #ifdef _MSC_VER
329  template <typename T,
330  typename = std::enable_if_t<std::is_same<T, std::string>>>
331 #else
332  template <typename T,
333  typename std::enable_if<std::is_same<T, std::string>::value>::type>
334 #endif
335  static std::shared_ptr<Value> MakeRaw(T&& value, uint64_t time = 0) {
336  auto val = std::make_shared<Value>(NT_RAW, time, private_init());
337  val->m_string = std::move(value);
338  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
339  val->m_val.data.v_raw.len = val->m_string.size();
340  return val;
341  }
342 
351  static std::shared_ptr<Value> MakeRpc(StringRef value, uint64_t time = 0) {
352  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
353  val->m_string = value;
354  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
355  val->m_val.data.v_raw.len = val->m_string.size();
356  return val;
357  }
358 
367  template <typename T>
368  static std::shared_ptr<Value> MakeRpc(T&& value, uint64_t time = 0) {
369  auto val = std::make_shared<Value>(NT_RPC, time, private_init());
370  val->m_string = std::move(value);
371  val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
372  val->m_val.data.v_raw.len = val->m_string.size();
373  return val;
374  }
375 
384  static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value,
385  uint64_t time = 0);
386 
395  static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value,
396  uint64_t time = 0);
397 
406  static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value,
407  uint64_t time = 0);
408 
419  static std::shared_ptr<Value> MakeStringArray(
420  std::vector<std::string>&& value, uint64_t time = 0);
421 
424  Value(const Value&) = delete;
425  Value& operator=(const Value&) = delete;
426  friend bool operator==(const Value& lhs, const Value& rhs);
427 
428  private:
429  NT_Value m_val;
430  std::string m_string;
431  std::vector<std::string> m_string_array;
432 };
433 
434 bool operator==(const Value& lhs, const Value& rhs);
435 inline bool operator!=(const Value& lhs, const Value& rhs) {
436  return !(lhs == rhs);
437 }
438 
444 
445 } // namespace nt
446 
447 #endif // NTCORE_NETWORKTABLES_NETWORKTABLEVALUE_H_
bool IsBoolean() const
Determine if entry value contains a boolean.
Definition: NetworkTableValue.h:89
static std::shared_ptr< Value > MakeRpc(StringRef value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:351
static std::shared_ptr< Value > MakeString(const Twine &value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:272
static std::shared_ptr< Value > MakeRaw(T &&value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:335
bool IsValid() const
Determine if entry value contains a value or is unassigned.
Definition: NetworkTableValue.h:82
NetworkTables Entry Value.
Definition: ntcore_c.h:122
static std::shared_ptr< Value > MakeRaw(StringRef value, uint64_t time=0)
Creates a raw entry value.
Definition: NetworkTableValue.h:312
static std::shared_ptr< Value > MakeString(T &&value, uint64_t time=0)
Creates a string entry value.
Definition: NetworkTableValue.h:296
bool IsRpc() const
Determine if entry value contains a rpc definition.
Definition: NetworkTableValue.h:117
bool IsStringArray() const
Determine if entry value contains a string array.
Definition: NetworkTableValue.h:138
static std::shared_ptr< Value > MakeDoubleArray(ArrayRef< double > value, uint64_t time=0)
Creates a double array entry value.
bool GetBoolean() const
Get the entry's boolean value.
Definition: NetworkTableValue.h:152
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
ArrayRef< double > GetDoubleArray() const
Get the entry's double array value.
Definition: NetworkTableValue.h:213
static std::shared_ptr< Value > MakeStringArray(ArrayRef< std::string > value, uint64_t time=0)
Creates a string array entry value.
static std::shared_ptr< Value > MakeBooleanArray(ArrayRef< int > value, uint64_t time=0)
Creates a boolean array entry value.
std::string str() const
Return the twine contents as a std::string.
NetworkTables (ntcore) namespace.
Definition: ITable.h:21
uint64_t time() const
Get the creation time of the value.
Definition: NetworkTableValue.h:70
StringRef GetRpc() const
Get the entry's rpc definition value.
Definition: NetworkTableValue.h:192
bool IsDouble() const
Determine if entry value contains a double.
Definition: NetworkTableValue.h:96
double GetDouble() const
Get the entry's double value.
Definition: NetworkTableValue.h:162
bool IsBooleanArray() const
Determine if entry value contains a boolean array.
Definition: NetworkTableValue.h:124
bool IsDoubleArray() const
Determine if entry value contains a double array.
Definition: NetworkTableValue.h:131
NT_Type type() const
Get the data type.
Definition: NetworkTableValue.h:49
ArrayRef< int > GetBooleanArray() const
Get the entry's boolean array value.
Definition: NetworkTableValue.h:202
const NT_Value & value() const
Get the data value stored.
Definition: NetworkTableValue.h:56
ArrayRef< std::string > GetStringArray() const
Get the entry's string array value.
Definition: NetworkTableValue.h:224
StringRef GetString() const
Get the entry's string value.
Definition: NetworkTableValue.h:172
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
NT_Type
NetworkTables data types.
Definition: ntcore_c.h:52
static std::shared_ptr< Value > MakeBoolean(bool value, uint64_t time=0)
Creates a boolean entry value.
Definition: NetworkTableValue.h:244
static std::shared_ptr< Value > MakeRpc(T &&value, uint64_t time=0)
Creates a rpc entry value.
Definition: NetworkTableValue.h:368
StringRef GetRaw() const
Get the entry's raw value.
Definition: NetworkTableValue.h:182
char * str
String contents (UTF-8).
Definition: ntcore_c.h:112
uint64_t last_change() const
Get the creation time of the value.
Definition: NetworkTableValue.h:63
A network table entry value.
Definition: NetworkTableValue.h:36
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
static std::shared_ptr< Value > MakeDouble(double value, uint64_t time=0)
Creates a double entry value.
Definition: NetworkTableValue.h:258
Value NetworkTableValue
NetworkTable Value alias for similarity with Java.
Definition: NetworkTableValue.h:443
bool IsString() const
Determine if entry value contains a string.
Definition: NetworkTableValue.h:103
bool IsRaw() const
Determine if entry value contains a raw.
Definition: NetworkTableValue.h:110