diff --git a/include/networktables/NetworkTable.h b/include/networktables/NetworkTable.h index 0a2b6927d8..5d8e9b7762 100644 --- a/include/networktables/NetworkTable.h +++ b/include/networktables/NetworkTable.h @@ -135,9 +135,9 @@ class NetworkTable : public ITable { /** * Gets the table with the specified key. If the table does not exist, a new - *table will be created.
+ * table will be created.
* This will automatically initialize network tables if it has not been - *already + * already. * * @param key * the key name @@ -357,6 +357,106 @@ class NetworkTable : public ITable { 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 is special-cased in C++. 0 is false, any + * non-zero value is true. + */ + virtual bool PutBooleanArray(llvm::StringRef key, + llvm::ArrayRef value) 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 instead of std::vector + * because std::vector is special-cased in C++. 0 is false, any + * non-zero value is true. + */ + virtual std::vector GetBooleanArray( + llvm::StringRef key, llvm::ArrayRef 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 value) 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 GetNumberArray( + llvm::StringRef key, llvm::ArrayRef 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 value) 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 GetStringArray( + llvm::StringRef key, + llvm::ArrayRef 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; + + /** + * 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 * diff --git a/include/tables/ITable.h b/include/tables/ITable.h index 4896d80870..28ee6c0feb 100644 --- a/include/tables/ITable.h +++ b/include/tables/ITable.h @@ -218,6 +218,9 @@ class ITable { * @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; @@ -256,6 +259,105 @@ class ITable { */ 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 is special-cased in C++. 0 is false, any + * non-zero value is true. + */ + virtual bool PutBooleanArray(llvm::StringRef key, + llvm::ArrayRef value) = 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 instead of std::vector + * because std::vector is special-cased in C++. 0 is false, any + * non-zero value is true. + */ + virtual std::vector GetBooleanArray( + llvm::StringRef key, llvm::ArrayRef 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 value) = 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 GetNumberArray( + llvm::StringRef key, llvm::ArrayRef 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 value) = 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 GetStringArray( + llvm::StringRef key, llvm::ArrayRef 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; + + /** + * 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 * diff --git a/src/networktables/NetworkTable.cpp b/src/networktables/NetworkTable.cpp index 7eb377225f..a585456385 100644 --- a/src/networktables/NetworkTable.cpp +++ b/src/networktables/NetworkTable.cpp @@ -366,6 +366,81 @@ bool NetworkTable::GetBoolean(StringRef key, bool defaultValue) const { return value->GetBoolean(); } +bool NetworkTable::PutBooleanArray(llvm::StringRef key, + llvm::ArrayRef value) { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + return nt::SetEntryValue(path, nt::Value::MakeBooleanArray(value)); +} + +std::vector NetworkTable::GetBooleanArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_BOOLEAN_ARRAY) + return defaultValue; + return value->GetBooleanArray(); +} + +bool NetworkTable::PutNumberArray(llvm::StringRef key, + llvm::ArrayRef value) { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + return nt::SetEntryValue(path, nt::Value::MakeDoubleArray(value)); +} + +std::vector NetworkTable::GetNumberArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_DOUBLE_ARRAY) + return defaultValue; + return value->GetDoubleArray(); +} + +bool NetworkTable::PutStringArray(llvm::StringRef key, + llvm::ArrayRef value) { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + return nt::SetEntryValue(path, nt::Value::MakeStringArray(value)); +} + +std::vector NetworkTable::GetStringArray( + llvm::StringRef key, llvm::ArrayRef defaultValue) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_STRING_ARRAY) + return defaultValue; + return value->GetStringArray(); +} + +bool NetworkTable::PutRaw(llvm::StringRef key, llvm::StringRef value) { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + return nt::SetEntryValue(path, nt::Value::MakeRaw(value)); +} + +std::string NetworkTable::GetRaw(llvm::StringRef key, + llvm::StringRef defaultValue) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_RAW) + return defaultValue; + return value->GetRaw(); +} + bool NetworkTable::PutValue(StringRef key, std::shared_ptr value) { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR;