WPILibC++  unspecified
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 "llvm/StringMap.h"
16 #include "networktables/NetworkTableEntry.h"
17 #include "networktables/TableEntryListener.h"
18 #include "networktables/TableListener.h"
19 #include "ntcore_c.h"
20 #include "tables/ITable.h"
21 
22 namespace nt {
23 
24 using llvm::ArrayRef;
25 using llvm::StringRef;
26 
27 class NetworkTableInstance;
28 
29 #ifdef __GNUC__
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #endif
33 
37 class NetworkTable final : public ITable {
38  private:
39  NT_Inst m_inst;
40  std::string m_path;
41  mutable std::mutex m_mutex;
42  mutable llvm::StringMap<NT_Entry> m_entries;
43  typedef std::pair<ITableListener*, NT_EntryListener> Listener;
44  std::vector<Listener> m_listeners;
45 
46  static std::vector<std::string> s_ip_addresses;
47  static std::string s_persistent_filename;
48  static bool s_client;
49  static bool s_enable_ds;
50  static bool s_running;
51  static unsigned int s_port;
52 
53  public:
54  NetworkTable(NT_Inst inst, StringRef path);
55  virtual ~NetworkTable();
56 
62 
66  static const char PATH_SEPARATOR_CHAR;
67 
72  "use NetworkTableInstance::StartServer() or "
73  "NetworkTableInstance::StartClient() instead")
74  static void Initialize();
75 
80  "use NetworkTableInstance::StopServer() or "
81  "NetworkTableInstance::StopClient() instead")
82  static void Shutdown();
83 
89  static void SetClientMode();
90 
96  static void SetServerMode();
97 
107  static void SetTeam(int team);
108 
114  "use NetworkTableInstance::SetServer() or "
115  "NetworkTableInstance::StartClient() instead")
116  static void SetIPAddress(StringRef address);
117 
123  "use NetworkTableInstance::SetServer() or "
124  "NetworkTableInstance::StartClient() instead")
125  static void SetIPAddress(ArrayRef<std::string> addresses);
126 
133  "use the appropriate parameters to NetworkTableInstance::SetServer(), "
136  "NetworkTableInstance::StartDSClient() instead")
137  static void SetPort(unsigned int port);
138 
144  "use NetworkTableInstance::StartDSClient() and "
145  "NetworkTableInstance::StopDSClient() instead")
146  static void SetDSClientEnabled(bool enabled);
147 
154  "use the appropriate parameter to NetworkTableInstance::StartServer() "
155  "instead")
156  static void SetPersistentFilename(StringRef filename);
157 
164  static void SetNetworkIdentity(StringRef name);
165 
170  static void GlobalDeleteAll();
171 
178  WPI_DEPRECATED("use NetworkTableInstance::Flush() instead")
179  static void Flush();
180 
188  static void SetUpdateRate(double interval);
189 
197  static const char* SavePersistent(StringRef filename);
198 
207  static const char* LoadPersistent(
208  StringRef filename,
209  std::function<void(size_t line, const char* msg)> warn);
210 
221  "use NetworkTableInstance::GetTable() or "
222  "NetworkTableInstance::GetEntry() instead")
223  static std::shared_ptr<NetworkTable> GetTable(StringRef key);
224 
231 
238  NT_EntryListener AddEntryListener(TableEntryListener listener,
239  unsigned int flags) const;
240 
248  NT_EntryListener AddEntryListener(StringRef key, TableEntryListener listener,
249  unsigned int flags) const;
250 
255  void RemoveEntryListener(NT_EntryListener listener) const;
256 
265  NT_EntryListener AddSubTableListener(TableListener listener,
266  bool localNotify = false) const;
267 
272  void RemoveTableListener(NT_EntryListener listener) const;
273 
275  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
276  "NT_NOTIFY_UPDATE")
277  void AddTableListener(ITableListener* listener) override;
278 
280  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
281  "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
282  void AddTableListener(ITableListener* listener,
283  bool immediateNotify) override;
284 
285  WPI_DEPRECATED("use AddEntryListener() instead")
286  void AddTableListenerEx(ITableListener* listener,
287  unsigned int flags) override;
288 
289  WPI_DEPRECATED("use AddEntryListener() instead")
290  void AddTableListener(StringRef key, ITableListener* listener,
291  bool immediateNotify) override;
292 
293  WPI_DEPRECATED("use AddEntryListener() instead")
294  void AddTableListenerEx(StringRef key, ITableListener* listener,
295  unsigned int flags) override;
296 
297  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
298  void AddSubTableListener(ITableListener* listener) override;
299 
300  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
301  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
302 
303  WPI_DEPRECATED("use RemoveTableListener(NT_EntryListener) instead")
304  void RemoveTableListener(ITableListener* listener) override;
305 
313  std::shared_ptr<NetworkTable> GetSubTable(StringRef key) const override;
314 
321  bool ContainsKey(StringRef key) const override;
322 
331  bool ContainsSubTable(StringRef key) const override;
332 
338  std::vector<std::string> GetKeys(int types = 0) const override;
339 
344  std::vector<std::string> GetSubTables() const override;
345 
351  void SetPersistent(StringRef key) override;
352 
359  void ClearPersistent(StringRef key) override;
360 
367  bool IsPersistent(StringRef key) const override;
368 
376  void SetFlags(StringRef key, unsigned int flags) override;
377 
385  void ClearFlags(StringRef key, unsigned int flags) override;
386 
393  unsigned int GetFlags(StringRef key) const override;
394 
400  void Delete(StringRef key) override;
401 
409  bool PutNumber(StringRef key, double value) override;
410 
417  bool SetDefaultNumber(StringRef key, double defaultValue) override;
418 
427  double GetNumber(StringRef key, double defaultValue) const override;
428 
436  bool PutString(StringRef key, StringRef value) override;
437 
444  bool SetDefaultString(StringRef key, StringRef defaultValue) override;
445 
455  std::string GetString(StringRef key, StringRef defaultValue) const override;
456 
464  bool PutBoolean(StringRef key, bool value) override;
465 
472  bool SetDefaultBoolean(StringRef key, bool defaultValue) override;
473 
483  bool GetBoolean(StringRef key, bool defaultValue) const override;
484 
495  bool PutBooleanArray(StringRef key, ArrayRef<int> value) override;
496 
504  ArrayRef<int> defaultValue) override;
505 
521  std::vector<int> GetBooleanArray(StringRef key,
522  ArrayRef<int> defaultValue) const override;
523 
530  bool PutNumberArray(StringRef key, ArrayRef<double> value) override;
531 
539  ArrayRef<double> defaultValue) override;
540 
552  std::vector<double> GetNumberArray(
553  StringRef key, ArrayRef<double> defaultValue) const override;
554 
561  bool PutStringArray(StringRef key, ArrayRef<std::string> value) override;
562 
570  ArrayRef<std::string> defaultValue) override;
571 
583  std::vector<std::string> GetStringArray(
584  StringRef key, ArrayRef<std::string> defaultValue) const override;
585 
592  bool PutRaw(StringRef key, StringRef value) override;
593 
600  bool SetDefaultRaw(StringRef key, StringRef defaultValue) override;
601 
613  std::string GetRaw(StringRef key, StringRef defaultValue) const override;
614 
622  bool PutValue(StringRef key, std::shared_ptr<Value> value) override;
623 
630  bool SetDefaultValue(StringRef key,
631  std::shared_ptr<Value> defaultValue) override;
632 
640  std::shared_ptr<Value> GetValue(StringRef key) const override;
641 
646  StringRef GetPath() const override;
647 
654  const char* SaveEntries(StringRef filename) const;
655 
663  const char* LoadEntries(
664  StringRef filename,
665  std::function<void(size_t line, const char* msg)> warn);
666 };
667 
668 #ifdef __GNUC__
669 #pragma GCC diagnostic pop
670 #endif
671 
672 } // namespace nt
673 
674 // For backwards compatability
675 #ifndef NAMESPACED_NT
676 using nt::NetworkTable; // NOLINT
677 #endif
678 
679 #endif // NETWORKTABLE_H_
std::shared_ptr< Value > GetValue(StringRef key) const override
Gets the value associated with a key as an object.
Definition: NetworkTable.cpp:458
void RemoveEntryListener(NT_EntryListener listener) const
Remove an entry listener.
Definition: NetworkTable.cpp:180
bool PutNumberArray(StringRef key, ArrayRef< double > value) override
Put a number array in the table.
Definition: NetworkTable.cpp:406
std::vector< double > GetNumberArray(StringRef key, ArrayRef< double > defaultValue) const override
Returns the number array the key maps to.
Definition: NetworkTable.cpp:416
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
Definition: ntcore_cpp.cpp:821
bool SetDefaultString(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:370
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:49
static void SetNetworkIdentity(StringRef name)
Sets the network identity.
Definition: NetworkTable.cpp:98
void SetPersistent(StringRef key) override
Makes a key&#39;s value persistent through program restarts.
Definition: NetworkTable.cpp:328
double GetNumber(StringRef key, double defaultValue) const override
Gets the number associated with the given name.
Definition: NetworkTable.cpp:362
void RemoveTableListener(NT_EntryListener listener) const
Remove a sub-table listener.
bool PutRaw(StringRef key, StringRef value) override
Put a raw value (byte array) in the table.
Definition: NetworkTable.cpp:436
Definition: json.cpp:1170
static void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTable.cpp:108
bool SetDefaultNumberArray(StringRef key, ArrayRef< double > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:411
bool ContainsSubTable(StringRef key) const override
Determines whether there exists a non-empty subtable for this key in this table.
Definition: NetworkTable.cpp:292
bool PutString(StringRef key, StringRef value) override
Put a string in the table.
Definition: NetworkTable.cpp:366
static void Flush()
Flushes all updated values immediately to the network.
Definition: NetworkTable.cpp:106
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:66
std::string GetString(StringRef key, StringRef defaultValue) const override
Gets the string associated with the given name.
Definition: NetworkTable.cpp:374
bool ContainsKey(StringRef key) const override
Determines whether the given key is in this table.
Definition: NetworkTable.cpp:288
NetworkTableInstance GetInstance() const
Gets the instance for the table.
Definition: NetworkTable.cpp:134
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
NetworkTables Instance.
Definition: NetworkTableInstance.h:59
bool SetDefaultBoolean(StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:383
NT_EntryListener AddSubTableListener(TableListener listener, bool localNotify=false) const
Listen for sub-table creation.
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
Definition: NetworkTable.cpp:102
void ClearPersistent(StringRef key) override
Stop making a key&#39;s value persistent through program restarts.
Definition: NetworkTable.cpp:332
std::vector< int > GetBooleanArray(StringRef key, ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
Definition: NetworkTable.cpp:401
void StopServer()
Stops the server if it is running.
Definition: ntcore_cpp.cpp:757
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:47
NT_Entry GetEntry(NT_Inst inst, StringRef name)
Get Entry Handle.
Definition: ntcore_cpp.cpp:55
void StartDSClient(unsigned int port)
Starts requesting server address from Driver Station.
Definition: ntcore_cpp.cpp:851
void StopDSClient()
Stops requesting server address from Driver Station.
Definition: ntcore_cpp.cpp:862
std::vector< std::string > GetStringArray(StringRef key, ArrayRef< std::string > defaultValue) const override
Returns the string array the key maps to.
Definition: NetworkTable.cpp:431
Definition: IEntryNotifier.h:15
NT_EntryListener AddEntryListener(TableEntryListener listener, unsigned int flags) const
Listen to keys only within this table.
Definition: NetworkTable.cpp:150
unsigned int GetFlags(StringRef key) const override
Returns the flags for the specified key.
Definition: NetworkTable.cpp:348
const char * LoadEntries(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
Definition: NetworkTable.cpp:470
bool PutStringArray(StringRef key, ArrayRef< std::string > value) override
Put a string array in the table.
Definition: NetworkTable.cpp:421
StringRef GetPath() const override
Gets the full path of this table.
Definition: NetworkTable.cpp:462
std::shared_ptr< NetworkTable > GetSubTable(StringRef key) const override
Returns the table at the specified key.
Definition: NetworkTable.cpp:281
std::vector< std::string > GetSubTables() const override
Gets the names of all subtables in the table.
Definition: NetworkTable.cpp:315
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
Definition: ntcore_cpp.cpp:844
void StartServer(StringRef persist_filename, const char *listen_address, unsigned int port)
Starts a server using the specified filename, listening address, and port.
Definition: ntcore_cpp.cpp:743
bool SetDefaultRaw(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:440
const char * SaveEntries(StringRef filename) const
Save table values to a file.
Definition: NetworkTable.cpp:464
static const char * SavePersistent(StringRef filename)
Saves persistent keys to a file.
Definition: NetworkTable.cpp:112
WPI_DEPRECATED("use NetworkTableInstance::StartServer() or ""NetworkTableInstance::StartClient() instead") static void Initialize()
Initializes network tables.
void DeleteAllEntries()
Delete All Entries.
Definition: ntcore_cpp.cpp:195
bool PutBooleanArray(StringRef key, ArrayRef< int > value) override
Put a boolean array in the table.
Definition: NetworkTable.cpp:391
bool GetBoolean(StringRef key, bool defaultValue) const override
Gets the boolean associated with the given name.
Definition: NetworkTable.cpp:387
bool IsPersistent(StringRef key) const override
Returns whether the value is persistent through program restarts.
Definition: NetworkTable.cpp:336
std::string GetRaw(StringRef key, StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
Definition: NetworkTable.cpp:444
bool SetDefaultNumber(StringRef key, double defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:358
NetworkTables Entry.
Definition: NetworkTableEntry.h:30
bool PutValue(StringRef key, std::shared_ptr< Value > value) override
Put a value in the table.
Definition: NetworkTable.cpp:449
std::vector< std::string > GetKeys(int types=0) const override
Gets all keys in the table (not including sub-tables).
Definition: NetworkTable.cpp:300
void ClearFlags(StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
Definition: NetworkTable.cpp:344
static const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Loads persistent keys from a file.
Definition: NetworkTable.cpp:116
bool PutNumber(StringRef key, double value) override
Put a number in the table.
Definition: NetworkTable.cpp:354
bool SetDefaultBooleanArray(StringRef key, ArrayRef< int > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:396
void SetFlags(StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
Definition: NetworkTable.cpp:340
void Delete(StringRef key) override
Deletes the specified key in this table.
Definition: NetworkTable.cpp:352
A network table entry value.
Definition: NetworkTableValue.h:30
A network table that knows its subtable path.
Definition: NetworkTable.h:37
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
bool PutBoolean(StringRef key, bool value) override
Put a boolean in the table.
Definition: NetworkTable.cpp:379
bool SetDefaultStringArray(StringRef key, ArrayRef< std::string > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:426
void StopClient()
Stops the client if it is running.
Definition: ntcore_cpp.cpp:812
void StartClient()
Starts a client.
Definition: ntcore_cpp.cpp:766
void StartClientTeam(NT_Inst inst, unsigned int team, unsigned int port)
Starts a client using commonly known robot addresses for the specified team.
Definition: ntcore_cpp.cpp:804
bool SetDefaultValue(StringRef key, std::shared_ptr< Value > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:453