WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
NetworkTable.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. 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 NETWORKTABLE_H_
9 #define NETWORKTABLE_H_
10 
11 #include <functional>
12 #include <mutex>
13 #include <vector>
14 
15 #include "tables/ITable.h"
16 
20 class NetworkTable : public ITable {
21  private:
22  struct private_init {};
23 
24  std::string m_path;
25  std::mutex m_mutex;
26  typedef std::pair<ITableListener*, unsigned int> Listener;
27  std::vector<Listener> m_listeners;
28 
29  static std::string s_ip_address;
30  static std::string s_persistent_filename;
31  static bool s_client;
32  static bool s_running;
33  static unsigned int s_port;
34 
35  public:
36  NetworkTable(llvm::StringRef path, const private_init&);
37  virtual ~NetworkTable();
38 
43  static const char PATH_SEPARATOR_CHAR;
44 
48  static void Initialize();
49  static void Shutdown();
50 
55  static void SetClientMode();
56 
61  static void SetServerMode();
62 
69  static void SetTeam(int team);
70 
75  static void SetIPAddress(llvm::StringRef address);
76 
81  static void SetPort(unsigned int port);
82 
88  static void SetPersistentFilename(llvm::StringRef filename);
89 
95  static void SetNetworkIdentity(llvm::StringRef name);
96 
100  static void GlobalDeleteAll();
101 
108  static void Flush();
109 
115  static void SetUpdateRate(double interval);
116 
123  static const char* SavePersistent(llvm::StringRef filename);
124 
132  static const char* LoadPersistent(
133  llvm::StringRef filename,
134  std::function<void(size_t line, const char* msg)> warn);
135 
146  static std::shared_ptr<NetworkTable> GetTable(llvm::StringRef key);
147 
148  void AddTableListener(ITableListener* listener) override;
149  void AddTableListener(ITableListener* listener,
150  bool immediateNotify) override;
151  void AddTableListenerEx(ITableListener* listener,
152  unsigned int flags) override;
153  void AddTableListener(llvm::StringRef key, ITableListener* listener,
154  bool immediateNotify) override;
156  unsigned int flags) override;
157  void AddSubTableListener(ITableListener* listener) override;
158  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
159  void RemoveTableListener(ITableListener* listener) override;
160 
169  std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const override;
170 
177  bool ContainsKey(llvm::StringRef key) const override;
178 
187  bool ContainsSubTable(llvm::StringRef key) const override;
188 
193  std::vector<std::string> GetKeys(int types = 0) const override;
194 
198  std::vector<std::string> GetSubTables() const override;
199 
205  void SetPersistent(llvm::StringRef key) override;
206 
213  void ClearPersistent(llvm::StringRef key) override;
214 
221  bool IsPersistent(llvm::StringRef key) const override;
222 
230  void SetFlags(llvm::StringRef key, unsigned int flags) override;
231 
239  void ClearFlags(llvm::StringRef key, unsigned int flags) override;
240 
247  unsigned int GetFlags(llvm::StringRef key) const override;
248 
254  void Delete(llvm::StringRef key) override;
255 
263  bool PutNumber(llvm::StringRef key, double value) override;
264 
275  NT_DEPRECATED("Raises an exception if key not found; "
276  "use GetNumber(StringRef key, double defaultValue) instead")
277  virtual double GetNumber(llvm::StringRef key) const override;
278 
287  virtual double GetNumber(llvm::StringRef key,
288  double defaultValue) const override;
289 
297  virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override;
298 
309  NT_DEPRECATED("Raises an exception if key not found; "
310  "use GetString(StringRef key, StringRef defaultValue) instead")
311  virtual std::string GetString(llvm::StringRef key) const override;
312 
322  virtual std::string GetString(llvm::StringRef key,
323  llvm::StringRef defaultValue) const override;
324 
332  virtual bool PutBoolean(llvm::StringRef key, bool value) override;
333 
344  NT_DEPRECATED("Raises an exception if key not found; "
345  "use GetBoolean(StringRef key, bool defaultValue) instead")
346  virtual bool GetBoolean(llvm::StringRef key) const override;
347 
357  virtual bool GetBoolean(llvm::StringRef key,
358  bool defaultValue) const override;
359 
370  virtual bool PutBooleanArray(llvm::StringRef key,
371  llvm::ArrayRef<int> value) override;
372 
388  virtual std::vector<int> GetBooleanArray(
389  llvm::StringRef key, llvm::ArrayRef<int> defaultValue) const override;
390 
397  virtual bool PutNumberArray(llvm::StringRef key,
398  llvm::ArrayRef<double> value) override;
399 
411  virtual std::vector<double> GetNumberArray(
412  llvm::StringRef key, llvm::ArrayRef<double> defaultValue) const override;
413 
420  virtual bool PutStringArray(llvm::StringRef key,
421  llvm::ArrayRef<std::string> value) override;
422 
434  virtual std::vector<std::string> GetStringArray(
435  llvm::StringRef key,
436  llvm::ArrayRef<std::string> defaultValue) const override;
437 
444  virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override;
445 
457  virtual std::string GetRaw(llvm::StringRef key,
458  llvm::StringRef defaultValue) const override;
459 
467  bool PutValue(llvm::StringRef key, std::shared_ptr<nt::Value> value) override;
468 
476  std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const override;
477 };
478 
479 #endif // NETWORKTABLE_H_
static void SetPort(unsigned int port)
Definition: NetworkTable.cpp:55
A table whose values can be read and written to.
Definition: ITable.h:43
virtual bool PutNumberArray(llvm::StringRef key, llvm::ArrayRef< double > value) override
Put a number array in the table.
Definition: NetworkTable.cpp:391
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:43
virtual std::vector< std::string > GetStringArray(llvm::StringRef key, llvm::ArrayRef< std::string > defaultValue) const override
Returns the string array the key maps to.
Definition: NetworkTable.cpp:418
std::vector< std::string > GetSubTables() const override
Definition: NetworkTable.cpp:237
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:40
static void SetTeam(int team)
set the team the robot is configured for (this will set the mdns address that network tables will con...
Definition: NetworkTable.cpp:42
void Delete(llvm::StringRef key) override
Deletes the specified key in this table.
Definition: NetworkTable.cpp:283
A network table that knows its subtable path.
Definition: NetworkTable.h:20
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
Definition: NetworkTable.cpp:65
virtual std::string GetRaw(llvm::StringRef key, llvm::StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
Definition: NetworkTable.cpp:436
void AddTableListenerEx(ITableListener *listener, unsigned int flags) override
Add a listener for changes to the table.
Definition: NetworkTable.cpp:114
static void SetNetworkIdentity(llvm::StringRef name)
Sets the network identity.
Definition: NetworkTable.cpp:61
bool PutNumber(llvm::StringRef key, double value) override
Put a number in the table.
Definition: NetworkTable.cpp:290
std::vector< std::string > GetKeys(int types=0) const override
Definition: NetworkTable.cpp:224
static void Initialize()
Definition: NetworkTable.cpp:20
bool IsPersistent(llvm::StringRef key) const override
Returns whether the value is persistent through program restarts.
Definition: NetworkTable.cpp:258
static std::shared_ptr< NetworkTable > GetTable(llvm::StringRef key)
Gets the table with the specified key.
Definition: NetworkTable.cpp:83
void ClearFlags(llvm::StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
Definition: NetworkTable.cpp:269
void SetFlags(llvm::StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
Definition: NetworkTable.cpp:262
static void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTable.cpp:69
static const char * LoadPersistent(llvm::StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Loads persistent keys from a file.
Definition: NetworkTable.cpp:77
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
bool ContainsSubTable(llvm::StringRef key) const override
Determines whether there exists a non-empty subtable for this key in this table.
Definition: NetworkTable.cpp:216
static void Flush()
Flushes all updated values immediately to the network.
Definition: NetworkTable.cpp:67
void AddSubTableListener(ITableListener *listener) override
This will immediately notify the listener of all current sub tables.
Definition: NetworkTable.cpp:157
virtual bool PutStringArray(llvm::StringRef key, llvm::ArrayRef< std::string > value) override
Put a string array in the table.
Definition: NetworkTable.cpp:410
virtual bool PutBoolean(llvm::StringRef key, bool value) override
Put a boolean in the table.
Definition: NetworkTable.cpp:345
unsigned int GetFlags(llvm::StringRef key) const override
Returns the flags for the specified key.
Definition: NetworkTable.cpp:276
std::shared_ptr< ITable > GetSubTable(llvm::StringRef key) const override
Returns the table at the specified key.
Definition: NetworkTable.cpp:202
std::shared_ptr< nt::Value > GetValue(llvm::StringRef key) const override
Gets the value associated with a key as an object.
Definition: NetworkTable.cpp:454
bool PutValue(llvm::StringRef key, std::shared_ptr< nt::Value > value) override
Put a value in the table.
Definition: NetworkTable.cpp:447
static void SetIPAddress(llvm::StringRef address)
Definition: NetworkTable.cpp:53
void SetPersistent(llvm::StringRef key) override
Makes a key's value persistent through program restarts.
Definition: NetworkTable.cpp:250
void ClearPersistent(llvm::StringRef key) override
Stop making a key's value persistent through program restarts.
Definition: NetworkTable.cpp:254
void AddTableListener(ITableListener *listener) override
Add a listener for changes to the table.
Definition: NetworkTable.cpp:103
virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override
Put a raw value (byte array) in the table.
Definition: NetworkTable.cpp:429
virtual std::vector< double > GetNumberArray(llvm::StringRef key, llvm::ArrayRef< double > defaultValue) const override
Returns the number array the key maps to.
Definition: NetworkTable.cpp:399
virtual std::vector< int > GetBooleanArray(llvm::StringRef key, llvm::ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
Definition: NetworkTable.cpp:380
virtual bool PutBooleanArray(llvm::StringRef key, llvm::ArrayRef< int > value) override
Put a boolean array in the table.
Definition: NetworkTable.cpp:372
void RemoveTableListener(ITableListener *listener) override
Remove a listener from receiving table events.
Definition: NetworkTable.cpp:191
static void SetPersistentFilename(llvm::StringRef filename)
Sets the persistent filename.
Definition: NetworkTable.cpp:57
static const char * SavePersistent(llvm::StringRef filename)
Saves persistent keys to a file.
Definition: NetworkTable.cpp:73
virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override
Put a string in the table.
Definition: NetworkTable.cpp:317
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
bool ContainsKey(llvm::StringRef key) const override
Determines whether the given key is in this table.
Definition: NetworkTable.cpp:209
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:38