8 #ifndef NTCORE_NETWORKTABLES_NETWORKTABLE_H_
9 #define NTCORE_NETWORKTABLES_NETWORKTABLE_H_
17 #include <wpi/ArrayRef.h>
18 #include <wpi/StringMap.h>
19 #include <wpi/Twine.h>
20 #include <wpi/mutex.h>
22 #include "networktables/NetworkTableEntry.h"
23 #include "networktables/TableEntryListener.h"
24 #include "networktables/TableListener.h"
26 #include "tables/ITable.h"
34 class NetworkTableInstance;
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
41 #pragma warning(disable : 4996)
58 mutable wpi::mutex m_mutex;
60 typedef std::pair<ITableListener*, NT_EntryListener> Listener;
61 std::vector<Listener> m_listeners;
62 std::vector<NT_EntryListener> m_lambdaListeners;
64 static std::vector<std::string> s_ip_addresses;
65 static std::string s_persistent_filename;
67 static bool s_enable_ds;
68 static bool s_running;
69 static unsigned int s_port;
71 struct private_init {};
101 bool withLeadingSlash =
true);
105 bool withLeadingSlash =
true);
140 "use NetworkTableInstance::StartServer() or "
141 "NetworkTableInstance::StartClient() instead")
142 static
void Initialize();
150 static
void Shutdown();
176 static
void SetTeam(
int team);
185 static
void SetIPAddress(
StringRef address);
194 static
void SetIPAddress(
ArrayRef<std::
string> addresses);
207 static
void SetPort(
unsigned int port);
217 static
void SetDSClientEnabled(
bool enabled);
228 static
void SetPersistentFilename(
StringRef filename);
282 std::function<
void(
size_t line, const
char* msg)> warn);
314 unsigned int flags) const;
326 unsigned int flags) const;
345 bool localNotify = false);
357 void AddTableListener(ITableListener* listener) override;
361 "NT_NOTIFY_UPDATE | NT_NOTIFY_IMMEDIATE")
362 void AddTableListener(ITableListener* listener,
363 bool immediateNotify) override;
366 void AddTableListenerEx(ITableListener* listener,
367 unsigned int flags) override;
370 void AddTableListener(
StringRef key, ITableListener* listener,
371 bool immediateNotify) override;
374 void AddTableListenerEx(
StringRef key, ITableListener* listener,
375 unsigned int flags) override;
419 std::vector<std::
string>
GetKeys(
int types = 0) const override;
591 ArrayRef<
int> defaultValue) override;
610 ArrayRef<
int> defaultValue) const override;
629 ArrayRef<
double> defaultValue) override;
663 ArrayRef<std::
string> defaultValue) override;
729 std::shared_ptr<
Value> defaultValue) override;
765 const
Twine& filename,
766 std::function<
void(
size_t line, const
char* msg)> warn);
770 #pragma GCC diagnostic pop
778 #ifndef NAMESPACED_NT
782 #endif // NTCORE_NETWORKTABLES_NETWORKTABLE_H_
A network table that knows its subtable path.
Definition: NetworkTable.h:54
void StartClientTeam(NT_Inst inst, unsigned int team, unsigned int port)
Starts a client using commonly known robot addresses for the specified team.
NetworkTables Instance.
Definition: NetworkTableInstance.h:53
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
static const char PATH_SEPARATOR_CHAR
The path separator for sub-tables and keys.
Definition: NetworkTable.h:134
NT_EntryListener AddEntryListener(TableEntryListener listener, unsigned int flags) const
Listen to keys only within this 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.
bool PutValue(const Twine &key, std::shared_ptr< Value > value) override
Put a value in the table.
void RemoveEntryListener(NT_EntryListener listener) const
Remove an entry listener.
bool PutNumberArray(StringRef key, ArrayRef< double > value) override
Put a number array in the 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.
NetworkTableInstance GetInstance() const
Gets the instance for the table.
static void GlobalDeleteAll()
Deletes ALL keys in ALL subtables.
static std::vector< std::string > GetHierarchy(const Twine &key)
Gets a list of the names of all the super tables of a given key.
std::vector< int > GetBooleanArray(StringRef key, ArrayRef< int > defaultValue) const override
Returns the boolean array the key maps to.
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.
bool SetDefaultBoolean(StringRef key, bool defaultValue) override
Gets the current value in the table, setting it if it does not exist.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
bool ContainsSubTable(const Twine &key) const override
Determines whether there exists a non-empty subtable for this key in this table.
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
static StringRef BasenameKey(StringRef key)
Gets the "base name" of a key.
void ClearPersistent(StringRef key) override
Stop making a key's value persistent through program restarts.
bool IsPersistent(StringRef key) const override
Returns whether the value is 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.
NetworkTables Entry.
Definition: NetworkTableEntry.h:39
void StopServer()
Stops the server if it is running.
bool PutBooleanArray(StringRef key, ArrayRef< int > value) override
Put a boolean array in the table.
void DeleteAllEntries()
Delete All Entries.
void SetPersistent(StringRef key) override
Makes a key's value persistent through program restarts.
bool GetBoolean(StringRef key, bool defaultValue) const override
Gets the boolean associated with the given name.
void StartServer(StringRef persist_filename, const char *listen_address, unsigned int port)
Starts a server using the specified filename, listening address, and port.
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.
const char * SaveEntries(const Twine &filename) const
Save table values to a file.
StringRef GetPath() const override
Gets the full path of this table.
WPI_DEPRECATED("use NetworkTableInstance::StartServer() or " "NetworkTableInstance::StartClient() instead") static void Initialize()
Initializes network tables.
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
static void SetServerMode()
set that network tables should be a server This must be called before initialize or GetTable
bool PutNumber(StringRef key, double value) override
Put a number in the table.
std::shared_ptr< Value > GetValue(const Twine &key) const override
Gets the value associated with a key as an object.
static const char * LoadPersistent(StringRef filename, std::function< void(size_t line, const char *msg)> warn)
Loads persistent keys from a file.
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
std::vector< std::string > GetKeys(int types=0) const override
Gets all keys in the table (not including sub-tables).
std::string GetRaw(StringRef key, StringRef defaultValue) const override
Returns the raw value (byte array) the key maps to.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
NT_Entry GetEntry(NT_Inst inst, const Twine &name)
Get Entry Handle.
void StartClient()
Starts a client.
void StartDSClient(unsigned int port)
Starts requesting server address from Driver Station.
bool PutString(StringRef key, StringRef value) override
Put a string in the 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...
static void Flush()
Flushes all updated values immediately to the network.
void StopDSClient()
Stops requesting server address from Driver Station.
void StopClient()
Stops the client if it is running.
bool SetDefaultBooleanArray(StringRef key, ArrayRef< int > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
static const char * SavePersistent(StringRef filename)
Saves persistent keys to a file.
void Delete(const Twine &key) override
Deletes the specified key in this table.
A network table entry value.
Definition: NetworkTableValue.h:37
NetworkTable(NT_Inst inst, const Twine &path, const private_init &)
Constructor.
void RemoveTableListener(NT_EntryListener listener)
Remove a sub-table listener.
bool SetDefaultRaw(StringRef key, StringRef defaultValue) override
Gets the current value in the table, setting it if it does not exist.
static void SetClientMode()
set that network tables should be a client This must be called before initialize or GetTable
std::shared_ptr< NetworkTable > GetSubTable(const Twine &key) const override
Returns the table at the specified key.
unsigned int GetFlags(StringRef key) const override
Returns the flags for the specified key.
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.
std::string GetString(StringRef key, StringRef defaultValue) const override
Gets the string associated with the given name.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
NetworkTables (ntcore) namespace.
Definition: NetworkTableValue.h:27
static void SetNetworkIdentity(StringRef name)
Sets the network identity.
bool SetDefaultNumberArray(StringRef key, ArrayRef< double > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
bool SetDefaultStringArray(StringRef key, ArrayRef< std::string > defaultValue) override
Gets the current value in the table, setting it if it does not exist.
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.
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.