WPILibC++  unspecified
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 
44 class NetworkTable final : public ITable {
45  private:
46  NT_Inst m_inst;
47  std::string m_path;
48  mutable wpi::mutex m_mutex;
49  mutable wpi::StringMap<NT_Entry> m_entries;
50  typedef std::pair<ITableListener*, NT_EntryListener> Listener;
51  std::vector<Listener> m_listeners;
52 
53  static std::vector<std::string> s_ip_addresses;
54  static std::string s_persistent_filename;
55  static bool s_client;
56  static bool s_enable_ds;
57  static bool s_running;
58  static unsigned int s_port;
59 
60  struct private_init {};
61  friend class NetworkTableInstance;
62 
63  public:
70  static StringRef BasenameKey(StringRef key);
71 
88  static std::string NormalizeKey(const Twine& key,
89  bool withLeadingSlash = true);
90 
91  static StringRef NormalizeKey(const Twine& key,
93  bool withLeadingSlash = true);
94 
102  static std::vector<std::string> GetHierarchy(const Twine& key);
103 
108  NetworkTable(NT_Inst inst, const Twine& path, const private_init&);
109  virtual ~NetworkTable();
110 
116 
120  static const char PATH_SEPARATOR_CHAR;
121 
126  "use NetworkTableInstance::StartServer() or "
127  "NetworkTableInstance::StartClient() instead")
128  static void Initialize();
129 
134  "use NetworkTableInstance::StopServer() or "
135  "NetworkTableInstance::StopClient() instead")
136  static void Shutdown();
137 
143  static void SetClientMode();
144 
150  static void SetServerMode();
151 
161  static void SetTeam(int team);
162 
168  "use NetworkTableInstance::SetServer() or "
169  "NetworkTableInstance::StartClient() instead")
170  static void SetIPAddress(StringRef address);
171 
177  "use NetworkTableInstance::SetServer() or "
178  "NetworkTableInstance::StartClient() instead")
179  static void SetIPAddress(ArrayRef<std::string> addresses);
180 
187  "use the appropriate parameters to NetworkTableInstance::SetServer(), "
190  "NetworkTableInstance::StartDSClient() instead")
191  static void SetPort(unsigned int port);
192 
198  "use NetworkTableInstance::StartDSClient() and "
199  "NetworkTableInstance::StopDSClient() instead")
200  static void SetDSClientEnabled(bool enabled);
201 
208  "use the appropriate parameter to NetworkTableInstance::StartServer() "
209  "instead")
210  static void SetPersistentFilename(StringRef filename);
211 
218  static void SetNetworkIdentity(StringRef name);
219 
224  static void GlobalDeleteAll();
225 
232  WPI_DEPRECATED("use NetworkTableInstance::Flush() instead")
233  static void Flush();
234 
242  static void SetUpdateRate(double interval);
243 
251  static const char* SavePersistent(StringRef filename);
252 
261  static const char* LoadPersistent(
262  StringRef filename,
263  std::function<void(size_t line, const char* msg)> warn);
264 
275  "use NetworkTableInstance::GetTable() or "
276  "NetworkTableInstance::GetEntry() instead")
277  static std::shared_ptr<NetworkTable> GetTable(StringRef key);
278 
284  NetworkTableEntry GetEntry(const Twine& key) const;
285 
292  NT_EntryListener AddEntryListener(TableEntryListener listener,
293  unsigned int flags) const;
294 
302  NT_EntryListener AddEntryListener(const Twine& key,
303  TableEntryListener listener,
304  unsigned int flags) const;
305 
310  void RemoveEntryListener(NT_EntryListener listener) const;
311 
320  NT_EntryListener AddSubTableListener(TableListener listener,
321  bool localNotify = false) const;
322 
327  void RemoveTableListener(NT_EntryListener listener) const;
328 
330  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
331  "NT_NOTIFY_UPDATE")
332  void AddTableListener(ITableListener* listener) override;
333 
335  "use AddEntryListener() instead with flags value of NT_NOTIFY_NEW | "
336  "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
337  void AddTableListener(ITableListener* listener,
338  bool immediateNotify) override;
339 
340  WPI_DEPRECATED("use AddEntryListener() instead")
341  void AddTableListenerEx(ITableListener* listener,
342  unsigned int flags) override;
343 
344  WPI_DEPRECATED("use AddEntryListener() instead")
345  void AddTableListener(StringRef key, ITableListener* listener,
346  bool immediateNotify) override;
347 
348  WPI_DEPRECATED("use AddEntryListener() instead")
349  void AddTableListenerEx(StringRef key, ITableListener* listener,
350  unsigned int flags) override;
351 
352  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
353  void AddSubTableListener(ITableListener* listener) override;
354 
355  WPI_DEPRECATED("use AddSubTableListener(TableListener, bool) instead")
356  void AddSubTableListener(ITableListener* listener, bool localNotify) override;
357 
358  WPI_DEPRECATED("use RemoveTableListener(NT_EntryListener) instead")
359  void RemoveTableListener(ITableListener* listener) override;
360 
368  std::shared_ptr<NetworkTable> GetSubTable(const Twine& key) const override;
369 
376  bool ContainsKey(const Twine& key) const override;
377 
386  bool ContainsSubTable(const Twine& key) const override;
387 
393  std::vector<std::string> GetKeys(int types = 0) const override;
394 
399  std::vector<std::string> GetSubTables() const override;
400 
406  void SetPersistent(StringRef key) override;
407 
414  void ClearPersistent(StringRef key) override;
415 
422  bool IsPersistent(StringRef key) const override;
423 
431  void SetFlags(StringRef key, unsigned int flags) override;
432 
440  void ClearFlags(StringRef key, unsigned int flags) override;
441 
448  unsigned int GetFlags(StringRef key) const override;
449 
455  void Delete(const Twine& key) override;
456 
464  bool PutNumber(StringRef key, double value) override;
465 
472  bool SetDefaultNumber(StringRef key, double defaultValue) override;
473 
482  double GetNumber(StringRef key, double defaultValue) const override;
483 
491  bool PutString(StringRef key, StringRef value) override;
492 
499  bool SetDefaultString(StringRef key, StringRef defaultValue) override;
500 
510  std::string GetString(StringRef key, StringRef defaultValue) const override;
511 
519  bool PutBoolean(StringRef key, bool value) override;
520 
527  bool SetDefaultBoolean(StringRef key, bool defaultValue) override;
528 
538  bool GetBoolean(StringRef key, bool defaultValue) const override;
539 
550  bool PutBooleanArray(StringRef key, ArrayRef<int> value) override;
551 
559  ArrayRef<int> defaultValue) override;
560 
576  std::vector<int> GetBooleanArray(StringRef key,
577  ArrayRef<int> defaultValue) const override;
578 
585  bool PutNumberArray(StringRef key, ArrayRef<double> value) override;
586 
594  ArrayRef<double> defaultValue) override;
595 
607  std::vector<double> GetNumberArray(
608  StringRef key, ArrayRef<double> defaultValue) const override;
609 
616  bool PutStringArray(StringRef key, ArrayRef<std::string> value) override;
617 
625  ArrayRef<std::string> defaultValue) override;
626 
638  std::vector<std::string> GetStringArray(
639  StringRef key, ArrayRef<std::string> defaultValue) const override;
640 
647  bool PutRaw(StringRef key, StringRef value) override;
648 
655  bool SetDefaultRaw(StringRef key, StringRef defaultValue) override;
656 
668  std::string GetRaw(StringRef key, StringRef defaultValue) const override;
669 
677  bool PutValue(const Twine& key, std::shared_ptr<Value> value) override;
678 
685  bool SetDefaultValue(const Twine& key,
686  std::shared_ptr<Value> defaultValue) override;
687 
695  std::shared_ptr<Value> GetValue(const Twine& key) const override;
696 
701  StringRef GetPath() const override;
702 
709  const char* SaveEntries(const Twine& filename) const;
710 
718  const char* LoadEntries(
719  const Twine& filename,
720  std::function<void(size_t line, const char* msg)> warn);
721 };
722 
723 #ifdef __GNUC__
724 #pragma GCC diagnostic pop
725 #endif
726 
727 } // namespace nt
728 
729 // For backwards compatability
730 #ifndef NAMESPACED_NT
731 using nt::NetworkTable; // NOLINT
732 #endif
733 
734 #endif // NTCORE_NETWORKTABLES_NETWORKTABLE_H_
void RemoveEntryListener(NT_EntryListener listener) const
Remove an entry listener.
Definition: NetworkTable.cpp:242
bool PutNumberArray(StringRef key, ArrayRef< double > value) override
Put a number array in the table.
Definition: NetworkTable.cpp:463
std::vector< double > GetNumberArray(StringRef key, ArrayRef< double > defaultValue) const override
Returns the number array the key maps to.
Definition: NetworkTable.cpp:472
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
Definition: ntcore_cpp.cpp:823
bool SetDefaultString(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:428
bool ContainsKey(const Twine &key) const override
Determines whether the given key is in this table.
Definition: NetworkTable.cpp:344
std::shared_ptr< Value > GetValue(const Twine &key) const override
Gets the value associated with a key as an object.
Definition: NetworkTable.cpp:512
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:114
static void SetNetworkIdentity(StringRef name)
Sets the network identity.
Definition: NetworkTable.cpp:163
void SetPersistent(StringRef key) override
Makes a key&#39;s value persistent through program restarts.
Definition: NetworkTable.cpp:386
double GetNumber(StringRef key, double defaultValue) const override
Gets the number associated with the given name.
Definition: NetworkTable.cpp:420
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:491
Definition: SmallVector.h:946
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
static void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: NetworkTable.cpp:173
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:467
bool PutString(StringRef key, StringRef value) override
Put a string in the table.
Definition: NetworkTable.cpp:424
static void Flush()
Flushes all updated values immediately to the network.
Definition: NetworkTable.cpp:171
const char * LoadEntries(const Twine &filename, std::function< void(size_t line, const char *msg)> warn)
Load table values from a file.
Definition: NetworkTable.cpp:522
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:120
std::string GetString(StringRef key, StringRef defaultValue) const override
Gets the string associated with the given name.
Definition: NetworkTable.cpp:432
NetworkTableInstance GetInstance() const
Gets the instance for the table.
Definition: NetworkTable.cpp:199
NetworkTables Instance.
Definition: NetworkTableInstance.h:50
NT_Entry GetEntry(NT_Inst inst, const Twine &name)
Get Entry Handle.
Definition: ntcore_cpp.cpp:56
bool SetDefaultBoolean(StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:441
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:167
void ClearPersistent(StringRef key) override
Stop making a key&#39;s value persistent through program restarts.
Definition: NetworkTable.cpp:390
std::vector< int > GetBooleanArray(StringRef key, ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
Definition: NetworkTable.cpp:458
const char * SaveEntries(const Twine &filename) const
Save table values to a file.
Definition: NetworkTable.cpp:518
void StopServer()
Stops the server if it is running.
Definition: ntcore_cpp.cpp:759
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable ...
Definition: NetworkTable.cpp:112
void StartDSClient(unsigned int port)
Starts requesting server address from Driver Station.
Definition: ntcore_cpp.cpp:853
void StopDSClient()
Stops requesting server address from Driver Station.
Definition: ntcore_cpp.cpp:864
std::vector< std::string > GetStringArray(StringRef key, ArrayRef< std::string > defaultValue) const override
Returns the string array the key maps to.
Definition: NetworkTable.cpp:486
NetworkTable(NT_Inst inst, const Twine &path, const private_init &)
Constructor.
Definition: NetworkTable.cpp:192
Definition: IStorage.h:21
NT_EntryListener AddEntryListener(TableEntryListener listener, unsigned int flags) const
Listen to keys only within this table.
Definition: NetworkTable.cpp:214
std::shared_ptr< NetworkTable > GetSubTable(const Twine &key) const override
Returns the table at the specified key.
Definition: NetworkTable.cpp:338
unsigned int GetFlags(StringRef key) const override
Returns the flags for the specified key.
Definition: NetworkTable.cpp:406
bool PutStringArray(StringRef key, ArrayRef< std::string > value) override
Put a string array in the table.
Definition: NetworkTable.cpp:477
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.
Definition: NetworkTable.cpp:507
StringRef GetPath() const override
Gets the full path of this table.
Definition: NetworkTable.cpp:516
std::vector< std::string > GetSubTables() const override
Gets the names of all subtables in the table.
Definition: NetworkTable.cpp:373
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:846
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:745
bool SetDefaultRaw(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:495
static const char * SavePersistent(StringRef filename)
Saves persistent keys to a file.
Definition: NetworkTable.cpp:177
WPI_DEPRECATED("use NetworkTableInstance::StartServer() or ""NetworkTableInstance::StartClient() instead") static void Initialize()
Initializes network tables.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static StringRef BasenameKey(StringRef key)
Gets the "base name" of a key.
Definition: NetworkTable.cpp:33
void DeleteAllEntries()
Delete All Entries.
Definition: ntcore_cpp.cpp:196
bool PutBooleanArray(StringRef key, ArrayRef< int > value) override
Put a boolean array in the table.
Definition: NetworkTable.cpp:449
bool GetBoolean(StringRef key, bool defaultValue) const override
Gets the boolean associated with the given name.
Definition: NetworkTable.cpp:445
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...
Definition: NetworkTable.cpp:39
bool IsPersistent(StringRef key) const override
Returns whether the value is persistent through program restarts.
Definition: NetworkTable.cpp:394
std::string GetRaw(StringRef key, StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
Definition: NetworkTable.cpp:499
bool SetDefaultNumber(StringRef key, double defaultValue) override
Gets the current value in the table, setting it if it does not exist.
Definition: NetworkTable.cpp:416
bool ContainsSubTable(const Twine &key) const override
Determines whether there exists a non-empty subtable for this key in this table.
Definition: NetworkTable.cpp:351
NetworkTables Entry.
Definition: NetworkTableEntry.h:35
std::vector< std::string > GetKeys(int types=0) const override
Gets all keys in the table (not including sub-tables).
Definition: NetworkTable.cpp:359
void ClearFlags(StringRef key, unsigned int flags) override
Clears flags on the specified key in this table.
Definition: NetworkTable.cpp:402
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:181
bool PutNumber(StringRef key, double value) override
Put a number in the table.
Definition: NetworkTable.cpp:412
static std::vector< std::string > GetHierarchy(const Twine &key)
Gets a list of the names of all the super tables of a given key.
Definition: NetworkTable.cpp:64
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:453
void SetFlags(StringRef key, unsigned int flags) override
Sets flags on the specified key in this table.
Definition: NetworkTable.cpp:398
A network table entry value.
Definition: NetworkTableValue.h:35
A network table that knows its subtable path.
Definition: NetworkTable.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void Delete(const Twine &key) override
Deletes the specified key in this table.
Definition: NetworkTable.cpp:410
bool PutBoolean(StringRef key, bool value) override
Put a boolean in the table.
Definition: NetworkTable.cpp:437
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:481
void StopClient()
Stops the client if it is running.
Definition: ntcore_cpp.cpp:814
void StartClient()
Starts a client.
Definition: ntcore_cpp.cpp:768
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:806
bool PutValue(const Twine &key, std::shared_ptr< Value > value) override
Put a value in the table.
Definition: NetworkTable.cpp:503