Brian Silverman | f7bd1c2 | 2015-12-24 16:07:11 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #ifndef ITABLE_H_ |
| 9 | #define ITABLE_H_ |
| 10 | |
| 11 | #include <memory> |
| 12 | |
| 13 | #include "llvm/StringRef.h" |
| 14 | #include "nt_Value.h" |
| 15 | |
| 16 | // [[deprecated(msg)]] is a C++14 feature not supported by MSVC or GCC < 4.9. |
| 17 | // We provide an equivalent warning implementation for those compilers here. |
| 18 | #ifndef NT_DEPRECATED |
| 19 | #if defined(_MSC_VER) |
| 20 | #define NT_DEPRECATED(msg) __declspec(deprecated(msg)) |
| 21 | #elif defined(__GNUC__) |
| 22 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) |
| 23 | #if __cplusplus > 201103L |
| 24 | #define NT_DEPRECATED(msg) [[deprecated(msg)]] |
| 25 | #else |
| 26 | #define NT_DEPRECATED(msg) [[gnu::deprecated(msg)]] |
| 27 | #endif |
| 28 | #else |
| 29 | #define NT_DEPRECATED(msg) __attribute__((deprecated(msg))) |
| 30 | #endif |
| 31 | #elif __cplusplus > 201103L |
| 32 | #define NT_DEPRECATED(msg) [[deprecated(msg)]] |
| 33 | #else |
| 34 | #define NT_DEPRECATED(msg) /*nothing*/ |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | class ITableListener; |
| 39 | |
| 40 | /** |
| 41 | * A table whose values can be read and written to |
| 42 | */ |
| 43 | class ITable { |
| 44 | public: |
| 45 | /** |
| 46 | * Determines whether the given key is in this table. |
| 47 | * |
| 48 | * @param key the key to search for |
| 49 | * @return true if the table as a value assigned to the given key |
| 50 | */ |
| 51 | virtual bool ContainsKey(llvm::StringRef key) const = 0; |
| 52 | |
| 53 | /** |
| 54 | * Determines whether there exists a non-empty subtable for this key |
| 55 | * in this table. |
| 56 | * |
| 57 | * @param key the key to search for |
| 58 | * @return true if there is a subtable with the key which contains at least |
| 59 | * one key/subtable of its own |
| 60 | */ |
| 61 | virtual bool ContainsSubTable(llvm::StringRef key) const = 0; |
| 62 | |
| 63 | /** |
| 64 | * Gets the subtable in this table for the given name. |
| 65 | * |
| 66 | * @param key the name of the table relative to this one |
| 67 | * @return a sub table relative to this one |
| 68 | */ |
| 69 | virtual std::shared_ptr<ITable> GetSubTable(llvm::StringRef key) const = 0; |
| 70 | |
| 71 | /** |
| 72 | * @param types bitmask of types; 0 is treated as a "don't care". |
| 73 | * @return keys currently in the table |
| 74 | */ |
| 75 | virtual std::vector<std::string> GetKeys(int types = 0) const = 0; |
| 76 | |
| 77 | /** |
| 78 | * @return subtables currently in the table |
| 79 | */ |
| 80 | virtual std::vector<std::string> GetSubTables() const = 0; |
| 81 | |
| 82 | /** |
| 83 | * Makes a key's value persistent through program restarts. |
| 84 | * |
| 85 | * @param key the key to make persistent |
| 86 | */ |
| 87 | virtual void SetPersistent(llvm::StringRef key) = 0; |
| 88 | |
| 89 | /** |
| 90 | * Stop making a key's value persistent through program restarts. |
| 91 | * The key cannot be null. |
| 92 | * |
| 93 | * @param key the key name |
| 94 | */ |
| 95 | virtual void ClearPersistent(llvm::StringRef key) = 0; |
| 96 | |
| 97 | /** |
| 98 | * Returns whether the value is persistent through program restarts. |
| 99 | * The key cannot be null. |
| 100 | * |
| 101 | * @param key the key name |
| 102 | */ |
| 103 | virtual bool IsPersistent(llvm::StringRef key) const = 0; |
| 104 | |
| 105 | /** |
| 106 | * Sets flags on the specified key in this table. The key can |
| 107 | * not be null. |
| 108 | * |
| 109 | * @param key the key name |
| 110 | * @param flags the flags to set (bitmask) |
| 111 | */ |
| 112 | virtual void SetFlags(llvm::StringRef key, unsigned int flags) = 0; |
| 113 | |
| 114 | /** |
| 115 | * Clears flags on the specified key in this table. The key can |
| 116 | * not be null. |
| 117 | * |
| 118 | * @param key the key name |
| 119 | * @param flags the flags to clear (bitmask) |
| 120 | */ |
| 121 | virtual void ClearFlags(llvm::StringRef key, unsigned int flags) = 0; |
| 122 | |
| 123 | /** |
| 124 | * Returns the flags for the specified key. |
| 125 | * |
| 126 | * @param key the key name |
| 127 | * @return the flags, or 0 if the key is not defined |
| 128 | */ |
| 129 | virtual unsigned int GetFlags(llvm::StringRef key) const = 0; |
| 130 | |
| 131 | /** |
| 132 | * Deletes the specified key in this table. |
| 133 | * |
| 134 | * @param key the key name |
| 135 | */ |
| 136 | virtual void Delete(llvm::StringRef key) = 0; |
| 137 | |
| 138 | /** |
| 139 | * Gets the value associated with a key as an object |
| 140 | * |
| 141 | * @param key the key of the value to look up |
| 142 | * @return the value associated with the given key, or nullptr if the key |
| 143 | * does not exist |
| 144 | */ |
| 145 | virtual std::shared_ptr<nt::Value> GetValue(llvm::StringRef key) const = 0; |
| 146 | |
| 147 | /** |
| 148 | * Put a value in the table |
| 149 | * |
| 150 | * @param key the key to be assigned to |
| 151 | * @param value the value that will be assigned |
| 152 | * @return False if the table key already exists with a different type |
| 153 | */ |
| 154 | virtual bool PutValue(llvm::StringRef key, |
| 155 | std::shared_ptr<nt::Value> value) = 0; |
| 156 | |
| 157 | /** |
| 158 | * Put a number in the table |
| 159 | * |
| 160 | * @param key the key to be assigned to |
| 161 | * @param value the value that will be assigned |
| 162 | * @return False if the table key already exists with a different type |
| 163 | */ |
| 164 | virtual bool PutNumber(llvm::StringRef key, double value) = 0; |
| 165 | |
| 166 | /** |
| 167 | * Gets the number associated with the given name. |
| 168 | * |
| 169 | * @param key the key to look up |
| 170 | * @return the value associated with the given key |
| 171 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 172 | * the given key |
| 173 | * @deprecated This exception-raising method has been replaced by the |
| 174 | * default-taking method. |
| 175 | */ |
| 176 | NT_DEPRECATED("Raises an exception if key not found; " |
| 177 | "use GetNumber(StringRef key, double defaultValue) instead") |
| 178 | virtual double GetNumber(llvm::StringRef key) const = 0; |
| 179 | |
| 180 | /** |
| 181 | * Gets the number associated with the given name. |
| 182 | * |
| 183 | * @param key the key to look up |
| 184 | * @param defaultValue the value to be returned if no value is found |
| 185 | * @return the value associated with the given key or the given default value |
| 186 | * if there is no value associated with the key |
| 187 | */ |
| 188 | virtual double GetNumber(llvm::StringRef key, double defaultValue) const = 0; |
| 189 | |
| 190 | /** |
| 191 | * Put a string in the table |
| 192 | * |
| 193 | * @param key the key to be assigned to |
| 194 | * @param value the value that will be assigned |
| 195 | * @return False if the table key already exists with a different type |
| 196 | */ |
| 197 | virtual bool PutString(llvm::StringRef key, llvm::StringRef value) = 0; |
| 198 | |
| 199 | /** |
| 200 | * Gets the string associated with the given name. |
| 201 | * |
| 202 | * @param key the key to look up |
| 203 | * @return the value associated with the given key |
| 204 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 205 | * the given key |
| 206 | * @deprecated This exception-raising method has been replaced by the |
| 207 | * default-taking method. |
| 208 | */ |
| 209 | NT_DEPRECATED("Raises an exception if key not found; " |
| 210 | "use GetString(StringRef key, StringRef defaultValue) instead") |
| 211 | virtual std::string GetString(llvm::StringRef key) const = 0; |
| 212 | |
| 213 | /** |
| 214 | * Gets the string associated with the given name. If the key does not |
| 215 | * exist or is of different type, it will return the default value. |
| 216 | * |
| 217 | * @param key the key to look up |
| 218 | * @param defaultValue the value to be returned if no value is found |
| 219 | * @return the value associated with the given key or the given default value |
| 220 | * if there is no value associated with the key |
| 221 | */ |
| 222 | virtual std::string GetString(llvm::StringRef key, |
| 223 | llvm::StringRef defaultValue) const = 0; |
| 224 | |
| 225 | /** |
| 226 | * Put a boolean in the table |
| 227 | * |
| 228 | * @param key the key to be assigned to |
| 229 | * @param value the value that will be assigned |
| 230 | * @return False if the table key already exists with a different type |
| 231 | */ |
| 232 | virtual bool PutBoolean(llvm::StringRef key, bool value) = 0; |
| 233 | |
| 234 | /** |
| 235 | * Gets the boolean associated with the given name. |
| 236 | * |
| 237 | * @param key the key to look up |
| 238 | * @return the value associated with the given key |
| 239 | * @throws TableKeyNotDefinedException if there is no value associated with |
| 240 | * the given key |
| 241 | * @deprecated This exception-raising method has been replaced by the |
| 242 | * default-taking method. |
| 243 | */ |
| 244 | NT_DEPRECATED("Raises an exception if key not found; " |
| 245 | "use GetBoolean(StringRef key, bool defaultValue) instead") |
| 246 | virtual bool GetBoolean(llvm::StringRef key) const = 0; |
| 247 | |
| 248 | /** |
| 249 | * Gets the boolean associated with the given name. If the key does not |
| 250 | * exist or is of different type, it will return the default value. |
| 251 | * |
| 252 | * @param key the key to look up |
| 253 | * @param defaultValue the value to be returned if no value is found |
| 254 | * @return the value associated with the given key or the given default value |
| 255 | * if there is no value associated with the key |
| 256 | */ |
| 257 | virtual bool GetBoolean(llvm::StringRef key, bool defaultValue) const = 0; |
| 258 | |
| 259 | /** |
| 260 | * Add a listener for changes to the table |
| 261 | * |
| 262 | * @param listener the listener to add |
| 263 | */ |
| 264 | virtual void AddTableListener(ITableListener* listener) = 0; |
| 265 | |
| 266 | /** |
| 267 | * Add a listener for changes to the table |
| 268 | * |
| 269 | * @param listener the listener to add |
| 270 | * @param immediateNotify if true then this listener will be notified of all |
| 271 | * current entries (marked as new) |
| 272 | */ |
| 273 | virtual void AddTableListener(ITableListener* listener, |
| 274 | bool immediateNotify) = 0; |
| 275 | |
| 276 | /** |
| 277 | * Add a listener for changes to the table |
| 278 | * |
| 279 | * @param listener the listener to add |
| 280 | * @param immediateNotify if true then this listener will be notified of all |
| 281 | * current entries (marked as new) |
| 282 | * @param flags bitmask of NT_NotifyKind specifying desired notifications |
| 283 | */ |
| 284 | virtual void AddTableListenerEx(ITableListener* listener, |
| 285 | unsigned int flags) = 0; |
| 286 | |
| 287 | /** |
| 288 | * Add a listener for changes to a specific key the table |
| 289 | * |
| 290 | * @param key the key to listen for |
| 291 | * @param listener the listener to add |
| 292 | * @param immediateNotify if true then this listener will be notified of all |
| 293 | * current entries (marked as new) |
| 294 | */ |
| 295 | virtual void AddTableListener(llvm::StringRef key, ITableListener* listener, |
| 296 | bool immediateNotify) = 0; |
| 297 | |
| 298 | /** |
| 299 | * Add a listener for changes to a specific key the table |
| 300 | * |
| 301 | * @param key the key to listen for |
| 302 | * @param listener the listener to add |
| 303 | * @param immediateNotify if true then this listener will be notified of all |
| 304 | * current entries (marked as new) |
| 305 | * @param flags bitmask of NT_NotifyKind specifying desired notifications |
| 306 | */ |
| 307 | virtual void AddTableListenerEx(llvm::StringRef key, ITableListener* listener, |
| 308 | unsigned int flags) = 0; |
| 309 | |
| 310 | /** |
| 311 | * This will immediately notify the listener of all current sub tables |
| 312 | * @param listener the listener to add |
| 313 | */ |
| 314 | virtual void AddSubTableListener(ITableListener* listener) = 0; |
| 315 | |
| 316 | /** |
| 317 | * This will immediately notify the listener of all current sub tables |
| 318 | * @param listener the listener to add |
| 319 | * @param localNotify if true then this listener will be notified of all |
| 320 | * local changes in addition to all remote changes |
| 321 | */ |
| 322 | virtual void AddSubTableListener(ITableListener* listener, |
| 323 | bool localNotify) = 0; |
| 324 | |
| 325 | /** |
| 326 | * Remove a listener from receiving table events |
| 327 | * |
| 328 | * @param listener the listener to be removed |
| 329 | */ |
| 330 | virtual void RemoveTableListener(ITableListener* listener) = 0; |
| 331 | }; |
| 332 | |
| 333 | #endif // ITABLE_H_ |