WPILibC++  2020.3.2-60-g3011ebe
NetworkTable.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 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 #elif _WIN32
40 #pragma warning(push)
41 #pragma warning(disable : 4996)
42 #endif
43 
54 class NetworkTable final : public ITable {
55  private:
56  NT_Inst m_inst;
57  std::string m_path;
58  mutable wpi::mutex m_mutex;
59  mutable wpi::StringMap<NT_Entry> m_entries;
60  typedef std::pair<ITableListener*, NT_EntryListener> Listener;
61  std::vector<Listener> m_listeners;
62  std::vector<NT_EntryListener> m_lambdaListeners;
63 
64  static std::vector<std::string> s_ip_addresses;
65  static std::string s_persistent_filename;
66  static bool s_client;
67  static bool s_enable_ds;
68  static bool s_running;
69  static unsigned int s_port;
70 
71  struct private_init {};
72  friend class NetworkTableInstance;
73 
74  public:
82  static StringRef BasenameKey(StringRef key);
83 
100  static std::string NormalizeKey(const Twine& key,
101  bool withLeadingSlash = true);
102 
103  static StringRef NormalizeKey(const Twine& key,
105  bool withLeadingSlash = true);
106 
115  static std::vector<std::string> GetHierarchy(const Twine& key);
116 
121  NetworkTable(NT_Inst inst, const Twine& path, const private_init&);
122  virtual ~NetworkTable();
123 
130 
134  static const char PATH_SEPARATOR_CHAR;
135 
140  "use NetworkTableInstance::StartServer() or "
141  "NetworkTableInstance::StartClient() instead")
142  static void Initialize();
143 
148  "use NetworkTableInstance::StopServer() or "
149  "NetworkTableInstance::StopClient() instead")
150  static void Shutdown();
151 
157  static void SetClientMode();
158 
164  static void SetServerMode();
165 
176  static void SetTeam(int team);
177 
183  "use NetworkTableInstance::SetServer() or "
184  "NetworkTableInstance::StartClient() instead")
185  static void SetIPAddress(StringRef address);
186 
192  "use NetworkTableInstance::SetServer() or "
193  "NetworkTableInstance::StartClient() instead")
194  static void SetIPAddress(ArrayRef<std::string> addresses);
195 
203  "use the appropriate parameters to NetworkTableInstance::SetServer(), "
206  "NetworkTableInstance::StartDSClient() instead")
207  static void SetPort(unsigned int port);
208 
215  "use NetworkTableInstance::StartDSClient() and "
216  "NetworkTableInstance::StopDSClient() instead")
217  static void SetDSClientEnabled(bool enabled);
218 
226  "use the appropriate parameter to NetworkTableInstance::StartServer() "
227  "instead")
228  static void SetPersistentFilename(StringRef filename);
229 
237  static void SetNetworkIdentity(StringRef name);
238 
243  static void GlobalDeleteAll();
244 
251  WPI_DEPRECATED("use NetworkTableInstance::Flush() instead")
252  static void Flush();
253 
261  static void SetUpdateRate(double interval);
262 
270  static const char* SavePersistent(StringRef filename);
271 
280  static const char* LoadPersistent(
281  StringRef filename,
282  std::function<void(size_t line, const char* msg)> warn);
283 
294  "use NetworkTableInstance::GetTable() or "
295  "NetworkTableInstance::GetEntry() instead")
296  static std::shared_ptr<NetworkTable> GetTable(StringRef key);
297 
304  NetworkTableEntry GetEntry(const Twine& key) const;
305 
313  NT_EntryListener AddEntryListener(TableEntryListener listener,
314  unsigned int flags) const;
315 
324  NT_EntryListener AddEntryListener(const Twine& key,
325  TableEntryListener listener,
326  unsigned int flags) const;
327 
333  void RemoveEntryListener(NT_EntryListener listener) const;
334 
344  NT_EntryListener AddSubTableListener(TableListener listener,
345  bool localNotify = false);
346 
352  void RemoveTableListener(NT_EntryListener listener);
353 
355  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
356  "NT_NOTIFY_UPDATE")
357  void AddTableListener(ITableListener* listener) override;
358 
360  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
361  "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
362  void AddTableListener(ITableListener* listener,
363  bool immediateNotify) override;
364 
365  WPI_DEPRECATED("use AddEntryListener() instead")
366  void AddTableListenerEx(ITableListener* listener,
367  unsigned int flags) override;
368 
369  WPI_DEPRECATED("use AddEntryListener() instead")
370  void AddTableListener(StringRef key, ITableListener* listener,
371  bool immediateNotify) override;
372 
373  WPI_DEPRECATED("use AddEntryListener() instead")
374  void AddTableListenerEx(StringRef key, ITableListener* listener,
375  unsigned int flags) override;
376 
377  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
378  void AddSubTableListener(ITableListener* listener) override;
379 
380  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
381  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
382 
383  WPI_DEPRECATED("use RemoveTableListener(NT_EntryListener) instead")
384  void RemoveTableListener(ITableListener* listener) override;
385 
393  std::shared_ptr<NetworkTable> GetSubTable(const Twine& key) const override;
394 
401  bool ContainsKey(const Twine& key) const override;
402 
411  bool ContainsSubTable(const Twine& key) const override;
412 
419  std::vector<std::string> GetKeys(int types = 0) const override;
420 
426  std::vector<std::string> GetSubTables() const override;
427 
433  void SetPersistent(StringRef key) override;
434 
441  void ClearPersistent(StringRef key) override;
442 
449  bool IsPersistent(StringRef key) const override;
450 
458  void SetFlags(StringRef key, unsigned int flags) override;
459 
467  void ClearFlags(StringRef key, unsigned int flags) override;
468 
475  unsigned int GetFlags(StringRef key) const override;
476 
482  void Delete(const Twine& key) override;
483 
491  bool PutNumber(StringRef key, double value) override;
492 
500  bool SetDefaultNumber(StringRef key, double defaultValue) override;
501 
510  double GetNumber(StringRef key, double defaultValue) const override;
511 
519  bool PutString(StringRef key, StringRef value) override;
520 
528  bool SetDefaultString(StringRef key, StringRef defaultValue) override;
529 
539  std::string GetString(StringRef key, StringRef defaultValue) const override;
540 
548  bool PutBoolean(StringRef key, bool value) override;
549 
557  bool SetDefaultBoolean(StringRef key, bool defaultValue) override;
558 
568  bool GetBoolean(StringRef key, bool defaultValue) const override;
569 
581  bool PutBooleanArray(StringRef key, ArrayRef<int> value) override;
582 
591  ArrayRef<int> defaultValue) override;
592 
609  std::vector<int> GetBooleanArray(StringRef key,
610  ArrayRef<int> defaultValue) const override;
611 
619  bool PutNumberArray(StringRef key, ArrayRef<double> value) override;
620 
629  ArrayRef<double> defaultValue) override;
630 
643  std::vector<double> GetNumberArray(
644  StringRef key, ArrayRef<double> defaultValue) const override;
645 
653  bool PutStringArray(StringRef key, ArrayRef<std::string> value) override;
654 
663  ArrayRef<std::string> defaultValue) override;
664 
677  std::vector<std::string> GetStringArray(
678  StringRef key, ArrayRef<std::string> defaultValue) const override;
679 
687  bool PutRaw(StringRef key, StringRef value) override;
688 
696  bool SetDefaultRaw(StringRef key, StringRef defaultValue) override;
697 
710  std::string GetRaw(StringRef key, StringRef defaultValue) const override;
711 
719  bool PutValue(const Twine& key, std::shared_ptr<Value> value) override;
720 
728  bool SetDefaultValue(const Twine& key,
729  std::shared_ptr<Value> defaultValue) override;
730 
738  std::shared_ptr<Value> GetValue(const Twine& key) const override;
739 
745  StringRef GetPath() const override;
746 
754  const char* SaveEntries(const Twine& filename) const;
755 
764  const char* LoadEntries(
765  const Twine& filename,
766  std::function<void(size_t line, const char* msg)> warn);
767 };
768 
769 #ifdef __GNUC__
770 #pragma GCC diagnostic pop
771 #elif _WIN32
772 #pragma warning(pop)
773 #endif
774 
775 } // namespace nt
776 
777 // For backwards compatability
778 #ifndef NAMESPACED_NT
779 using nt::NetworkTable; // NOLINT
780 #endif
781 
782 #endif // NTCORE_NETWORKTABLES_NETWORKTABLE_H_
nt::NetworkTable
A network table that knows its subtable path.
Definition: NetworkTable.h:54
nt::StartClientTeam
void StartClientTeam(NT_Inst inst, unsigned int team, unsigned int port)
Starts a client using commonly known robot addresses for the specified team.
nt::NetworkTableInstance
NetworkTables Instance.
Definition: NetworkTableInstance.h:53
nt::SetServerTeam
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
nt::NetworkTable::PATH_SEPARATOR_CHAR
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:134
nt::NetworkTable::AddEntryListener
NT_EntryListener AddEntryListener(TableEntryListener listener, unsigned int flags) const
Listen to keys only within this table.
nt::NetworkTable::SetDefaultValue
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.
nt::NetworkTable::LoadEntries
const char * LoadEntries(const Twine &filename, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
nt::NetworkTable::PutValue
bool PutValue(const Twine &key, std::shared_ptr< Value > value) override
Put a value in the table.
nt::NetworkTable::RemoveEntryListener
void RemoveEntryListener(NT_EntryListener listener) const
Remove an entry listener.
nt::NetworkTable::PutNumberArray
bool PutNumberArray(StringRef key, ArrayRef< double > value) override
Put a number array in the table.
nt::NetworkTable::ContainsKey
bool ContainsKey(const Twine &key) const override
Determines whether the given key is in this table.
nt::NetworkTable::PutRaw
bool PutRaw(StringRef key, StringRef value) override
Put a raw value (byte array) in the table.
nt::NetworkTable::GetInstance
NetworkTableInstance GetInstance() const
Gets the instance for the table.
nt::NetworkTable::GlobalDeleteAll
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
nt::NetworkTable::GetHierarchy
static std::vector< std::string > GetHierarchy(const Twine &key)
Gets a list of the names of all the super tables of a given key.
nt::NetworkTable::GetBooleanArray
std::vector< int > GetBooleanArray(StringRef key, ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
nt::NetworkTable::SetDefaultNumber
bool SetDefaultNumber(StringRef key, double defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::PutBoolean
bool PutBoolean(StringRef key, bool value) override
Put a boolean in the table.
nt::NetworkTable::SetDefaultBoolean
bool SetDefaultBoolean(StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::SmallVectorImpl< char >
nt::NetworkTable::ContainsSubTable
bool ContainsSubTable(const Twine &key) const override
Determines whether there exists a non-empty subtable for this key in this table.
nt::TableListener
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
nt::NetworkTable::BasenameKey
static StringRef BasenameKey(StringRef key)
Gets the "base name" of a key.
nt::NetworkTable::ClearPersistent
void ClearPersistent(StringRef key) override
Stop making a key's value persistent through program restarts.
nt::NetworkTable::IsPersistent
bool IsPersistent(StringRef key) const override
Returns whether the value is persistent through program restarts.
nt::NetworkTable::GetStringArray
std::vector< std::string > GetStringArray(StringRef key, ArrayRef< std::string > defaultValue) const override
Returns the string array the key maps to.
nt::NetworkTable::GetNumber
double GetNumber(StringRef key, double defaultValue) const override
Gets the number associated with the given name.
nt::NetworkTableEntry
NetworkTables Entry.
Definition: NetworkTableEntry.h:39
nt::StopServer
void StopServer()
Stops the server if it is running.
nt::NetworkTable::PutBooleanArray
bool PutBooleanArray(StringRef key, ArrayRef< int > value) override
Put a boolean array in the table.
nt::DeleteAllEntries
void DeleteAllEntries()
Delete All Entries.
nt::NetworkTable::SetPersistent
void SetPersistent(StringRef key) override
Makes a key's value persistent through program restarts.
nt::NetworkTable::GetBoolean
bool GetBoolean(StringRef key, bool defaultValue) const override
Gets the boolean associated with the given name.
nt::StartServer
void StartServer(StringRef persist_filename, const char *listen_address, unsigned int port)
Starts a server using the specified filename, listening address, and port.
nt::NetworkTable::PutStringArray
bool PutStringArray(StringRef key, ArrayRef< std::string > value) override
Put a string array in the table.
nt::NetworkTable::ClearFlags
void ClearFlags(StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
nt::NetworkTable::SaveEntries
const char * SaveEntries(const Twine &filename) const
Save table values to a file.
nt::NetworkTable::GetPath
StringRef GetPath() const override
Gets the full path of this table.
nt::NetworkTable::WPI_DEPRECATED
WPI_DEPRECATED("use NetworkTableInstance::StartServer() or " "NetworkTableInstance::StartClient() instead") static void Initialize()
Initializes network tables.
nt::SetServer
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
nt::NetworkTable::SetServerMode
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable
nt::NetworkTable::PutNumber
bool PutNumber(StringRef key, double value) override
Put a number in the table.
nt::NetworkTable::GetValue
std::shared_ptr< Value > GetValue(const Twine &key) const override
Gets the value associated with a key as an object.
nt::NetworkTable::LoadPersistent
static const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Loads persistent keys from a file.
nt::TableEntryListener
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
nt::NetworkTable::GetKeys
std::vector< std::string > GetKeys(int types=0) const override
Gets all keys in the table (not including sub-tables).
nt::NetworkTable::GetRaw
std::string GetRaw(StringRef key, StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
nt::GetEntry
NT_Entry GetEntry(NT_Inst inst, const Twine &name)
Get Entry Handle.
nt::StartClient
void StartClient()
Starts a client.
nt::StartDSClient
void StartDSClient(unsigned int port)
Starts requesting server address from Driver Station.
nt::NetworkTable::PutString
bool PutString(StringRef key, StringRef value) override
Put a string in the table.
nt::NetworkTable::NormalizeKey
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...
nt::NetworkTable::Flush
static void Flush()
Flushes all updated values immediately to the network.
wpi::StringMap< NT_Entry >
nt::StopDSClient
void StopDSClient()
Stops requesting server address from Driver Station.
nt::StopClient
void StopClient()
Stops the client if it is running.
nt::NetworkTable::SetDefaultBooleanArray
bool SetDefaultBooleanArray(StringRef key, ArrayRef< int > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::SavePersistent
static const char * SavePersistent(StringRef filename)
Saves persistent keys to a file.
nt::NetworkTable::Delete
void Delete(const Twine &key) override
Deletes the specified key in this table.
nt::Value
A network table entry value.
Definition: NetworkTableValue.h:37
nt::NetworkTable::NetworkTable
NetworkTable(NT_Inst inst, const Twine &path, const private_init &)
Constructor.
nt::NetworkTable::RemoveTableListener
void RemoveTableListener(NT_EntryListener listener)
Remove a sub-table listener.
nt::NetworkTable::SetDefaultRaw
bool SetDefaultRaw(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::SetClientMode
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable
nt::NetworkTable::GetSubTable
std::shared_ptr< NetworkTable > GetSubTable(const Twine &key) const override
Returns the table at the specified key.
nt::NetworkTable::GetFlags
unsigned int GetFlags(StringRef key) const override
Returns the flags for the specified key.
nt::NetworkTable::AddSubTableListener
NT_EntryListener AddSubTableListener(TableListener listener, bool localNotify=false)
Listen for sub-table creation.
nt::NetworkTable::GetSubTables
std::vector< std::string > GetSubTables() const override
Gets the names of all subtables in the table.
nt::NetworkTable::GetString
std::string GetString(StringRef key, StringRef defaultValue) const override
Gets the string associated with the given name.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
nt
NetworkTables (ntcore) namespace.
Definition: ITable.h:21
nt::NetworkTable::SetNetworkIdentity
static void SetNetworkIdentity(StringRef name)
Sets the network identity.
nt::NetworkTable::SetDefaultNumberArray
bool SetDefaultNumberArray(StringRef key, ArrayRef< double > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::SetDefaultStringArray
bool SetDefaultStringArray(StringRef key, ArrayRef< std::string > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::SetUpdateRate
static void SetUpdateRate(double interval)
Set the periodic update rate.
nt::NetworkTable::SetDefaultString
bool SetDefaultString(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
nt::NetworkTable::SetFlags
void SetFlags(StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
nt::NetworkTable::GetNumberArray
std::vector< double > GetNumberArray(StringRef key, ArrayRef< double > defaultValue) const override
Returns the number array the key maps to.