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::vector<std::string> s_ip_addresses;
30  static std::string s_persistent_filename;
31  static bool s_client;
32  static bool s_enable_ds;
33  static bool s_running;
34  static unsigned int s_port;
35 
36  public:
37  NetworkTable(llvm::StringRef path, const private_init&);
38  virtual ~NetworkTable();
39 
44  static const char PATH_SEPARATOR_CHAR;
45 
49  static void Initialize();
50  static void Shutdown();
51 
56  static void SetClientMode();
57 
62  static void SetServerMode();
63 
70  static void SetTeam(int team);
71 
76  static void SetIPAddress(llvm::StringRef address);
77 
82  static void SetIPAddress(llvm::ArrayRef<std::string> addresses);
83 
88  static void SetPort(unsigned int port);
89 
94  static void SetDSClientEnabled(bool enabled);
95 
101  static void SetPersistentFilename(llvm::StringRef filename);
102 
108  static void SetNetworkIdentity(llvm::StringRef name);
109 
113  static void GlobalDeleteAll();
114 
121  static void Flush();
122 
128  static void SetUpdateRate(double interval);
129 
136  static const char* SavePersistent(llvm::StringRef filename);
137 
145  static const char* LoadPersistent(
146  llvm::StringRef filename,
147  std::function<void(size_t line, const char* msg)> warn);
148 
159  static std::shared_ptr<NetworkTable> GetTable(llvm::StringRef key);
160 
161  void AddTableListener(ITableListener* listener) override;
162  void AddTableListener(ITableListener* listener,
163  bool immediateNotify) override;
164  void AddTableListenerEx(ITableListener* listener,
165  unsigned int flags) override;
166  void AddTableListener(llvm::StringRef key, ITableListener* listener,
167  bool immediateNotify) override;
168  void AddTableListenerEx(llvm::StringRef key, ITableListener* listener,
169  unsigned int flags) override;
170  void AddSubTableListener(ITableListener* listener) override;
171  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
172  void RemoveTableListener(ITableListener* listener) override;
173 
182  std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const override;
183 
190  bool ContainsKey(llvm::StringRef key) const override;
191 
200  bool ContainsSubTable(llvm::StringRef key) const override;
201 
206  std::vector<std::string> GetKeys(int types = 0) const override;
207 
211  std::vector<std::string> GetSubTables() const override;
212 
218  void SetPersistent(llvm::StringRef key) override;
219 
226  void ClearPersistent(llvm::StringRef key) override;
227 
234  bool IsPersistent(llvm::StringRef key) const override;
235 
243  void SetFlags(llvm::StringRef key, unsigned int flags) override;
244 
252  void ClearFlags(llvm::StringRef key, unsigned int flags) override;
253 
260  unsigned int GetFlags(llvm::StringRef key) const override;
261 
267  void Delete(llvm::StringRef key) override;
268 
276  bool PutNumber(llvm::StringRef key, double value) override;
277 
284  virtual bool SetDefaultNumber(llvm::StringRef key,
285  double defaultValue) override;
286 
297  WPI_DEPRECATED(
298  "Raises an exception if key not found; "
299  "use GetNumber(StringRef key, double defaultValue) instead")
300  virtual double GetNumber(llvm::StringRef key) const override;
301 
310  virtual double GetNumber(llvm::StringRef key,
311  double defaultValue) const override;
312 
320  virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override;
321 
328  virtual bool SetDefaultString(llvm::StringRef key,
329  llvm::StringRef defaultValue) override;
330 
341  WPI_DEPRECATED(
342  "Raises an exception if key not found; "
343  "use GetString(StringRef key, StringRef defaultValue) instead")
344  virtual std::string GetString(llvm::StringRef key) const override;
345 
355  virtual std::string GetString(llvm::StringRef key,
356  llvm::StringRef defaultValue) const override;
357 
365  virtual bool PutBoolean(llvm::StringRef key, bool value) override;
366 
373  virtual bool SetDefaultBoolean(llvm::StringRef key,
374  bool defaultValue) override;
375 
386  WPI_DEPRECATED(
387  "Raises an exception if key not found; "
388  "use GetBoolean(StringRef key, bool defaultValue) instead")
389  virtual bool GetBoolean(llvm::StringRef key) const override;
390 
400  virtual bool GetBoolean(llvm::StringRef key,
401  bool defaultValue) const override;
402 
413  virtual bool PutBooleanArray(llvm::StringRef key,
414  llvm::ArrayRef<int> value) override;
415 
422  virtual bool SetDefaultBooleanArray(
423  llvm::StringRef key, llvm::ArrayRef<int> defaultValue) override;
424 
440  virtual std::vector<int> GetBooleanArray(
441  llvm::StringRef key, llvm::ArrayRef<int> defaultValue) const override;
442 
449  virtual bool PutNumberArray(llvm::StringRef key,
450  llvm::ArrayRef<double> value) override;
451 
458  virtual bool SetDefaultNumberArray(
459  llvm::StringRef key, llvm::ArrayRef<double> defaultValue) override;
460 
472  virtual std::vector<double> GetNumberArray(
473  llvm::StringRef key, llvm::ArrayRef<double> defaultValue) const override;
474 
481  virtual bool PutStringArray(llvm::StringRef key,
482  llvm::ArrayRef<std::string> value) override;
483 
490  virtual bool SetDefaultStringArray(
491  llvm::StringRef key, llvm::ArrayRef<std::string> defaultValue) override;
492 
504  virtual std::vector<std::string> GetStringArray(
505  llvm::StringRef key,
506  llvm::ArrayRef<std::string> defaultValue) const override;
507 
514  virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override;
515 
522  virtual bool SetDefaultRaw(llvm::StringRef key,
523  llvm::StringRef defaultValue) override;
524 
536  virtual std::string GetRaw(llvm::StringRef key,
537  llvm::StringRef defaultValue) const override;
538 
546  bool PutValue(llvm::StringRef key, std::shared_ptr<nt::Value> value) override;
547 
554  virtual bool SetDefaultValue(
555  llvm::StringRef key, std::shared_ptr<nt::Value> defaultValue) override;
556 
564  std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const override;
565 };
566 
567 #endif // NETWORKTABLE_H_
static void SetIPAddress(llvm::StringRef address)
static void SetPort(unsigned int port)
Definition: NetworkTable.cpp:112
A table whose values can be read and written to.
Definition: ITable.h:22
virtual bool PutNumberArray(llvm::StringRef key, llvm::ArrayRef< double > value) override
Put a number array in the table.
Definition: NetworkTable.cpp:480
virtual bool SetDefaultStringArray(llvm::StringRef key, llvm::ArrayRef< std::string > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:515
virtual bool SetDefaultNumber(llvm::StringRef key, double defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:360
virtual bool SetDefaultNumberArray(llvm::StringRef key, llvm::ArrayRef< double > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:488
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:44
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:524
std::vector< std::string > GetSubTables() const override
Definition: NetworkTable.cpp:300
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:43
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:45
void Delete(llvm::StringRef key) override
Deletes the specified key in this table.
Definition: NetworkTable.cpp:346
A network table that knows its subtable path.
Definition: NetworkTable.h:20
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
Definition: NetworkTable.cpp:130
virtual bool SetDefaultRaw(llvm::StringRef key, llvm::StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:541
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:548
void AddTableListenerEx(ITableListener *listener, unsigned int flags) override
Add a listener for changes to the table.
Definition: NetworkTable.cpp:178
static void SetNetworkIdentity(llvm::StringRef name)
Sets the network identity.
Definition: NetworkTable.cpp:126
bool PutNumber(llvm::StringRef key, double value) override
Put a number in the table.
Definition: NetworkTable.cpp:353
std::vector< std::string > GetKeys(int types=0) const override
Definition: NetworkTable.cpp:288
static void Initialize()
Definition: NetworkTable.cpp:21
static void SetDSClientEnabled(bool enabled)
Definition: NetworkTable.cpp:114
bool IsPersistent(llvm::StringRef key) const override
Returns whether the value is persistent through program restarts.
Definition: NetworkTable.cpp:321
static std::shared_ptr< NetworkTable > GetTable(llvm::StringRef key)
Gets the table with the specified key.
Definition: NetworkTable.cpp:148
void ClearFlags(llvm::StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
Definition: NetworkTable.cpp:332
virtual bool SetDefaultBooleanArray(llvm::StringRef key, llvm::ArrayRef< int > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:461
void SetFlags(llvm::StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
Definition: NetworkTable.cpp:325
static void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTable.cpp:134
virtual bool SetDefaultBoolean(llvm::StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:427
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:142
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:280
static void Flush()
Flushes all updated values immediately to the network.
Definition: NetworkTable.cpp:132
void AddSubTableListener(ITableListener *listener) override
This will immediately notify the listener of all current sub tables.
Definition: NetworkTable.cpp:221
virtual bool PutStringArray(llvm::StringRef key, llvm::ArrayRef< std::string > value) override
Put a string array in the table.
Definition: NetworkTable.cpp:507
virtual bool PutBoolean(llvm::StringRef key, bool value) override
Put a boolean in the table.
Definition: NetworkTable.cpp:420
virtual bool SetDefaultString(llvm::StringRef key, llvm::StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:393
unsigned int GetFlags(llvm::StringRef key) const override
Returns the flags for the specified key.
Definition: NetworkTable.cpp:339
std::shared_ptr< ITable > GetSubTable(llvm::StringRef key) const override
Returns the table at the specified key.
Definition: NetworkTable.cpp:266
std::shared_ptr< nt::Value > GetValue(llvm::StringRef key) const override
Gets the value associated with a key as an object.
Definition: NetworkTable.cpp:573
bool PutValue(llvm::StringRef key, std::shared_ptr< nt::Value > value) override
Put a value in the table.
Definition: NetworkTable.cpp:558
void SetPersistent(llvm::StringRef key) override
Makes a key's value persistent through program restarts.
Definition: NetworkTable.cpp:313
void ClearPersistent(llvm::StringRef key) override
Stop making a key's value persistent through program restarts.
Definition: NetworkTable.cpp:317
void AddTableListener(ITableListener *listener) override
Add a listener for changes to the table.
Definition: NetworkTable.cpp:167
virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override
Put a raw value (byte array) in the table.
Definition: NetworkTable.cpp:534
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:497
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:470
virtual bool PutBooleanArray(llvm::StringRef key, llvm::ArrayRef< int > value) override
Put a boolean array in the table.
Definition: NetworkTable.cpp:453
void RemoveTableListener(ITableListener *listener) override
Remove a listener from receiving table events.
Definition: NetworkTable.cpp:255
static void SetPersistentFilename(llvm::StringRef filename)
Sets the persistent filename.
Definition: NetworkTable.cpp:122
static const char * SavePersistent(llvm::StringRef filename)
Saves persistent keys to a file.
Definition: NetworkTable.cpp:138
virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override
Put a string in the table.
Definition: NetworkTable.cpp:386
virtual bool SetDefaultValue(llvm::StringRef key, std::shared_ptr< nt::Value > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:565
bool ContainsKey(llvm::StringRef key) const override
Determines whether the given key is in this table.
Definition: NetworkTable.cpp:273
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:41