WPILibC++  2019.2.1-24-g74f7ba0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
NetworkTable.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_NETWORKTABLE_H_
9 #define NTCORE_NETWORKTABLES_NETWORKTABLE_H_
10 
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include <wpi/ArrayRef.h>
18 #include <wpi/StringMap.h>
19 #include <wpi/Twine.h>
20 #include <wpi/mutex.h>
21 
22 #include "networktables/NetworkTableEntry.h"
23 #include "networktables/TableEntryListener.h"
24 #include "networktables/TableListener.h"
25 #include "ntcore_c.h"
26 #include "tables/ITable.h"
27 
28 namespace nt {
29 
30 using wpi::ArrayRef;
31 using wpi::StringRef;
32 using wpi::Twine;
33 
34 class NetworkTableInstance;
35 
36 #ifdef __GNUC__
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
39 #endif
40 
51 class NetworkTable final : public ITable {
52  private:
53  NT_Inst m_inst;
54  std::string m_path;
55  mutable wpi::mutex m_mutex;
56  mutable wpi::StringMap<NT_Entry> m_entries;
57  typedef std::pair<ITableListener*, NT_EntryListener> Listener;
58  std::vector<Listener> m_listeners;
59  std::vector<NT_EntryListener> m_lambdaListeners;
60 
61  static std::vector<std::string> s_ip_addresses;
62  static std::string s_persistent_filename;
63  static bool s_client;
64  static bool s_enable_ds;
65  static bool s_running;
66  static unsigned int s_port;
67 
68  struct private_init {};
69  friend class NetworkTableInstance;
70 
71  public:
79  static StringRef BasenameKey(StringRef key);
80 
97  static std::string NormalizeKey(const Twine& key,
98  bool withLeadingSlash = true);
99 
100  static StringRef NormalizeKey(const Twine& key,
102  bool withLeadingSlash = true);
103 
112  static std::vector<std::string> GetHierarchy(const Twine& key);
113 
118  NetworkTable(NT_Inst inst, const Twine& path, const private_init&);
119  virtual ~NetworkTable();
120 
127 
131  static const char PATH_SEPARATOR_CHAR;
132 
137  "use NetworkTableInstance::StartServer() or "
138  "NetworkTableInstance::StartClient() instead")
139  static void Initialize();
140 
145  "use NetworkTableInstance::StopServer() or "
146  "NetworkTableInstance::StopClient() instead")
147  static void Shutdown();
148 
154  static void SetClientMode();
155 
161  static void SetServerMode();
162 
173  static void SetTeam(int team);
174 
180  "use NetworkTableInstance::SetServer() or "
181  "NetworkTableInstance::StartClient() instead")
182  static void SetIPAddress(StringRef address);
183 
189  "use NetworkTableInstance::SetServer() or "
190  "NetworkTableInstance::StartClient() instead")
191  static void SetIPAddress(ArrayRef<std::string> addresses);
192 
200  "use the appropriate parameters to NetworkTableInstance::SetServer(), "
203  "NetworkTableInstance::StartDSClient() instead")
204  static void SetPort(unsigned int port);
205 
212  "use NetworkTableInstance::StartDSClient() and "
213  "NetworkTableInstance::StopDSClient() instead")
214  static void SetDSClientEnabled(bool enabled);
215 
223  "use the appropriate parameter to NetworkTableInstance::StartServer() "
224  "instead")
225  static void SetPersistentFilename(StringRef filename);
226 
234  static void SetNetworkIdentity(StringRef name);
235 
240  static void GlobalDeleteAll();
241 
248  WPI_DEPRECATED("use NetworkTableInstance::Flush() instead")
249  static void Flush();
250 
258  static void SetUpdateRate(double interval);
259 
267  static const char* SavePersistent(StringRef filename);
268 
277  static const char* LoadPersistent(
278  StringRef filename,
279  std::function<void(size_t line, const char* msg)> warn);
280 
291  "use NetworkTableInstance::GetTable() or "
292  "NetworkTableInstance::GetEntry() instead")
293  static std::shared_ptr<NetworkTable> GetTable(StringRef key);
294 
301  NetworkTableEntry GetEntry(const Twine& key) const;
302 
310  NT_EntryListener AddEntryListener(TableEntryListener listener,
311  unsigned int flags) const;
312 
321  NT_EntryListener AddEntryListener(const Twine& key,
322  TableEntryListener listener,
323  unsigned int flags) const;
324 
330  void RemoveEntryListener(NT_EntryListener listener) const;
331 
341  NT_EntryListener AddSubTableListener(TableListener listener,
342  bool localNotify = false);
343 
349  void RemoveTableListener(NT_EntryListener listener);
350 
352  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
353  "NT_NOTIFY_UPDATE")
354  void AddTableListener(ITableListener* listener) override;
355 
357  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
358  "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
359  void AddTableListener(ITableListener* listener,
360  bool immediateNotify) override;
361 
362  WPI_DEPRECATED("use AddEntryListener() instead")
363  void AddTableListenerEx(ITableListener* listener,
364  unsigned int flags) override;
365 
366  WPI_DEPRECATED("use AddEntryListener() instead")
367  void AddTableListener(StringRef key, ITableListener* listener,
368  bool immediateNotify) override;
369 
370  WPI_DEPRECATED("use AddEntryListener() instead")
371  void AddTableListenerEx(StringRef key, ITableListener* listener,
372  unsigned int flags) override;
373 
374  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
375  void AddSubTableListener(ITableListener* listener) override;
376 
377  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
378  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
379 
380  WPI_DEPRECATED("use RemoveTableListener(NT_EntryListener) instead")
381  void RemoveTableListener(ITableListener* listener) override;
382 
390  std::shared_ptr<NetworkTable> GetSubTable(const Twine& key) const override;
391 
398  bool ContainsKey(const Twine& key) const override;
399 
408  bool ContainsSubTable(const Twine& key) const override;
409 
416  std::vector<std::string> GetKeys(int types = 0) const override;
417 
423  std::vector<std::string> GetSubTables() const override;
424 
430  void SetPersistent(StringRef key) override;
431 
438  void ClearPersistent(StringRef key) override;
439 
446  bool IsPersistent(StringRef key) const override;
447 
455  void SetFlags(StringRef key, unsigned int flags) override;
456 
464  void ClearFlags(StringRef key, unsigned int flags) override;
465 
472  unsigned int GetFlags(StringRef key) const override;
473 
479  void Delete(const Twine& key) override;
480 
488  bool PutNumber(StringRef key, double value) override;
489 
497  bool SetDefaultNumber(StringRef key, double defaultValue) override;
498 
507  double GetNumber(StringRef key, double defaultValue) const override;
508 
516  bool PutString(StringRef key, StringRef value) override;
517 
525  bool SetDefaultString(StringRef key, StringRef defaultValue) override;
526 
536  std::string GetString(StringRef key, StringRef defaultValue) const override;
537 
545  bool PutBoolean(StringRef key, bool value) override;
546 
554  bool SetDefaultBoolean(StringRef key, bool defaultValue) override;
555 
565  bool GetBoolean(StringRef key, bool defaultValue) const override;
566 
578  bool PutBooleanArray(StringRef key, ArrayRef<int> value) override;
579 
588  ArrayRef<int> defaultValue) override;
589 
606  std::vector<int> GetBooleanArray(StringRef key,
607  ArrayRef<int> defaultValue) const override;
608 
616  bool PutNumberArray(StringRef key, ArrayRef<double> value) override;
617 
626  ArrayRef<double> defaultValue) override;
627 
640  std::vector<double> GetNumberArray(
641  StringRef key, ArrayRef<double> defaultValue) const override;
642 
650  bool PutStringArray(StringRef key, ArrayRef<std::string> value) override;
651 
660  ArrayRef<std::string> defaultValue) override;
661 
674  std::vector<std::string> GetStringArray(
675  StringRef key, ArrayRef<std::string> defaultValue) const override;
676 
684  bool PutRaw(StringRef key, StringRef value) override;
685 
693  bool SetDefaultRaw(StringRef key, StringRef defaultValue) override;
694 
707  std::string GetRaw(StringRef key, StringRef defaultValue) const override;
708 
716  bool PutValue(const Twine& key, std::shared_ptr<Value> value) override;
717 
725  bool SetDefaultValue(const Twine& key,
726  std::shared_ptr<Value> defaultValue) override;
727 
735  std::shared_ptr<Value> GetValue(const Twine& key) const override;
736 
742  StringRef GetPath() const override;
743 
751  const char* SaveEntries(const Twine& filename) const;
752 
761  const char* LoadEntries(
762  const Twine& filename,
763  std::function<void(size_t line, const char* msg)> warn);
764 };
765 
766 #ifdef __GNUC__
767 #pragma GCC diagnostic pop
768 #endif
769 
770 } // namespace nt
771 
772 // For backwards compatability
773 #ifndef NAMESPACED_NT
774 using nt::NetworkTable; // NOLINT
775 #endif
776 
777 #endif // NTCORE_NETWORKTABLES_NETWORKTABLE_H_
bool SetDefaultNumber(StringRef key, double defaultValue) override
Gets the current value in the table, setting it if it does not exist.
bool PutBoolean(StringRef key, bool value) override
Put a boolean in the table.
std::shared_ptr< NetworkTable > GetSubTable(const Twine &key) const override
Returns the table at the specified key.
std::string GetString(StringRef key, StringRef defaultValue) const override
Gets the string associated with the given name.
void ClearPersistent(StringRef key) override
Stop making a key's value persistent through program restarts.
std::vector< std::string > GetStringArray(StringRef key, ArrayRef< std::string > defaultValue) const override
Returns the string array the key maps to.
double GetNumber(StringRef key, double defaultValue) const override
Gets the number associated with the given name.
static StringRef BasenameKey(StringRef key)
Gets the "base name" of a key.
void RemoveEntryListener(NT_EntryListener listener) const
Remove an entry listener.
bool SetDefaultStringArray(StringRef key, ArrayRef< std::string > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
bool PutBooleanArray(StringRef key, ArrayRef< int > value) override
Put a boolean array in the table.
static void SetNetworkIdentity(StringRef name)
Sets the network identity.
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:131
const char * SaveEntries(const Twine &filename) const
Save table values to a file.
void SetPersistent(StringRef key) override
Makes a key's value persistent through program restarts.
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
Definition: optional.h:885
StringRef GetPath() const override
Gets the full path of this table.
bool ContainsKey(const Twine &key) const override
Determines whether the given key is in this table.
bool PutRaw(StringRef key, StringRef value) override
Put a raw value (byte array) in the table.
bool SetDefaultValue(const Twine &key, std::shared_ptr< Value > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
const char * LoadEntries(const Twine &filename, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
void StartDSClient(unsigned int port)
Starts requesting server address from Driver Station.
std::vector< int > GetBooleanArray(StringRef key, ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
bool PutNumber(StringRef key, double value) override
Put a number in the table.
std::string GetRaw(StringRef key, StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
NetworkTables Instance.
Definition: NetworkTableInstance.h:53
static std::vector< std::string > GetHierarchy(const Twine &key)
Gets a list of the names of all the super tables of a given key.
void StartServer(StringRef persist_filename, const char *listen_address, unsigned int port)
Starts a server using the specified filename, listening address, and port.
std::shared_ptr< Value > GetValue(const Twine &key) const override
Gets the value associated with a key as an object.
bool ContainsSubTable(const Twine &key) const override
Determines whether there exists a non-empty subtable for this key in this table.
void StopServer()
Stops the server if it is running.
std::vector< std::string > GetKeys(int types=0) const override
Gets all keys in the table (not including sub-tables).
NT_Entry GetEntry(NT_Inst inst, const Twine &name)
Get Entry Handle.
bool SetDefaultBoolean(StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
bool IsPersistent(StringRef key) const override
Returns whether the value is persistent through program restarts.
static void Flush()
Flushes all updated values immediately to the network.
NetworkTables (ntcore) namespace.
Definition: ITable.h:21
void StopClient()
Stops the client if it is running.
void DeleteAllEntries()
Delete All Entries.
bool PutString(StringRef key, StringRef value) override
Put a string in the table.
bool GetBoolean(StringRef key, bool defaultValue) const override
Gets the boolean associated with the given name.
static const char * SavePersistent(StringRef filename)
Saves persistent keys to a file.
bool PutStringArray(StringRef key, ArrayRef< std::string > value) override
Put a string array in the table.
void ClearFlags(StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
void StartClient()
Starts a client.
std::function< void(NetworkTable *table, StringRef name, NetworkTableEntry entry, std::shared_ptr< Value > value, int flags)> TableEntryListener
A listener that listens to changes in values in a NetworkTable.
Definition: TableEntryListener.h:41
NetworkTable(NT_Inst inst, const Twine &path, const private_init &)
Constructor.
NetworkTableInstance GetInstance() const
Gets the instance for the table.
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable ...
NT_EntryListener AddSubTableListener(TableListener listener, bool localNotify=false)
Listen for sub-table creation.
std::vector< std::string > GetSubTables() const override
Gets the names of all subtables in the table.
bool SetDefaultRaw(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
void StartClientTeam(NT_Inst inst, unsigned int team, unsigned int port)
Starts a client using commonly known robot addresses for the specified team.
WPI_DEPRECATED("use NetworkTableInstance::StartServer() or ""NetworkTableInstance::StartClient() instead") static void Initialize()
Initializes network tables.
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
bool SetDefaultNumberArray(StringRef key, ArrayRef< double > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable ...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Loads persistent keys from a file.
static void SetUpdateRate(double interval)
Set the periodic update rate.
bool SetDefaultString(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
NetworkTables Entry.
Definition: NetworkTableEntry.h:38
void SetFlags(StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
std::vector< double > GetNumberArray(StringRef key, ArrayRef< double > defaultValue) const override
Returns the number array the key maps to.
NT_EntryListener AddEntryListener(TableEntryListener listener, unsigned int flags) const
Listen to keys only within this table.
static std::string NormalizeKey(const Twine &key, bool withLeadingSlash=true)
Normalizes an network table key to contain no consecutive slashes and optionally start with a leading...
bool PutNumberArray(StringRef key, ArrayRef< double > value) override
Put a number array in the table.
A network table entry value.
Definition: NetworkTableValue.h:36
A network table that knows its subtable path.
Definition: NetworkTable.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool SetDefaultBooleanArray(StringRef key, ArrayRef< int > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
bool PutValue(const Twine &key, std::shared_ptr< Value > value) override
Put a value in the table.
void StopDSClient()
Stops requesting server address from Driver Station.
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
void Delete(const Twine &key) override
Deletes the specified key in this table.
void RemoveTableListener(NT_EntryListener listener)
Remove a sub-table listener.
unsigned int GetFlags(StringRef key) const override
Returns the flags for the specified key.
std::function< void(NetworkTable *parent, StringRef name, std::shared_ptr< NetworkTable > table)> TableListener
A listener that listens to new sub-tables in a NetworkTable.
Definition: TableListener.h:35