diff --git a/include/networktables/NetworkTable.h b/include/networktables/NetworkTable.h index 37181ee22c..0664e66561 100644 --- a/include/networktables/NetworkTable.h +++ b/include/networktables/NetworkTable.h @@ -1,5 +1,12 @@ -#ifndef _NETWORKTABLE_H_ -#define _NETWORKTABLE_H_ +/*----------------------------------------------------------------------------*/ +/* 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 #include @@ -7,6 +14,9 @@ #include "tables/ITable.h" +/** + * A network table that knows its subtable path. + */ class NetworkTable : public ITable { private: struct private_init {}; @@ -51,8 +61,8 @@ class NetworkTable : public ITable { 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) + * 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 */ @@ -154,35 +164,43 @@ class NetworkTable : public ITable { * the key name * @return the networktable to be returned */ - std::shared_ptr GetSubTable(llvm::StringRef key) const; + std::shared_ptr GetSubTable(llvm::StringRef key) const override; /** - * Checks the table and tells if it contains the specified key + * Determines whether the given key is in this table. * - * @param key - * the key to be checked + * @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; + bool ContainsKey(llvm::StringRef key) const override; - bool ContainsSubTable(llvm::StringRef key) const; + /** + * 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 GetKeys(int types = 0) const; + std::vector GetKeys(int types = 0) const override; /** * @return subtables currently in the table */ - std::vector GetSubTables() const; + std::vector GetSubTables() const override; /** * Makes a key's value persistent through program restarts. * * @param key the key to make persistent */ - void SetPersistent(llvm::StringRef key); + void SetPersistent(llvm::StringRef key) override; /** * Stop making a key's value persistent through program restarts. @@ -190,7 +208,7 @@ class NetworkTable : public ITable { * * @param key the key name */ - void ClearPersistent(llvm::StringRef key); + void ClearPersistent(llvm::StringRef key) override; /** * Returns whether the value is persistent through program restarts. @@ -198,7 +216,7 @@ class NetworkTable : public ITable { * * @param key the key name */ - bool IsPersistent(llvm::StringRef key); + bool IsPersistent(llvm::StringRef key) const override; /** * Sets flags on the specified key in this table. The key can @@ -207,7 +225,7 @@ class NetworkTable : public ITable { * @param key the key name * @param flags the flags to set (bitmask) */ - void SetFlags(llvm::StringRef key, unsigned int flags); + void SetFlags(llvm::StringRef key, unsigned int flags) override; /** * Clears flags on the specified key in this table. The key can @@ -216,118 +234,144 @@ class NetworkTable : public ITable { * @param key the key name * @param flags the flags to clear (bitmask) */ - void ClearFlags(llvm::StringRef key, unsigned int flags); + void ClearFlags(llvm::StringRef key, unsigned int flags) override; /** * Returns the flags for the specified key. * - * @param key - * the key name + * @param key the key name * @return the flags, or 0 if the key is not defined */ - unsigned int GetFlags(llvm::StringRef key); + unsigned int GetFlags(llvm::StringRef key) const override; /** - * Deletes the specified key in this table. The key can - * not be null. + * Deletes the specified key in this table. * * @param key the key name */ - void Delete(llvm::StringRef key); + void Delete(llvm::StringRef key) override; /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. + * Put a number in the table * - * @param key - * the key - * @param value - * the value + * @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); + bool PutNumber(llvm::StringRef key, double value) override; /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value + * Gets the number associated with the given name. * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * @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. */ - double GetNumber(llvm::StringRef key, double defaultValue) const; + NT_DEPRECATED("Raises an exception if key not found; " + "use GetNumber(StringRef key, double defaultValue) instead") + virtual double GetNumber(llvm::StringRef key) const override; /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. + * Gets the number associated with the given name. * - * @param key - * the key - * @param value - * the 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 */ - bool PutString(llvm::StringRef key, llvm::StringRef value); + virtual double GetNumber(llvm::StringRef key, + double defaultValue) const override; /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value + * Put a string in the table * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * @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 */ - std::string GetString(llvm::StringRef key, - llvm::StringRef defaultValue) const; + virtual bool PutString(llvm::StringRef key, llvm::StringRef value) override; /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. + * Gets the string associated with the given name. * - * @param key - * the key - * @param value - * the value + * @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. */ - bool PutBoolean(llvm::StringRef key, bool value); + NT_DEPRECATED("Raises an exception if key not found; " + "use GetString(StringRef key, StringRef defaultValue) instead") + virtual std::string GetString(llvm::StringRef key) const override; /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value + * 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 name - * @param defaultValue - * the default value if the key is null - * @return the key + * @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 */ - bool GetBoolean(llvm::StringRef key, bool defaultValue) const; + virtual std::string GetString(llvm::StringRef key, + llvm::StringRef defaultValue) const override; /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. + * Put a boolean in the table * - * @param key the key name - * @param value the value to be put + * @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 value); + virtual bool PutBoolean(llvm::StringRef key, bool value) override; /** - * Returns the key that the name maps to. - * NOTE: If the value is a double, it will return a Double object, - * not a primitive. To get the primitive, use GetDouble + * Gets the boolean associated with the given name. * - * @param key - * the key name - * @return the key, or nullptr if the key is not defined + * @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. */ - std::shared_ptr GetValue(llvm::StringRef key) const; + NT_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 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 value) 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 GetValue(llvm::StringRef key) const override; }; -#endif +#endif // NETWORKTABLE_H_ diff --git a/include/tables/ITable.h b/include/tables/ITable.h index 9b93da9860..4896d80870 100644 --- a/include/tables/ITable.h +++ b/include/tables/ITable.h @@ -1,9 +1,9 @@ -/* - * ITable.h - * - * Created on: Sep 19, 2012 - * Author: Mitchell Wills - */ +/*----------------------------------------------------------------------------*/ +/* 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_ @@ -13,8 +13,33 @@ #include "llvm/StringRef.h" #include "nt_Value.h" +// [[deprecated(msg)]] is a C++14 feature not supported by MSVC or GCC < 4.9. +// We provide an equivalent warning implementation for those compilers here. +#ifndef NT_DEPRECATED + #if defined(_MSC_VER) + #define NT_DEPRECATED(msg) __declspec(deprecated(msg)) + #elif defined(__GNUC__) + #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) + #if __cplusplus > 201103L + #define NT_DEPRECATED(msg) [[deprecated(msg)]] + #else + #define NT_DEPRECATED(msg) [[gnu::deprecated(msg)]] + #endif + #else + #define NT_DEPRECATED(msg) __attribute__((deprecated(msg))) + #endif + #elif __cplusplus > 201103L + #define NT_DEPRECATED(msg) [[deprecated(msg)]] + #else + #define NT_DEPRECATED(msg) /*nothing*/ + #endif +#endif + class ITableListener; +/** + * A table whose values can be read and written to + */ class ITable { public: /** @@ -75,7 +100,7 @@ class ITable { * * @param key the key name */ - virtual bool IsPersistent(llvm::StringRef key) = 0; + virtual bool IsPersistent(llvm::StringRef key) const = 0; /** * Sets flags on the specified key in this table. The key can @@ -98,15 +123,13 @@ class ITable { /** * Returns the flags for the specified key. * - * @param key - * the key name + * @param key the key name * @return the flags, or 0 if the key is not defined */ - virtual unsigned int GetFlags(llvm::StringRef key) = 0; + virtual unsigned int GetFlags(llvm::StringRef key) const = 0; /** - * Deletes the specified key in this table. The key can - * not be null. + * Deletes the specified key in this table. * * @param key the key name */ @@ -116,9 +139,8 @@ class ITable { * 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 - * @throws TableKeyNotDefinedException if there is no value associated with - * the given key + * @return the value associated with the given key, or nullptr if the key + * does not exist */ virtual std::shared_ptr GetValue(llvm::StringRef key) const = 0; @@ -128,8 +150,6 @@ class ITable { * @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 - * @throws IllegalArgumentException when the value is not supported by the - * table */ virtual bool PutValue(llvm::StringRef key, std::shared_ptr value) = 0; @@ -143,6 +163,20 @@ class ITable { */ virtual bool PutNumber(llvm::StringRef key, double value) = 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. + */ + NT_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. * @@ -166,6 +200,21 @@ class ITable { * 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. + */ + NT_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 @@ -186,6 +235,21 @@ class ITable { * 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. + */ + NT_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 @@ -266,4 +330,4 @@ class ITable { virtual void RemoveTableListener(ITableListener* listener) = 0; }; -#endif /* ITABLE_H_ */ +#endif // ITABLE_H_ diff --git a/include/tables/TableKeyNotDefinedException.h b/include/tables/TableKeyNotDefinedException.h new file mode 100644 index 0000000000..2ff4cf9dc1 --- /dev/null +++ b/include/tables/TableKeyNotDefinedException.h @@ -0,0 +1,30 @@ +/*----------------------------------------------------------------------------*/ +/* 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 +#include "llvm/StringRef.h" + +/** + * 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() noexcept; + const char* what(); + + private: + std::string msg; +}; + +#endif // TABLEKEYNOTDEFINEDEXCEPTION_H_ diff --git a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java index ec5743214d..a2d1eb6c8f 100644 --- a/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java +++ b/java/src/edu/wpi/first/wpilibj/networktables/NetworkTable.java @@ -8,7 +8,6 @@ import java.util.*; /** * A network table that knows its subtable path. - * */ public class NetworkTable implements ITable, IRemote { /** @@ -240,10 +239,18 @@ public class NetworkTable implements ITable, IRemote { } } + /** + * {@inheritDoc} + */ + @Override public void addTableListener(ITableListener listener) { addTableListenerEx(listener, NOTIFY_NEW | NOTIFY_UPDATE); } + /** + * {@inheritDoc} + */ + @Override public void addTableListener(ITableListener listener, boolean immediateNotify) { int flags = NOTIFY_NEW | NOTIFY_UPDATE; @@ -285,6 +292,10 @@ public class NetworkTable implements ITable, IRemote { adapters.add(adapter); } + /** + * {@inheritDoc} + */ + @Override public void addTableListener(String key, ITableListener listener, boolean immediateNotify) { int flags = NOTIFY_NEW | NOTIFY_UPDATE; @@ -313,6 +324,10 @@ public class NetworkTable implements ITable, IRemote { } } + /** + * {@inheritDoc} + */ + @Override public synchronized void addTableListenerEx(String key, ITableListener listener, int flags) { @@ -328,6 +343,10 @@ public class NetworkTable implements ITable, IRemote { adapters.add(adapter); } + /** + * {@inheritDoc} + */ + @Override public void addSubTableListener(final ITableListener listener) { addSubTableListener(listener, false); } @@ -357,6 +376,10 @@ public class NetworkTable implements ITable, IRemote { } } + /** + * {@inheritDoc} + */ + @Override public synchronized void addSubTableListener(final ITableListener listener, boolean localNotify) { List adapters = listenerMap.get(listener); @@ -373,6 +396,10 @@ public class NetworkTable implements ITable, IRemote { adapters.add(adapter); } + /** + * {@inheritDoc} + */ + @Override public synchronized void removeTableListener(ITableListener listener) { List adapters = listenerMap.get(listener); if (adapters != null) { @@ -383,23 +410,17 @@ public class NetworkTable implements ITable, IRemote { } /** - * 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 + * {@inheritDoc} */ + @Override public ITable getSubTable(String key) { return new NetworkTable(path + PATH_SEPARATOR + key); } /** - * Checks the table and tells if it contains the specified key - * - * @param key - * the key to be checked + * {@inheritDoc} */ + @Override public boolean containsKey(String key) { return NetworkTablesJNI.containsKey(path + PATH_SEPARATOR + key); } @@ -426,15 +447,17 @@ public class NetworkTable implements ITable, IRemote { } /** - * @return keys currently in the table + * {@inheritDoc} */ + @Override public Set getKeys() { return getKeys(0); } /** - * @return subtables currently in the table + * {@inheritDoc} */ + @Override public Set getSubTables() { Set keys = new HashSet(); int prefixLen = path.length() + 1; @@ -449,198 +472,125 @@ public class NetworkTable implements ITable, IRemote { } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putNumber(String key, double value) { return NetworkTablesJNI.putDouble(path + PATH_SEPARATOR + key, value); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getNumber(String, double)}. */ + @Override + @Deprecated public double getNumber(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getDouble(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public double getNumber(String key, double defaultValue) { return NetworkTablesJNI.getDouble(path + PATH_SEPARATOR + key, defaultValue); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putString(String key, String value) { return NetworkTablesJNI.putString(path + PATH_SEPARATOR + key, value); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getString(String, String)}. */ + @Override + @Deprecated public String getString(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getString(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public String getString(String key, String defaultValue) { return NetworkTablesJNI.getString(path + PATH_SEPARATOR + key, defaultValue); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putBoolean(String key, boolean value) { return NetworkTablesJNI.putBoolean(path + PATH_SEPARATOR + key, value); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getBoolean(String, boolean)}. */ + @Override + @Deprecated public boolean getBoolean(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getBoolean(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public boolean getBoolean(String key, boolean defaultValue) { return NetworkTablesJNI.getBoolean(path + PATH_SEPARATOR + key, defaultValue); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putBooleanArray(String key, boolean[] value) { return NetworkTablesJNI.putBooleanArray(path + PATH_SEPARATOR + key, value); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putBooleanArray(String key, Boolean[] value) { return putBooleanArray(key, toNative(value)); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getBooleanArray(String, boolean[])}. */ + @Override + @Deprecated public boolean[] getBooleanArray(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getBooleanArray(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public boolean[] getBooleanArray(String key, boolean[] defaultValue) { return NetworkTablesJNI.getBooleanArray(path + PATH_SEPARATOR + key, defaultValue); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public Boolean[] getBooleanArray(String key, Boolean[] defaultValue) { try { return fromNative(getBooleanArray(key)); @@ -650,72 +600,44 @@ public class NetworkTable implements ITable, IRemote { } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putNumberArray(String key, double[] value) { return NetworkTablesJNI.putDoubleArray(path + PATH_SEPARATOR + key, value); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putNumberArray(String key, Double[] value) { return putNumberArray(key, toNative(value)); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getNumberArray(String, double[])}. */ + @Override + @Deprecated public double[] getNumberArray(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getDoubleArray(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public double[] getNumberArray(String key, double[] defaultValue) { return NetworkTablesJNI.getDoubleArray(path + PATH_SEPARATOR + key, defaultValue); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public Double[] getNumberArray(String key, Double[] defaultValue) { try { return fromNative(getNumberArray(key)); @@ -725,75 +647,44 @@ public class NetworkTable implements ITable, IRemote { } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putStringArray(String key, String[] value) { return NetworkTablesJNI.putStringArray(path + PATH_SEPARATOR + key, value); } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getStringArray(String, String[])}. */ + @Override + @Deprecated public String[] getStringArray(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getStringArray(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public String[] getStringArray(String key, String[] defaultValue) { return NetworkTablesJNI.getStringArray(path + PATH_SEPARATOR + key, defaultValue); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putRaw(String key, byte[] value) { return NetworkTablesJNI.putRaw(path + PATH_SEPARATOR + key, value); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key - * the key - * @param value - * the value - * @param len - * the length of the value - * @return False if the table key already exists with a different type + * {@inheritDoc} */ + @Override public boolean putRaw(String key, ByteBuffer value, int len) { if (!value.isDirect()) throw new IllegalArgumentException("must be a direct buffer"); @@ -803,43 +694,28 @@ public class NetworkTable implements ITable, IRemote { } /** - * Returns the key that the name maps to. - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getRaw(String, byte[])}. */ + @Override + @Deprecated public byte[] getRaw(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getRaw(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public byte[] getRaw(String key, byte[] defaultValue) { return NetworkTablesJNI.getRaw(path + PATH_SEPARATOR + key, defaultValue); } /** - * Maps the specified key to the specified value in this table. The key can - * not be null. The value can be retrieved by calling the get method with a - * key that is equal to the original key. - * - * @param key the key name - * @param value the value to be put - * @return False if the table key already exists with a different type - * @throws IllegalArgumentException when the value is not supported by the - * table + * {@inheritDoc} */ + @Override public boolean putValue(String key, Object value) throws IllegalArgumentException { if (value instanceof Boolean) return NetworkTablesJNI.putBoolean(path + PATH_SEPARATOR + key, ((Boolean)value).booleanValue()); @@ -869,6 +745,12 @@ public class NetworkTable implements ITable, IRemote { throw new IllegalArgumentException(key); } + /** + * {@inheritDoc} + * @deprecated Use get*Array functions instead. + */ + @Override + @Deprecated public void retrieveValue(String key, Object externalData) throws TableKeyNotDefinedException { Object value = getValue(key); if (value instanceof boolean[] && externalData instanceof BooleanArray) @@ -882,32 +764,20 @@ public class NetworkTable implements ITable, IRemote { } /** - * Returns the key that the name maps to. - * NOTE: If the value is a double, it will return a Double object, - * not a primitive. To get the primitive, use getDouble - * - * @param key - * the key name - * @return the key - * @throws TableKeyNotDefinedException - * if the specified key is null + * {@inheritDoc} + * @deprecated This exception-raising method has been replaced by the + * default-taking method {@link #getValue(String, Object)}. */ + @Override + @Deprecated public Object getValue(String key) throws TableKeyNotDefinedException { return NetworkTablesJNI.getValue(path + PATH_SEPARATOR + key); } /** - * Returns the key that the name maps to. If the key is null, it will return - * the default value - * NOTE: If the value is a double, it will return a Double object, - * not a primitive. To get the primitive, use getDouble - * - * @param key - * the key name - * @param defaultValue - * the default value if the key is null - * @return the key + * {@inheritDoc} */ + @Override public Object getValue(String key, Object defaultValue) { return NetworkTablesJNI.getValue(path + PATH_SEPARATOR + key, defaultValue); } @@ -916,75 +786,57 @@ public class NetworkTable implements ITable, IRemote { public static final int PERSISTENT = 1; /** - * Makes a key's value persistent through program restarts. - * The key cannot be null. - * - * @param key the key name + * {@inheritDoc} */ + @Override public void setPersistent(String key) { setFlags(key, PERSISTENT); } /** - * Stop making a key's value persistent through program restarts. - * The key cannot be null. - * - * @param key the key name + * {@inheritDoc} */ + @Override public void clearPersistent(String key) { clearFlags(key, PERSISTENT); } /** - * Returns whether the value is persistent through program restarts. - * The key cannot be null. - * - * @param key the key name - * @return True if the value is persistent. + * {@inheritDoc} */ + @Override public boolean isPersistent(String key) { return (getFlags(key) & PERSISTENT) != 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) + * {@inheritDoc} */ + @Override public void setFlags(String key, int flags) { NetworkTablesJNI.setEntryFlags(path + PATH_SEPARATOR + key, getFlags(key) | flags); } /** - * 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) + * {@inheritDoc} */ + @Override public void clearFlags(String key, int flags) { NetworkTablesJNI.setEntryFlags(path + PATH_SEPARATOR + key, getFlags(key) & ~flags); } /** - * Returns the flags for the specified key. - * - * @param key - * the key name - * @return the flags, or 0 if the key is not defined + * {@inheritDoc} */ + @Override public int getFlags(String key) { return NetworkTablesJNI.getEntryFlags(path + PATH_SEPARATOR + key); } /** - * Deletes the specified key in this table. The key can - * not be null. - * - * @param key the key name + * {@inheritDoc} */ + @Override public void delete(String key) { NetworkTablesJNI.deleteEntry(path + PATH_SEPARATOR + key); } @@ -1041,46 +893,31 @@ public class NetworkTable implements ITable, IRemote { */ /** - * @deprecated - * Maps the specified key to the specified value in this table. - * The key can not be null. - * The value can be retrieved by calling the get method with a key that is - * equal to the original key. - * @param key the key - * @param value the value - * @return False if the table key already exists with a different type - * @throws IllegalArgumentException if key is null + * {@inheritDoc} + * @deprecated Use {@link #putNumber(String, double)} instead. */ + @Override + @Deprecated public boolean putInt(String key, int value) { return putNumber(key, value); } /** - * @deprecated - * Returns the value at the specified key. - * @param key the key - * @return the value - * @throws TableKeyNotDefinedException if there is no value mapped to by the - * key - * @throws IllegalArgumentException if the value mapped to by the key is not - * an int - * @throws IllegalArgumentException if the key is null + * {@inheritDoc} + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Override + @Deprecated public int getInt(String key) throws TableKeyNotDefinedException { return (int)getNumber(key); } /** - * @deprecated - * Returns the value at the specified key. - * @param key the key - * @param defaultValue the value returned if the key is undefined - * @return the value - * @throws NetworkTableKeyNotDefined if there is no value mapped to by the key - * @throws IllegalArgumentException if the value mapped to by the key is not - * an int - * @throws IllegalArgumentException if the key is null + * {@inheritDoc} + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Override + @Deprecated public int getInt(String key, int defaultValue) throws TableKeyNotDefinedException { try { return (int)getNumber(key); @@ -1090,47 +927,31 @@ public class NetworkTable implements ITable, IRemote { } /** - * @deprecated - * Maps the specified key to the specified value in this table. - * The key can not be null. - * The value can be retrieved by calling the get method with a key that is - * equal to the original key. - * @param key the key - * @param value the value - * @return False if the table key already exists with a different type - * @throws IllegalArgumentException if key is null + * {@inheritDoc} + * @deprecated Use {@link #putNumber(String, double)} instead. */ + @Override + @Deprecated public boolean putDouble(String key, double value) { return putNumber(key, value); } /** - * @deprecated - * Returns the value at the specified key. - * @param key the key - * @return the value - * @throws TableKeyNotDefinedException if there is no - * value mapped to by the key - * @throws IllegalArgumentException if the value mapped to by the key is not a - * double - * @throws IllegalArgumentException if the key is null + * {@inheritDoc} + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Override + @Deprecated public double getDouble(String key) throws TableKeyNotDefinedException { return getNumber(key); } /** - * @deprecated - * Returns the value at the specified key. - * @param key the key - * @param defaultValue the value returned if the key is undefined - * @return the value - * @throws TableKeyNotDefinedException if there is no - * value mapped to by the key - * @throws IllegalArgumentException if the value mapped to by the key is not a - * double - * @throws IllegalArgumentException if the key is null + * {@inheritDoc} + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Override + @Deprecated public double getDouble(String key, double defaultValue) { return getNumber(key, defaultValue); } diff --git a/java/src/edu/wpi/first/wpilibj/tables/ITable.java b/java/src/edu/wpi/first/wpilibj/tables/ITable.java index 44ab586d74..81d12be241 100644 --- a/java/src/edu/wpi/first/wpilibj/tables/ITable.java +++ b/java/src/edu/wpi/first/wpilibj/tables/ITable.java @@ -7,13 +7,12 @@ import java.util.Set; /** * A table whose values can be read and written to - * - * @author Mitchell - * */ public interface ITable { /** + * Checks the table and tells if it contains the specified key + * * @param key the key to search for * @return true if the table as a value assigned to the given key */ @@ -27,6 +26,9 @@ public interface ITable { public boolean containsSubTable(String key); /** + * 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 name of the table relative to this one * @return a sub table relative to this one */ @@ -94,8 +96,7 @@ public interface ITable { /** * Returns the flags for the specified key. * - * @param key - * the key name + * @param key the key name * @return the flags, or 0 if the key is not defined */ public int getFlags(String key); @@ -109,14 +110,32 @@ public interface ITable { public void delete(String key); /** - * Gets the value associated with a key as an object + * Gets the value associated with a key as an object. If the key does not + * exist, it will return the default value + * NOTE: If the value is a double, it will return a Double object, + * not a primitive. To get the primitive, use + * {@link #getDouble(String, double)}. * @param key the key of the value 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 {@link #getValue(String, Object)}. */ + @Deprecated public Object getValue(String key) throws TableKeyNotDefinedException; + /** + * Gets the value associated with a key as an object. + * NOTE: If the value is a double, it will return a Double object, + * not a primitive. To get the primitive, use + * {@link #getDouble(String, double)}. + * @param key the key of the value to look up + * @param defaultValue the default value if the key is null + * @return the value associated with the given key + */ + public Object getValue(String key, Object defaultValue); + /** * Put a value in the table * @param key the key to be assigned to @@ -136,6 +155,7 @@ public interface ITable { * the given key * @deprecated Use get*Array functions instead. */ + @Deprecated public void retrieveValue(String key, Object externalValue) throws TableKeyNotDefinedException; /** @@ -146,13 +166,19 @@ public interface ITable { */ public boolean putNumber(String key, double value); /** + * Returns the number the key maps to. * @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 {@link #getNumber(String, double)}. */ + @Deprecated public double getNumber(String key) throws TableKeyNotDefinedException; /** + * Returns the number 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 @@ -168,13 +194,19 @@ public interface ITable { */ public boolean putString(String key, String value); /** + * Returns the string the key maps to. * @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 {@link #getString(String, String)}. */ + @Deprecated public String getString(String key) throws TableKeyNotDefinedException; /** + * Returns the string 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 @@ -190,13 +222,19 @@ public interface ITable { */ public boolean putBoolean(String key, boolean value); /** + * Returns the boolean the key maps to. * @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 {@link #getBoolean(String, boolean)}. */ + @Deprecated public boolean getBoolean(String key) throws TableKeyNotDefinedException; /** + * Returns the boolean 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 @@ -219,13 +257,19 @@ public interface ITable { */ public boolean putBooleanArray(String key, Boolean[] value); /** + * Returns the boolean array the key maps to. * @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 {@link #getBooleanArray(String, boolean[])}. */ + @Deprecated public boolean[] getBooleanArray(String key) throws TableKeyNotDefinedException; /** + * 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 @@ -233,6 +277,8 @@ public interface ITable { */ public boolean[] getBooleanArray(String key, boolean[] defaultValue); /** + * 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 @@ -255,13 +301,19 @@ public interface ITable { */ public boolean putNumberArray(String key, Double[] value); /** + * Returns the number array the key maps to. * @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 {@link #getNumberArray(String, double[])}. */ + @Deprecated public double[] getNumberArray(String key) throws TableKeyNotDefinedException; /** + * 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 @@ -269,6 +321,8 @@ public interface ITable { */ public double[] getNumberArray(String key, double[] defaultValue); /** + * 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 @@ -284,13 +338,19 @@ public interface ITable { */ public boolean putStringArray(String key, String[] value); /** + * Returns the string array the key maps to. * @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 {@link #getStringArray(String, String[])}. */ + @Deprecated public String[] getStringArray(String key) throws TableKeyNotDefinedException; /** + * 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 @@ -314,13 +374,19 @@ public interface ITable { */ public boolean putRaw(String key, ByteBuffer value, int len); /** + * Returns the raw value (byte array) the key maps to. * @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 {@link #getRaw(String, byte[])}. */ + @Deprecated public byte[] getRaw(String key) throws TableKeyNotDefinedException; /** + * 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 @@ -397,7 +463,6 @@ public interface ITable { */ /** - * @deprecated * Maps the specified key to the specified value in this table. * The key can not be null. * The value can be retrieved by calling the get method with a key that is @@ -406,11 +471,12 @@ public interface ITable { * @param value the value * @return False if the table key already exists with a different type * @throws IllegalArgumentException if key is null + * @deprecated Use {@link #putNumber(String, double)} instead. */ + @Deprecated public boolean putInt(String key, int value); /** - * @deprecated * Returns the value at the specified key. * @param key the key * @return the value @@ -419,25 +485,26 @@ public interface ITable { * @throws IllegalArgumentException if the value mapped to by the key is not * an int * @throws IllegalArgumentException if the key is null + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Deprecated public int getInt(String key) throws TableKeyNotDefinedException; /** - * @deprecated * Returns the value at the specified key. * @param key the key * @param defaultValue the value returned if the key is undefined * @return the value - * @throws TableKeyNotDefinedException if there is no value mapped to by the key * @throws IllegalArgumentException if the value mapped to by the key is not * an int * @throws IllegalArgumentException if the key is null + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Deprecated public int getInt(String key, int defaultValue) throws TableKeyNotDefinedException; /** - * @deprecated * Maps the specified key to the specified value in this table. * The key can not be null. * The value can be retrieved by calling the get method with a key that is @@ -446,11 +513,12 @@ public interface ITable { * @param value the value * @return False if the table key already exists with a different type * @throws IllegalArgumentException if key is null + * @deprecated Use {@link #putNumber(String, double)} instead. */ + @Deprecated public boolean putDouble(String key, double value); /** - * @deprecated * Returns the value at the specified key. * @param key the key * @return the value @@ -459,20 +527,21 @@ public interface ITable { * @throws IllegalArgumentException if the value mapped to by the key is not a * double * @throws IllegalArgumentException if the key is null + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Deprecated public double getDouble(String key) throws TableKeyNotDefinedException; /** - * @deprecated * Returns the value at the specified key. * @param key the key * @param defaultValue the value returned if the key is undefined * @return the value - * @throws TableKeyNotDefinedException if there is no - * value mapped to by the key * @throws IllegalArgumentException if the value mapped to by the key is not a * double * @throws IllegalArgumentException if the key is null + * @deprecated Use {@link #getNumber(String, double)} instead. */ + @Deprecated public double getDouble(String key, double defaultValue); } diff --git a/src/networktables/NetworkTable.cpp b/src/networktables/NetworkTable.cpp index a2f5790af8..4a707c52b9 100644 --- a/src/networktables/NetworkTable.cpp +++ b/src/networktables/NetworkTable.cpp @@ -5,6 +5,7 @@ #include "llvm/SmallString.h" #include "llvm/StringMap.h" #include "tables/ITableListener.h" +#include "tables/TableKeyNotDefinedException.h" #include "ntcore.h" using llvm::StringRef; @@ -251,7 +252,7 @@ void NetworkTable::ClearPersistent(StringRef key) { ClearFlags(key, NT_PERSISTENT); } -bool NetworkTable::IsPersistent(StringRef key) { +bool NetworkTable::IsPersistent(StringRef key) const { return (GetFlags(key) & NT_PERSISTENT) != 0; } @@ -269,7 +270,7 @@ void NetworkTable::ClearFlags(StringRef key, unsigned int flags) { nt::SetEntryFlags(path, nt::GetEntryFlags(path) & ~flags); } -unsigned int NetworkTable::GetFlags(StringRef key) { +unsigned int NetworkTable::GetFlags(StringRef key) const { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; path += key; @@ -290,6 +291,16 @@ bool NetworkTable::PutNumber(StringRef key, double value) { return nt::SetEntryValue(path, nt::Value::MakeDouble(value)); } +double NetworkTable::GetNumber(StringRef key) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_DOUBLE) + throw TableKeyNotDefinedException(path); + return value->GetDouble(); +} + double NetworkTable::GetNumber(StringRef key, double defaultValue) const { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; @@ -307,6 +318,16 @@ bool NetworkTable::PutString(StringRef key, StringRef value) { return nt::SetEntryValue(path, nt::Value::MakeString(value)); } +std::string NetworkTable::GetString(StringRef key) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_STRING) + throw TableKeyNotDefinedException(path); + return value->GetString(); +} + std::string NetworkTable::GetString(StringRef key, StringRef defaultValue) const { llvm::SmallString<128> path(m_path); @@ -325,6 +346,16 @@ bool NetworkTable::PutBoolean(StringRef key, bool value) { return nt::SetEntryValue(path, nt::Value::MakeBoolean(value)); } +bool NetworkTable::GetBoolean(StringRef key) const { + llvm::SmallString<128> path(m_path); + path += PATH_SEPARATOR_CHAR; + path += key; + auto value = nt::GetEntryValue(path); + if (!value || value->type() != NT_BOOLEAN) + throw TableKeyNotDefinedException(path); + return value->GetBoolean(); +} + bool NetworkTable::GetBoolean(StringRef key, bool defaultValue) const { llvm::SmallString<128> path(m_path); path += PATH_SEPARATOR_CHAR; diff --git a/src/tables/TableKeyNotDefinedException.cpp b/src/tables/TableKeyNotDefinedException.cpp new file mode 100644 index 0000000000..3fef63872d --- /dev/null +++ b/src/tables/TableKeyNotDefinedException.cpp @@ -0,0 +1,17 @@ +/*----------------------------------------------------------------------------*/ +/* 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. */ +/*----------------------------------------------------------------------------*/ + +#include "tables/TableKeyNotDefinedException.h" + +TableKeyNotDefinedException::TableKeyNotDefinedException(llvm::StringRef key) + : msg("Unknown Table Key: ") { + msg += key; +} + +const char* TableKeyNotDefinedException::what() { return msg.c_str(); } + +TableKeyNotDefinedException::~TableKeyNotDefinedException() noexcept {}