mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Remove wpiutil and update to the new build system (#210)
This commit is contained in:
committed by
Peter Johnson
parent
f43675e2bd
commit
5df7463663
572
src/main/native/include/networktables/NetworkTable.h
Normal file
572
src/main/native/include/networktables/NetworkTable.h
Normal file
@@ -0,0 +1,572 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NETWORKTABLE_H_
|
||||
#define NETWORKTABLE_H_
|
||||
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include "tables/ITable.h"
|
||||
|
||||
/**
|
||||
* A network table that knows its subtable path.
|
||||
*/
|
||||
class NetworkTable : public ITable {
|
||||
private:
|
||||
struct private_init {};
|
||||
|
||||
std::string m_path;
|
||||
std::mutex m_mutex;
|
||||
typedef std::pair<ITableListener*, unsigned int> Listener;
|
||||
std::vector<Listener> m_listeners;
|
||||
|
||||
static std::vector<std::string> s_ip_addresses;
|
||||
static std::string s_persistent_filename;
|
||||
static bool s_client;
|
||||
static bool s_enable_ds;
|
||||
static bool s_running;
|
||||
static unsigned int s_port;
|
||||
|
||||
public:
|
||||
NetworkTable(llvm::StringRef path, const private_init&);
|
||||
virtual ~NetworkTable();
|
||||
|
||||
/**
|
||||
* The path separator for sub-tables and keys
|
||||
*
|
||||
*/
|
||||
static const char PATH_SEPARATOR_CHAR;
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
*/
|
||||
static void Initialize();
|
||||
static void Shutdown();
|
||||
|
||||
/**
|
||||
* set that network tables should be a client
|
||||
* This must be called before initialize or GetTable
|
||||
*/
|
||||
static void SetClientMode();
|
||||
|
||||
/**
|
||||
* set that network tables should be a server
|
||||
* This must be called before initialize or GetTable
|
||||
*/
|
||||
static void SetServerMode();
|
||||
|
||||
/**
|
||||
* set the team the robot is configured for (this will set the mdns address
|
||||
* that network tables will connect to in client mode)
|
||||
* This must be called before initialize or GetTable
|
||||
* @param team the team number
|
||||
*/
|
||||
static void SetTeam(int team);
|
||||
|
||||
/**
|
||||
* @param address the adress that network tables will connect to in client
|
||||
* mode
|
||||
*/
|
||||
static void SetIPAddress(llvm::StringRef address);
|
||||
|
||||
/**
|
||||
* @param addresses the addresses that network tables will connect to in
|
||||
* client mode (in round robin order)
|
||||
*/
|
||||
static void SetIPAddress(llvm::ArrayRef<std::string> addresses);
|
||||
|
||||
/**
|
||||
* @param port the port number that network tables will connect to in client
|
||||
* mode or listen to in server mode
|
||||
*/
|
||||
static void SetPort(unsigned int port);
|
||||
|
||||
/**
|
||||
* @param enabled whether to enable the connection to the local DS to get
|
||||
* the robot IP address (defaults to enabled)
|
||||
*/
|
||||
static void SetDSClientEnabled(bool enabled);
|
||||
|
||||
/**
|
||||
* Sets the persistent filename.
|
||||
* @param filename the filename that the network tables server uses for
|
||||
* automatic loading and saving of persistent values
|
||||
*/
|
||||
static void SetPersistentFilename(llvm::StringRef filename);
|
||||
|
||||
/**
|
||||
* Sets the network identity.
|
||||
* This is provided in the connection info on the remote end.
|
||||
* @param name identity
|
||||
*/
|
||||
static void SetNetworkIdentity(llvm::StringRef name);
|
||||
|
||||
/**
|
||||
* Deletes ALL keys in ALL subtables. Use with caution!
|
||||
*/
|
||||
static void GlobalDeleteAll();
|
||||
|
||||
/**
|
||||
* Flushes all updated values immediately to the network.
|
||||
* Note: This is rate-limited to protect the network from flooding.
|
||||
* This is primarily useful for synchronizing network updates with
|
||||
* user code.
|
||||
*/
|
||||
static void Flush();
|
||||
|
||||
/**
|
||||
* Set the periodic update rate.
|
||||
*
|
||||
* @param interval update interval in seconds (range 0.01 to 1.0)
|
||||
*/
|
||||
static void SetUpdateRate(double interval);
|
||||
|
||||
/**
|
||||
* Saves persistent keys to a file. The server does this automatically.
|
||||
*
|
||||
* @param filename file name
|
||||
* @return Error (or nullptr).
|
||||
*/
|
||||
static const char* SavePersistent(llvm::StringRef filename);
|
||||
|
||||
/**
|
||||
* Loads persistent keys from a file. The server does this automatically.
|
||||
*
|
||||
* @param filename file name
|
||||
* @param warn callback function called for warnings
|
||||
* @return Error (or nullptr).
|
||||
*/
|
||||
static const char* LoadPersistent(
|
||||
llvm::StringRef filename,
|
||||
std::function<void(size_t line, const char* msg)> warn);
|
||||
|
||||
/**
|
||||
* Gets the table with the specified key. If the table does not exist, a new
|
||||
* table will be created.<br>
|
||||
* This will automatically initialize network tables if it has not been
|
||||
* already.
|
||||
*
|
||||
* @param key
|
||||
* the key name
|
||||
* @return the network table requested
|
||||
*/
|
||||
static std::shared_ptr<NetworkTable> GetTable(llvm::StringRef key);
|
||||
|
||||
void AddTableListener(ITableListener* listener) override;
|
||||
void AddTableListener(ITableListener* listener,
|
||||
bool immediateNotify) override;
|
||||
void AddTableListenerEx(ITableListener* listener,
|
||||
unsigned int flags) override;
|
||||
void AddTableListener(llvm::StringRef key, ITableListener* listener,
|
||||
bool immediateNotify) override;
|
||||
void AddTableListenerEx(llvm::StringRef key, ITableListener* listener,
|
||||
unsigned int flags) override;
|
||||
void AddSubTableListener(ITableListener* listener) override;
|
||||
void AddSubTableListener(ITableListener* listener, bool localNotify) override;
|
||||
void RemoveTableListener(ITableListener* listener) override;
|
||||
|
||||
/**
|
||||
* Returns the table at the specified key. If there is no table at the
|
||||
* specified key, it will create a new table
|
||||
*
|
||||
* @param key
|
||||
* the key name
|
||||
* @return the networktable to be returned
|
||||
*/
|
||||
std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Determines whether the given key is in this table.
|
||||
*
|
||||
* @param key the key to search for
|
||||
* @return true if the table as a value assigned to the given key
|
||||
*/
|
||||
bool ContainsKey(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Determines whether there exists a non-empty subtable for this key
|
||||
* in this table.
|
||||
*
|
||||
* @param key the key to search for
|
||||
* @return true if there is a subtable with the key which contains at least
|
||||
* one key/subtable of its own
|
||||
*/
|
||||
bool ContainsSubTable(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* @param types bitmask of types; 0 is treated as a "don't care".
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
std::vector<std::string> GetKeys(int types = 0) const override;
|
||||
|
||||
/**
|
||||
* @return subtables currently in the table
|
||||
*/
|
||||
std::vector<std::string> GetSubTables() const override;
|
||||
|
||||
/**
|
||||
* Makes a key's value persistent through program restarts.
|
||||
*
|
||||
* @param key the key to make persistent
|
||||
*/
|
||||
void SetPersistent(llvm::StringRef key) override;
|
||||
|
||||
/**
|
||||
* Stop making a key's value persistent through program restarts.
|
||||
* The key cannot be null.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
void ClearPersistent(llvm::StringRef key) override;
|
||||
|
||||
/**
|
||||
* Returns whether the value is persistent through program restarts.
|
||||
* The key cannot be null.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
bool IsPersistent(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Sets flags on the specified key in this table. The key can
|
||||
* not be null.
|
||||
*
|
||||
* @param key the key name
|
||||
* @param flags the flags to set (bitmask)
|
||||
*/
|
||||
void SetFlags(llvm::StringRef key, unsigned int flags) override;
|
||||
|
||||
/**
|
||||
* Clears flags on the specified key in this table. The key can
|
||||
* not be null.
|
||||
*
|
||||
* @param key the key name
|
||||
* @param flags the flags to clear (bitmask)
|
||||
*/
|
||||
void ClearFlags(llvm::StringRef key, unsigned int flags) override;
|
||||
|
||||
/**
|
||||
* Returns the flags for the specified key.
|
||||
*
|
||||
* @param key the key name
|
||||
* @return the flags, or 0 if the key is not defined
|
||||
*/
|
||||
unsigned int GetFlags(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Deletes the specified key in this table.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
void Delete(llvm::StringRef key) override;
|
||||
|
||||
/**
|
||||
* Put a number in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
bool PutNumber(llvm::StringRef key, double value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultNumber(llvm::StringRef key,
|
||||
double defaultValue) override;
|
||||
|
||||
/**
|
||||
* Gets the number associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetNumber(StringRef key, double defaultValue) instead")
|
||||
virtual double GetNumber(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Gets the number associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*/
|
||||
virtual double GetNumber(llvm::StringRef key,
|
||||
double defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a string in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultString(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) override;
|
||||
|
||||
/**
|
||||
* Gets the string associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetString(StringRef key, StringRef defaultValue) instead")
|
||||
virtual std::string GetString(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Gets the string associated with the given name. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*/
|
||||
virtual std::string GetString(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a boolean in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutBoolean(llvm::StringRef key, bool value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultBoolean(llvm::StringRef key,
|
||||
bool defaultValue) override;
|
||||
|
||||
/**
|
||||
* Gets the boolean associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetBoolean(StringRef key, bool defaultValue) instead")
|
||||
virtual bool GetBoolean(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Gets the boolean associated with the given name. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*/
|
||||
virtual bool GetBoolean(llvm::StringRef key,
|
||||
bool defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a boolean array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*
|
||||
* @note The array must be of int's rather than of bool's because
|
||||
* std::vector<bool> is special-cased in C++. 0 is false, any
|
||||
* non-zero value is true.
|
||||
*/
|
||||
virtual bool PutBooleanArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<int> value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultBooleanArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<int> defaultValue) override;
|
||||
|
||||
/**
|
||||
* Returns the boolean array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*
|
||||
* @note The returned array is std::vector<int> instead of std::vector<bool>
|
||||
* because std::vector<bool> is special-cased in C++. 0 is false, any
|
||||
* non-zero value is true.
|
||||
*/
|
||||
virtual std::vector<int> GetBooleanArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<int> defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a number array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutNumberArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<double> value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultNumberArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<double> defaultValue) override;
|
||||
|
||||
/**
|
||||
* Returns the number array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::vector<double> GetNumberArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<double> defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a string array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutStringArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<std::string> value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultStringArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<std::string> defaultValue) override;
|
||||
|
||||
/**
|
||||
* Returns the string array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::vector<std::string> GetStringArray(
|
||||
llvm::StringRef key,
|
||||
llvm::ArrayRef<std::string> defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a raw value (byte array) in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultRaw(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) override;
|
||||
|
||||
/**
|
||||
* Returns the raw value (byte array) the key maps to. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the raw contents. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::string GetRaw(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) const override;
|
||||
|
||||
/**
|
||||
* Put a value in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
bool PutValue(llvm::StringRef key, std::shared_ptr<nt::Value> value) override;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultValue(
|
||||
llvm::StringRef key, std::shared_ptr<nt::Value> defaultValue) override;
|
||||
|
||||
/**
|
||||
* Gets the value associated with a key as an object
|
||||
*
|
||||
* @param key the key of the value to look up
|
||||
* @return the value associated with the given key, or nullptr if the key
|
||||
* does not exist
|
||||
*/
|
||||
std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const override;
|
||||
|
||||
/**
|
||||
* Gets the full path of this table.
|
||||
*/
|
||||
llvm::StringRef GetPath() const override;
|
||||
};
|
||||
|
||||
#endif // NETWORKTABLE_H_
|
||||
183
src/main/native/include/nt_Value.h
Normal file
183
src/main/native/include/nt_Value.h
Normal file
@@ -0,0 +1,183 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NT_VALUE_H_
|
||||
#define NT_VALUE_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "llvm/ArrayRef.h"
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
#include "ntcore_c.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
using llvm::ArrayRef;
|
||||
using llvm::StringRef;
|
||||
|
||||
/** NetworkTables Entry Value */
|
||||
class Value {
|
||||
struct private_init {};
|
||||
|
||||
public:
|
||||
Value();
|
||||
Value(NT_Type type, const private_init&);
|
||||
~Value();
|
||||
|
||||
NT_Type type() const { return m_val.type; }
|
||||
const NT_Value& value() const { return m_val; }
|
||||
unsigned long long last_change() const { return m_val.last_change; }
|
||||
|
||||
/*
|
||||
* Type Checkers
|
||||
*/
|
||||
bool IsBoolean() const { return m_val.type == NT_BOOLEAN; }
|
||||
bool IsDouble() const { return m_val.type == NT_DOUBLE; }
|
||||
bool IsString() const { return m_val.type == NT_STRING; }
|
||||
bool IsRaw() const { return m_val.type == NT_RAW; }
|
||||
bool IsRpc() const { return m_val.type == NT_RPC; }
|
||||
bool IsBooleanArray() const { return m_val.type == NT_BOOLEAN_ARRAY; }
|
||||
bool IsDoubleArray() const { return m_val.type == NT_DOUBLE_ARRAY; }
|
||||
bool IsStringArray() const { return m_val.type == NT_STRING_ARRAY; }
|
||||
|
||||
/*
|
||||
* Type-Safe Getters
|
||||
*/
|
||||
bool GetBoolean() const {
|
||||
assert(m_val.type == NT_BOOLEAN);
|
||||
return m_val.data.v_boolean != 0;
|
||||
}
|
||||
double GetDouble() const {
|
||||
assert(m_val.type == NT_DOUBLE);
|
||||
return m_val.data.v_double;
|
||||
}
|
||||
StringRef GetString() const {
|
||||
assert(m_val.type == NT_STRING);
|
||||
return m_string;
|
||||
}
|
||||
StringRef GetRaw() const {
|
||||
assert(m_val.type == NT_RAW);
|
||||
return m_string;
|
||||
}
|
||||
StringRef GetRpc() const {
|
||||
assert(m_val.type == NT_RPC);
|
||||
return m_string;
|
||||
}
|
||||
ArrayRef<int> GetBooleanArray() const {
|
||||
assert(m_val.type == NT_BOOLEAN_ARRAY);
|
||||
return ArrayRef<int>(m_val.data.arr_boolean.arr,
|
||||
m_val.data.arr_boolean.size);
|
||||
}
|
||||
ArrayRef<double> GetDoubleArray() const {
|
||||
assert(m_val.type == NT_DOUBLE_ARRAY);
|
||||
return ArrayRef<double>(m_val.data.arr_double.arr,
|
||||
m_val.data.arr_double.size);
|
||||
}
|
||||
ArrayRef<std::string> GetStringArray() const {
|
||||
assert(m_val.type == NT_STRING_ARRAY);
|
||||
return m_string_array;
|
||||
}
|
||||
|
||||
static std::shared_ptr<Value> MakeBoolean(bool value) {
|
||||
auto val = std::make_shared<Value>(NT_BOOLEAN, private_init());
|
||||
val->m_val.data.v_boolean = value;
|
||||
return val;
|
||||
}
|
||||
static std::shared_ptr<Value> MakeDouble(double value) {
|
||||
auto val = std::make_shared<Value>(NT_DOUBLE, private_init());
|
||||
val->m_val.data.v_double = value;
|
||||
return val;
|
||||
}
|
||||
static std::shared_ptr<Value> MakeString(StringRef value) {
|
||||
auto val = std::make_shared<Value>(NT_STRING, private_init());
|
||||
val->m_string = value;
|
||||
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_string.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
template <typename T,
|
||||
typename = std::enable_if_t<std::is_same<T, std::string>>>
|
||||
#else
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_same<T, std::string>::value>::type>
|
||||
#endif
|
||||
static std::shared_ptr<Value> MakeString(T&& value) {
|
||||
auto val = std::make_shared<Value>(NT_STRING, private_init());
|
||||
val->m_string = std::move(value);
|
||||
val->m_val.data.v_string.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_string.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
static std::shared_ptr<Value> MakeRaw(StringRef value) {
|
||||
auto val = std::make_shared<Value>(NT_RAW, private_init());
|
||||
val->m_string = value;
|
||||
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_raw.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
template <typename T,
|
||||
typename = std::enable_if_t<std::is_same<T, std::string>>>
|
||||
#else
|
||||
template <typename T,
|
||||
typename std::enable_if<std::is_same<T, std::string>::value>::type>
|
||||
#endif
|
||||
static std::shared_ptr<Value> MakeRaw(T&& value) {
|
||||
auto val = std::make_shared<Value>(NT_RAW, private_init());
|
||||
val->m_string = std::move(value);
|
||||
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_raw.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
static std::shared_ptr<Value> MakeRpc(StringRef value) {
|
||||
auto val = std::make_shared<Value>(NT_RPC, private_init());
|
||||
val->m_string = value;
|
||||
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_raw.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
template <typename T>
|
||||
static std::shared_ptr<Value> MakeRpc(T&& value) {
|
||||
auto val = std::make_shared<Value>(NT_RPC, private_init());
|
||||
val->m_string = std::move(value);
|
||||
val->m_val.data.v_raw.str = const_cast<char*>(val->m_string.c_str());
|
||||
val->m_val.data.v_raw.len = val->m_string.size();
|
||||
return val;
|
||||
}
|
||||
|
||||
static std::shared_ptr<Value> MakeBooleanArray(ArrayRef<int> value);
|
||||
static std::shared_ptr<Value> MakeDoubleArray(ArrayRef<double> value);
|
||||
static std::shared_ptr<Value> MakeStringArray(ArrayRef<std::string> value);
|
||||
|
||||
// Note: This function moves the values out of the vector.
|
||||
static std::shared_ptr<Value> MakeStringArray(
|
||||
std::vector<std::string>&& value);
|
||||
|
||||
Value(const Value&) = delete;
|
||||
Value& operator=(const Value&) = delete;
|
||||
friend bool operator==(const Value& lhs, const Value& rhs);
|
||||
|
||||
private:
|
||||
NT_Value m_val;
|
||||
std::string m_string;
|
||||
std::vector<std::string> m_string_array;
|
||||
};
|
||||
|
||||
bool operator==(const Value& lhs, const Value& rhs);
|
||||
inline bool operator!=(const Value& lhs, const Value& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#endif // NT_VALUE_H_
|
||||
19
src/main/native/include/ntcore.h
Normal file
19
src/main/native/include/ntcore.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NTCORE_H_
|
||||
#define NTCORE_H_
|
||||
|
||||
/* C API */
|
||||
#include "ntcore_c.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
/* C++ API */
|
||||
#include "ntcore_cpp.h"
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* NTCORE_H_ */
|
||||
1076
src/main/native/include/ntcore_c.h
Normal file
1076
src/main/native/include/ntcore_c.h
Normal file
File diff suppressed because it is too large
Load Diff
295
src/main/native/include/ntcore_cpp.h
Normal file
295
src/main/native/include/ntcore_cpp.h
Normal file
@@ -0,0 +1,295 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NTCORE_CPP_H_
|
||||
#define NTCORE_CPP_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "llvm/ArrayRef.h"
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
#include "nt_Value.h"
|
||||
|
||||
namespace nt {
|
||||
|
||||
using llvm::ArrayRef;
|
||||
using llvm::StringRef;
|
||||
|
||||
/** NetworkTables Entry Information */
|
||||
struct EntryInfo {
|
||||
/** Entry name */
|
||||
std::string name;
|
||||
|
||||
/** Entry type */
|
||||
NT_Type type;
|
||||
|
||||
/** Entry flags */
|
||||
unsigned int flags;
|
||||
|
||||
/** Timestamp of last change to entry (type or value). */
|
||||
unsigned long long last_change;
|
||||
};
|
||||
|
||||
/** NetworkTables Connection Information */
|
||||
struct ConnectionInfo {
|
||||
std::string remote_id;
|
||||
std::string remote_ip;
|
||||
unsigned int remote_port;
|
||||
unsigned long long last_update;
|
||||
unsigned int protocol_version;
|
||||
};
|
||||
|
||||
/** NetworkTables RPC Parameter Definition */
|
||||
struct RpcParamDef {
|
||||
RpcParamDef() = default;
|
||||
RpcParamDef(StringRef name_, std::shared_ptr<Value> def_value_)
|
||||
: name(name_), def_value(def_value_) {}
|
||||
|
||||
std::string name;
|
||||
std::shared_ptr<Value> def_value;
|
||||
};
|
||||
|
||||
/** NetworkTables RPC Result Definition */
|
||||
struct RpcResultDef {
|
||||
RpcResultDef() = default;
|
||||
RpcResultDef(StringRef name_, NT_Type type_) : name(name_), type(type_) {}
|
||||
|
||||
std::string name;
|
||||
NT_Type type;
|
||||
};
|
||||
|
||||
/** NetworkTables RPC Definition */
|
||||
struct RpcDefinition {
|
||||
unsigned int version;
|
||||
std::string name;
|
||||
std::vector<RpcParamDef> params;
|
||||
std::vector<RpcResultDef> results;
|
||||
};
|
||||
|
||||
/** NetworkTables RPC Call Data */
|
||||
struct RpcCallInfo {
|
||||
unsigned int rpc_id;
|
||||
unsigned int call_uid;
|
||||
std::string name;
|
||||
std::string params;
|
||||
};
|
||||
|
||||
/*
|
||||
* Table Functions
|
||||
*/
|
||||
|
||||
/** Get Entry Value.
|
||||
* Returns copy of current entry value.
|
||||
* Note that one of the type options is "unassigned".
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @return entry value
|
||||
*/
|
||||
std::shared_ptr<Value> GetEntryValue(StringRef name);
|
||||
|
||||
/** Set Default Entry Value
|
||||
* Returns copy of current entry value if it exists.
|
||||
* Otherwise, sets passed in value, and returns set value.
|
||||
* Note that one of the type options is "unassigned".
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param value value to be set if name does not exist
|
||||
* @return False on error (value not set), True on success
|
||||
*/
|
||||
bool SetDefaultEntryValue(StringRef name, std::shared_ptr<Value> value);
|
||||
|
||||
/** Set Entry Value.
|
||||
* Sets new entry value. If type of new value differs from the type of the
|
||||
* currently stored entry, returns error and does not update value.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param value new entry value
|
||||
* @return False on error (type mismatch), True on success
|
||||
*/
|
||||
bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
|
||||
|
||||
/** Set Entry Type and Value.
|
||||
* Sets new entry value. If type of new value differs from the type of the
|
||||
* currently stored entry, the currently stored entry type is overridden
|
||||
* (generally this will generate an Entry Assignment message).
|
||||
*
|
||||
* This is NOT the preferred method to update a value; generally
|
||||
* SetEntryValue() should be used instead, with appropriate error handling.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param value new entry value
|
||||
*/
|
||||
void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
|
||||
|
||||
/** Set Entry Flags.
|
||||
*/
|
||||
void SetEntryFlags(StringRef name, unsigned int flags);
|
||||
|
||||
/** Get Entry Flags.
|
||||
*/
|
||||
unsigned int GetEntryFlags(StringRef name);
|
||||
|
||||
/** Delete Entry.
|
||||
* Deletes an entry. This is a new feature in version 3.0 of the protocol,
|
||||
* so this may not have an effect if any other node in the network is not
|
||||
* version 3.0 or newer.
|
||||
*
|
||||
* Note: GetConnections() can be used to determine the protocol version
|
||||
* of direct remote connection(s), but this is not sufficient to determine
|
||||
* if all nodes in the network are version 3.0 or newer.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
*/
|
||||
void DeleteEntry(StringRef name);
|
||||
|
||||
/** Delete All Entries.
|
||||
* Deletes ALL table entries. This is a new feature in version 3.0 of the
|
||||
* so this may not have an effect if any other node in the network is not
|
||||
* version 3.0 or newer.
|
||||
*
|
||||
* Note: GetConnections() can be used to determine the protocol version
|
||||
* of direct remote connection(s), but this is not sufficient to determine
|
||||
* if all nodes in the network are version 3.0 or newer.
|
||||
*/
|
||||
void DeleteAllEntries();
|
||||
|
||||
/** Get Entry Information.
|
||||
* Returns an array of entry information (name, entry type,
|
||||
* and timestamp of last change to type/value). The results are optionally
|
||||
* filtered by string prefix and entry type to only return a subset of all
|
||||
* entries.
|
||||
*
|
||||
* @param prefix entry name required prefix; only entries whose name
|
||||
* starts with this string are returned
|
||||
* @param types bitmask of NT_Type values; 0 is treated specially
|
||||
* as a "don't care"
|
||||
* @return Array of entry information.
|
||||
*/
|
||||
std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
|
||||
|
||||
/** Flush Entries.
|
||||
* Forces an immediate flush of all local entry changes to network.
|
||||
* Normally this is done on a regularly scheduled interval (see
|
||||
* NT_SetUpdateRate()).
|
||||
*
|
||||
* Note: flushes are rate limited to avoid excessive network traffic. If
|
||||
* the time between calls is too short, the flush will occur after the minimum
|
||||
* time elapses (rather than immediately).
|
||||
*/
|
||||
void Flush();
|
||||
|
||||
/*
|
||||
* Callback Creation Functions
|
||||
*/
|
||||
|
||||
void SetListenerOnStart(std::function<void()> on_start);
|
||||
void SetListenerOnExit(std::function<void()> on_exit);
|
||||
|
||||
typedef std::function<void(unsigned int uid, StringRef name,
|
||||
std::shared_ptr<Value> value, unsigned int flags)>
|
||||
EntryListenerCallback;
|
||||
|
||||
typedef std::function<void(unsigned int uid, bool connected,
|
||||
const ConnectionInfo& conn)>
|
||||
ConnectionListenerCallback;
|
||||
|
||||
unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
|
||||
unsigned int flags);
|
||||
void RemoveEntryListener(unsigned int entry_listener_uid);
|
||||
unsigned int AddConnectionListener(ConnectionListenerCallback callback,
|
||||
bool immediate_notify);
|
||||
void RemoveConnectionListener(unsigned int conn_listener_uid);
|
||||
|
||||
bool NotifierDestroyed();
|
||||
|
||||
/*
|
||||
* Remote Procedure Call Functions
|
||||
*/
|
||||
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
const double kTimeout_Indefinite = -1;
|
||||
#else
|
||||
constexpr double kTimeout_Indefinite = -1;
|
||||
#endif
|
||||
|
||||
void SetRpcServerOnStart(std::function<void()> on_start);
|
||||
void SetRpcServerOnExit(std::function<void()> on_exit);
|
||||
|
||||
typedef std::function<std::string(StringRef name, StringRef params,
|
||||
const ConnectionInfo& conn_info)>
|
||||
RpcCallback;
|
||||
|
||||
void CreateRpc(StringRef name, StringRef def, RpcCallback callback);
|
||||
void CreatePolledRpc(StringRef name, StringRef def);
|
||||
|
||||
bool PollRpc(bool blocking, RpcCallInfo* call_info);
|
||||
bool PollRpc(bool blocking, double time_out, RpcCallInfo* call_info);
|
||||
void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
|
||||
StringRef result);
|
||||
|
||||
unsigned int CallRpc(StringRef name, StringRef params);
|
||||
bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result);
|
||||
bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out,
|
||||
std::string* result);
|
||||
void CancelBlockingRpcResult(unsigned int call_uid);
|
||||
|
||||
std::string PackRpcDefinition(const RpcDefinition& def);
|
||||
bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def);
|
||||
std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values);
|
||||
std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
|
||||
ArrayRef<NT_Type> types);
|
||||
|
||||
/*
|
||||
* Client/Server Functions
|
||||
*/
|
||||
void SetNetworkIdentity(StringRef name);
|
||||
unsigned int GetNetworkMode();
|
||||
void StartServer(StringRef persist_filename, const char* listen_address,
|
||||
unsigned int port);
|
||||
void StopServer();
|
||||
void StartClient();
|
||||
void StartClient(const char* server_name, unsigned int port);
|
||||
void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
|
||||
void StopClient();
|
||||
void SetServer(const char* server_name, unsigned int port);
|
||||
void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
|
||||
void StartDSClient(unsigned int port);
|
||||
void StopDSClient();
|
||||
void StopRpcServer();
|
||||
void StopNotifier();
|
||||
void SetUpdateRate(double interval);
|
||||
std::vector<ConnectionInfo> GetConnections();
|
||||
|
||||
/*
|
||||
* Persistent Functions
|
||||
*/
|
||||
/* return error string, or nullptr if successful */
|
||||
const char* SavePersistent(StringRef filename);
|
||||
const char* LoadPersistent(
|
||||
StringRef filename, std::function<void(size_t line, const char* msg)> warn);
|
||||
|
||||
/*
|
||||
* Utility Functions
|
||||
*/
|
||||
|
||||
/* timestamp */
|
||||
unsigned long long Now();
|
||||
|
||||
/* logging */
|
||||
typedef std::function<void(unsigned int level, const char* file,
|
||||
unsigned int line, const char* msg)>
|
||||
LogFunc;
|
||||
void SetLogger(LogFunc func, unsigned int min_level);
|
||||
|
||||
} // namespace nt
|
||||
|
||||
#endif /* NTCORE_CPP_H_ */
|
||||
84
src/main/native/include/ntcore_test.h
Normal file
84
src/main/native/include/ntcore_test.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef NTCORE_TEST_H_
|
||||
#define NTCORE_TEST_H_
|
||||
|
||||
#include "ntcore.h"
|
||||
|
||||
// Functions in this header are to be used only for testing
|
||||
|
||||
extern "C" {
|
||||
struct NT_String* NT_GetStringForTesting(const char* string, int* struct_size);
|
||||
// No need for free as one already exists in main library
|
||||
|
||||
struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name,
|
||||
enum NT_Type type,
|
||||
unsigned int flags,
|
||||
unsigned long long last_change,
|
||||
int* struct_size);
|
||||
|
||||
void NT_FreeEntryInfoForTesting(struct NT_EntryInfo* info);
|
||||
|
||||
struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting(
|
||||
const char* remote_id, const char* remote_ip, unsigned int remote_port,
|
||||
unsigned long long last_update, unsigned int protocol_version,
|
||||
int* struct_size);
|
||||
|
||||
void NT_FreeConnectionInfoForTesting(struct NT_ConnectionInfo* info);
|
||||
|
||||
struct NT_Value* NT_GetValueBooleanForTesting(unsigned long long last_change,
|
||||
int val, int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueDoubleForTesting(unsigned long long last_change,
|
||||
double val, int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueStringForTesting(unsigned long long last_change,
|
||||
const char* str, int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueRawForTesting(unsigned long long last_change,
|
||||
const char* raw, int raw_len,
|
||||
int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueBooleanArrayForTesting(
|
||||
unsigned long long last_change, const int* arr, size_t array_len,
|
||||
int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueDoubleArrayForTesting(
|
||||
unsigned long long last_change, const double* arr, size_t array_len,
|
||||
int* struct_size);
|
||||
|
||||
struct NT_Value* NT_GetValueStringArrayForTesting(
|
||||
unsigned long long last_change, const struct NT_String* arr,
|
||||
size_t array_len, int* struct_size);
|
||||
// No need for free as one already exists in the main library
|
||||
|
||||
struct NT_RpcParamDef* NT_GetRpcParamDefForTesting(const char* name,
|
||||
const struct NT_Value* val,
|
||||
int* struct_size);
|
||||
|
||||
void NT_FreeRpcParamDefForTesting(struct NT_RpcParamDef* def);
|
||||
|
||||
struct NT_RpcResultDef* NT_GetRpcResultsDefForTesting(const char* name,
|
||||
enum NT_Type type,
|
||||
int* struct_size);
|
||||
|
||||
void NT_FreeRpcResultsDefForTesting(struct NT_RpcResultDef* def);
|
||||
|
||||
struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting(
|
||||
unsigned int version, const char* name, size_t num_params,
|
||||
const struct NT_RpcParamDef* params, size_t num_results,
|
||||
const struct NT_RpcResultDef* results, int* struct_size);
|
||||
// No need for free as one already exists in the main library
|
||||
|
||||
struct NT_RpcCallInfo* NT_GetRpcCallInfoForTesting(
|
||||
unsigned int rpc_id, unsigned int call_uid, const char* name,
|
||||
const char* params, size_t params_len, int* struct_size);
|
||||
// No need for free as one already exists in the main library
|
||||
}
|
||||
|
||||
#endif /* NTCORE_TEST_H_ */
|
||||
493
src/main/native/include/tables/ITable.h
Normal file
493
src/main/native/include/tables/ITable.h
Normal file
@@ -0,0 +1,493 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef ITABLE_H_
|
||||
#define ITABLE_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "llvm/StringRef.h"
|
||||
#include "nt_Value.h"
|
||||
#include "support/deprecated.h"
|
||||
|
||||
class ITableListener;
|
||||
|
||||
/**
|
||||
* A table whose values can be read and written to
|
||||
*/
|
||||
class ITable {
|
||||
public:
|
||||
/**
|
||||
* Determines whether the given key is in this table.
|
||||
*
|
||||
* @param key the key to search for
|
||||
* @return true if the table as a value assigned to the given key
|
||||
*/
|
||||
virtual bool ContainsKey(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Determines whether there exists a non-empty subtable for this key
|
||||
* in this table.
|
||||
*
|
||||
* @param key the key to search for
|
||||
* @return true if there is a subtable with the key which contains at least
|
||||
* one key/subtable of its own
|
||||
*/
|
||||
virtual bool ContainsSubTable(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the subtable in this table for the given name.
|
||||
*
|
||||
* @param key the name of the table relative to this one
|
||||
* @return a sub table relative to this one
|
||||
*/
|
||||
virtual std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* @param types bitmask of types; 0 is treated as a "don't care".
|
||||
* @return keys currently in the table
|
||||
*/
|
||||
virtual std::vector<std::string> GetKeys(int types = 0) const = 0;
|
||||
|
||||
/**
|
||||
* @return subtables currently in the table
|
||||
*/
|
||||
virtual std::vector<std::string> GetSubTables() const = 0;
|
||||
|
||||
/**
|
||||
* Makes a key's value persistent through program restarts.
|
||||
*
|
||||
* @param key the key to make persistent
|
||||
*/
|
||||
virtual void SetPersistent(llvm::StringRef key) = 0;
|
||||
|
||||
/**
|
||||
* Stop making a key's value persistent through program restarts.
|
||||
* The key cannot be null.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
virtual void ClearPersistent(llvm::StringRef key) = 0;
|
||||
|
||||
/**
|
||||
* Returns whether the value is persistent through program restarts.
|
||||
* The key cannot be null.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
virtual bool IsPersistent(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Sets flags on the specified key in this table. The key can
|
||||
* not be null.
|
||||
*
|
||||
* @param key the key name
|
||||
* @param flags the flags to set (bitmask)
|
||||
*/
|
||||
virtual void SetFlags(llvm::StringRef key, unsigned int flags) = 0;
|
||||
|
||||
/**
|
||||
* Clears flags on the specified key in this table. The key can
|
||||
* not be null.
|
||||
*
|
||||
* @param key the key name
|
||||
* @param flags the flags to clear (bitmask)
|
||||
*/
|
||||
virtual void ClearFlags(llvm::StringRef key, unsigned int flags) = 0;
|
||||
|
||||
/**
|
||||
* Returns the flags for the specified key.
|
||||
*
|
||||
* @param key the key name
|
||||
* @return the flags, or 0 if the key is not defined
|
||||
*/
|
||||
virtual unsigned int GetFlags(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Deletes the specified key in this table.
|
||||
*
|
||||
* @param key the key name
|
||||
*/
|
||||
virtual void Delete(llvm::StringRef key) = 0;
|
||||
|
||||
/**
|
||||
* Gets the value associated with a key as an object
|
||||
*
|
||||
* @param key the key of the value to look up
|
||||
* @return the value associated with the given key, or nullptr if the key
|
||||
* does not exist
|
||||
*/
|
||||
virtual std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultValue(llvm::StringRef key,
|
||||
std::shared_ptr<nt::Value> defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Put a value in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutValue(llvm::StringRef key,
|
||||
std::shared_ptr<nt::Value> value) = 0;
|
||||
|
||||
/**
|
||||
* Put a number in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutNumber(llvm::StringRef key, double value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultNumber(llvm::StringRef key, double defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Gets the number associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetNumber(StringRef key, double defaultValue) instead")
|
||||
virtual double GetNumber(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the number associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*/
|
||||
virtual double GetNumber(llvm::StringRef key, double defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a string in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutString(llvm::StringRef key, llvm::StringRef value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultString(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Gets the string associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetString(StringRef key, StringRef defaultValue) instead")
|
||||
virtual std::string GetString(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the string associated with the given name. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the string. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::string GetString(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a boolean in the table
|
||||
*
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutBoolean(llvm::StringRef key, bool value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultBoolean(llvm::StringRef key, bool defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Gets the boolean associated with the given name.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @return the value associated with the given key
|
||||
* @throws TableKeyNotDefinedException if there is no value associated with
|
||||
* the given key
|
||||
* @deprecated This exception-raising method has been replaced by the
|
||||
* default-taking method.
|
||||
*/
|
||||
WPI_DEPRECATED(
|
||||
"Raises an exception if key not found; "
|
||||
"use GetBoolean(StringRef key, bool defaultValue) instead")
|
||||
virtual bool GetBoolean(llvm::StringRef key) const = 0;
|
||||
|
||||
/**
|
||||
* Gets the boolean associated with the given name. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
*
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*/
|
||||
virtual bool GetBoolean(llvm::StringRef key, bool defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a boolean array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*
|
||||
* @note The array must be of int's rather than of bool's because
|
||||
* std::vector<bool> is special-cased in C++. 0 is false, any
|
||||
* non-zero value is true.
|
||||
*/
|
||||
virtual bool PutBooleanArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<int> value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultBooleanArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<int> defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Returns the boolean array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*
|
||||
* @note The returned array is std::vector<int> instead of std::vector<bool>
|
||||
* because std::vector<bool> is special-cased in C++. 0 is false, any
|
||||
* non-zero value is true.
|
||||
*/
|
||||
virtual std::vector<int> GetBooleanArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<int> defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a number array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutNumberArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<double> value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultNumberArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<double> defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Returns the number array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::vector<double> GetNumberArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<double> defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a string array in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutStringArray(llvm::StringRef key,
|
||||
llvm::ArrayRef<std::string> value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultStringArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<std::string> defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Returns the string array the key maps to. If the key does not exist or is
|
||||
* of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the array. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::vector<std::string> GetStringArray(
|
||||
llvm::StringRef key, llvm::ArrayRef<std::string> defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Put a raw value (byte array) in the table
|
||||
* @param key the key to be assigned to
|
||||
* @param value the value that will be assigned
|
||||
* @return False if the table key already exists with a different type
|
||||
*/
|
||||
virtual bool PutRaw(llvm::StringRef key, llvm::StringRef value) = 0;
|
||||
|
||||
/**
|
||||
* Gets the current value in the table, setting it if it does not exist.
|
||||
* @param key the key
|
||||
* @param defaultValue the default value to set if key doesn't exist.
|
||||
* @returns False if the table key exists with a different type
|
||||
*/
|
||||
virtual bool SetDefaultRaw(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) = 0;
|
||||
|
||||
/**
|
||||
* Returns the raw value (byte array) the key maps to. If the key does not
|
||||
* exist or is of different type, it will return the default value.
|
||||
* @param key the key to look up
|
||||
* @param defaultValue the value to be returned if no value is found
|
||||
* @return the value associated with the given key or the given default value
|
||||
* if there is no value associated with the key
|
||||
*
|
||||
* @note This makes a copy of the raw contents. If the overhead of this is a
|
||||
* concern, use GetValue() instead.
|
||||
*/
|
||||
virtual std::string GetRaw(llvm::StringRef key,
|
||||
llvm::StringRef defaultValue) const = 0;
|
||||
|
||||
/**
|
||||
* Add a listener for changes to the table
|
||||
*
|
||||
* @param listener the listener to add
|
||||
*/
|
||||
virtual void AddTableListener(ITableListener* listener) = 0;
|
||||
|
||||
/**
|
||||
* Add a listener for changes to the table
|
||||
*
|
||||
* @param listener the listener to add
|
||||
* @param immediateNotify if true then this listener will be notified of all
|
||||
* current entries (marked as new)
|
||||
*/
|
||||
virtual void AddTableListener(ITableListener* listener,
|
||||
bool immediateNotify) = 0;
|
||||
|
||||
/**
|
||||
* Add a listener for changes to the table
|
||||
*
|
||||
* @param listener the listener to add
|
||||
* @param immediateNotify if true then this listener will be notified of all
|
||||
* current entries (marked as new)
|
||||
* @param flags bitmask of NT_NotifyKind specifying desired notifications
|
||||
*/
|
||||
virtual void AddTableListenerEx(ITableListener* listener,
|
||||
unsigned int flags) = 0;
|
||||
|
||||
/**
|
||||
* Add a listener for changes to a specific key the table
|
||||
*
|
||||
* @param key the key to listen for
|
||||
* @param listener the listener to add
|
||||
* @param immediateNotify if true then this listener will be notified of all
|
||||
* current entries (marked as new)
|
||||
*/
|
||||
virtual void AddTableListener(llvm::StringRef key, ITableListener* listener,
|
||||
bool immediateNotify) = 0;
|
||||
|
||||
/**
|
||||
* Add a listener for changes to a specific key the table
|
||||
*
|
||||
* @param key the key to listen for
|
||||
* @param listener the listener to add
|
||||
* @param immediateNotify if true then this listener will be notified of all
|
||||
* current entries (marked as new)
|
||||
* @param flags bitmask of NT_NotifyKind specifying desired notifications
|
||||
*/
|
||||
virtual void AddTableListenerEx(llvm::StringRef key, ITableListener* listener,
|
||||
unsigned int flags) = 0;
|
||||
|
||||
/**
|
||||
* This will immediately notify the listener of all current sub tables
|
||||
* @param listener the listener to add
|
||||
*/
|
||||
virtual void AddSubTableListener(ITableListener* listener) = 0;
|
||||
|
||||
/**
|
||||
* This will immediately notify the listener of all current sub tables
|
||||
* @param listener the listener to add
|
||||
* @param localNotify if true then this listener will be notified of all
|
||||
* local changes in addition to all remote changes
|
||||
*/
|
||||
virtual void AddSubTableListener(ITableListener* listener,
|
||||
bool localNotify) = 0;
|
||||
|
||||
/**
|
||||
* Remove a listener from receiving table events
|
||||
*
|
||||
* @param listener the listener to be removed
|
||||
*/
|
||||
virtual void RemoveTableListener(ITableListener* listener) = 0;
|
||||
|
||||
/**
|
||||
* Gets the full path of this table.
|
||||
*/
|
||||
virtual llvm::StringRef GetPath() const = 0;
|
||||
|
||||
};
|
||||
|
||||
#endif // ITABLE_H_
|
||||
47
src/main/native/include/tables/ITableListener.h
Normal file
47
src/main/native/include/tables/ITableListener.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* ITableListener.h
|
||||
*/
|
||||
|
||||
#ifndef ITABLELISTENER_H_
|
||||
#define ITABLELISTENER_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "llvm/StringRef.h"
|
||||
#include "nt_Value.h"
|
||||
|
||||
class ITable;
|
||||
|
||||
/**
|
||||
* A listener that listens to changes in values in a {@link ITable}
|
||||
*/
|
||||
class ITableListener {
|
||||
public:
|
||||
virtual ~ITableListener() = default;
|
||||
/**
|
||||
* Called when a key-value pair is changed in a {@link ITable}
|
||||
* @param source the table the key-value pair exists in
|
||||
* @param key the key associated with the value that changed
|
||||
* @param value the new value
|
||||
* @param isNew true if the key did not previously exist in the table,
|
||||
* otherwise it is false
|
||||
*/
|
||||
virtual void ValueChanged(ITable* source, llvm::StringRef key,
|
||||
std::shared_ptr<nt::Value> value, bool isNew) = 0;
|
||||
|
||||
/**
|
||||
* Extended version of ValueChanged. Called when a key-value pair is
|
||||
* changed in a {@link ITable}. The default implementation simply calls
|
||||
* ValueChanged(). If this is overridden, ValueChanged() will not be called.
|
||||
* @param source the table the key-value pair exists in
|
||||
* @param key the key associated with the value that changed
|
||||
* @param value the new value
|
||||
* @param flags update flags; for example, NT_NOTIFY_NEW if the key did not
|
||||
* previously exist in the table
|
||||
*/
|
||||
virtual void ValueChangedEx(ITable* source, llvm::StringRef key,
|
||||
std::shared_ptr<nt::Value> value,
|
||||
unsigned int flags);
|
||||
};
|
||||
|
||||
#endif /* ITABLELISTENER_H_ */
|
||||
36
src/main/native/include/tables/TableKeyNotDefinedException.h
Normal file
36
src/main/native/include/tables/TableKeyNotDefinedException.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef TABLEKEYNOTDEFINEDEXCEPTION_H_
|
||||
#define TABLEKEYNOTDEFINEDEXCEPTION_H_
|
||||
|
||||
#include <exception>
|
||||
#include "llvm/StringRef.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define NT_NOEXCEPT throw()
|
||||
#else
|
||||
#define NT_NOEXCEPT noexcept
|
||||
#endif
|
||||
|
||||
/**
|
||||
* An exception thrown when the lookup a a key-value fails in a {@link ITable}
|
||||
*/
|
||||
class TableKeyNotDefinedException : public std::exception {
|
||||
public:
|
||||
/**
|
||||
* @param key the key that was not defined in the table
|
||||
*/
|
||||
TableKeyNotDefinedException(llvm::StringRef key);
|
||||
~TableKeyNotDefinedException() NT_NOEXCEPT;
|
||||
const char* what() const NT_NOEXCEPT override;
|
||||
|
||||
private:
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
#endif // TABLEKEYNOTDEFINEDEXCEPTION_H_
|
||||
Reference in New Issue
Block a user