mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
Adds SetDefault methods to NetworkTables (#54)
There was no way to atomically check for a key in the table, and then setting if it if non existant. Back before persistent this was not a problem, however now it is, as its possible for values to be added before team's robot programs start. This makes the old method of calling Put*** methods in RobotInit invalid. This adds SetDefault methods, which do this atomically.
This commit is contained in:
committed by
Peter Johnson
parent
6615a34e99
commit
58092c5190
@@ -267,6 +267,14 @@ class NetworkTable : public ITable {
|
||||
* @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.
|
||||
@@ -301,6 +309,15 @@ class NetworkTable : public ITable {
|
||||
* @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.
|
||||
@@ -336,6 +353,14 @@ class NetworkTable : public ITable {
|
||||
* @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.
|
||||
@@ -376,6 +401,15 @@ class NetworkTable : public ITable {
|
||||
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.
|
||||
@@ -403,6 +437,15 @@ class NetworkTable : public ITable {
|
||||
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.
|
||||
@@ -426,6 +469,15 @@ class NetworkTable : public ITable {
|
||||
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.
|
||||
@@ -448,6 +500,15 @@ class NetworkTable : public ITable {
|
||||
* @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
|
||||
@@ -471,6 +532,15 @@ class NetworkTable : public ITable {
|
||||
* @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
|
||||
|
||||
@@ -174,6 +174,19 @@ struct NT_RpcCallInfo {
|
||||
*/
|
||||
void NT_GetEntryValue(const char *name, size_t name_len,
|
||||
struct NT_Value *value);
|
||||
|
||||
/** 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 name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryValue(const char* name, size_t name_len,
|
||||
const struct NT_Value *default_value);
|
||||
|
||||
/** Set Entry Value.
|
||||
* Sets new entry value. If type of new value differs from the type of the
|
||||
@@ -802,6 +815,107 @@ NT_String *NT_GetEntryStringArray(const char *name, size_t name_len,
|
||||
unsigned long long *last_change,
|
||||
size_t *arr_size);
|
||||
|
||||
/* Set Default Values */
|
||||
|
||||
/** Set Default Entry Boolean.
|
||||
* Sets the default for the specified key to be a boolean.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_boolean value to be set if name does not exist
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryBoolean(const char *name, size_t name_len,
|
||||
int default_boolean);
|
||||
|
||||
/** Set Default Entry Double.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_double value to be set if name does not exist
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryDouble(const char *name, size_t name_len,
|
||||
double default_double);
|
||||
|
||||
/** Set Default Entry String.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @param default_len length of value
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryString(const char *name, size_t name_len,
|
||||
const char *default_value, size_t default_len);
|
||||
|
||||
/** Set Default Entry Raw.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @param default_len length of value array
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryRaw(const char *name, size_t name_len,
|
||||
const char *default_value, size_t default_len);
|
||||
|
||||
/** Set Default Entry Boolean Array.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @param default_size size of value array
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryBooleanArray(const char *name, size_t name_len,
|
||||
const int *default_value,
|
||||
size_t default_size);
|
||||
|
||||
/** Set Default Entry Double Array.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @param default_size size of value array
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryDoubleArray(const char *name, size_t name_len,
|
||||
const double *default_value,
|
||||
size_t default_size);
|
||||
|
||||
/** Set Default Entry String Array.
|
||||
* Sets the default for the specified key.
|
||||
* If key exists with same type, does not set value. Otherwise
|
||||
* sets value to the default.
|
||||
*
|
||||
* @param name entry name (UTF-8 string)
|
||||
* @param name_len length of name in bytes
|
||||
* @param default_value value to be set if name does not exist
|
||||
* @param default_size size of value array
|
||||
* @return 0 on error (value not set), 1 on success
|
||||
*/
|
||||
int NT_SetDefaultEntryStringArray(const char *name, size_t name_len,
|
||||
const struct NT_String* default_value,
|
||||
size_t default_size);
|
||||
|
||||
/* Entry Value Setters */
|
||||
|
||||
/** Set Entry Boolean
|
||||
|
||||
@@ -96,6 +96,17 @@ struct RpcCallInfo {
|
||||
*/
|
||||
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.
|
||||
|
||||
@@ -143,6 +143,15 @@ class ITable {
|
||||
* 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
|
||||
@@ -162,6 +171,14 @@ class ITable {
|
||||
* @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.
|
||||
@@ -195,6 +212,15 @@ class ITable {
|
||||
* @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.
|
||||
@@ -233,6 +259,14 @@ class ITable {
|
||||
* @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.
|
||||
@@ -271,6 +305,15 @@ class ITable {
|
||||
*/
|
||||
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
|
||||
@@ -298,6 +341,15 @@ class ITable {
|
||||
*/
|
||||
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
|
||||
@@ -321,6 +373,15 @@ class ITable {
|
||||
*/
|
||||
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
|
||||
@@ -343,6 +404,15 @@ class ITable {
|
||||
* @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
|
||||
|
||||
Reference in New Issue
Block a user