Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2011-2017. 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 | #pragma once |
| 9 | |
| 10 | #include <map> |
| 11 | #include <memory> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "ErrorBase.h" |
| 16 | #include "networktables/NetworkTable.h" |
| 17 | #include "tables/ITableListener.h" |
| 18 | |
| 19 | namespace frc { |
| 20 | |
| 21 | /** |
| 22 | * The preferences class provides a relatively simple way to save important |
| 23 | * values to the roboRIO to access the next time the roboRIO is booted. |
| 24 | * |
| 25 | * <p>This class loads and saves from a file inside the roboRIO. The user can |
| 26 | * not access the file directly, but may modify values at specific fields which |
| 27 | * will then be automatically periodically saved to the file by the NetworkTable |
| 28 | * server.</p> |
| 29 | * |
| 30 | * <p>This class is thread safe.</p> |
| 31 | * |
| 32 | * <p>This will also interact with {@link NetworkTable} by creating a table |
| 33 | * called "Preferences" with all the key-value pairs.</p> |
| 34 | */ |
| 35 | class Preferences : public ErrorBase { |
| 36 | public: |
| 37 | static Preferences* GetInstance(); |
| 38 | |
| 39 | std::vector<std::string> GetKeys(); |
| 40 | std::string GetString(llvm::StringRef key, llvm::StringRef defaultValue = ""); |
| 41 | int GetInt(llvm::StringRef key, int defaultValue = 0); |
| 42 | double GetDouble(llvm::StringRef key, double defaultValue = 0.0); |
| 43 | float GetFloat(llvm::StringRef key, float defaultValue = 0.0); |
| 44 | bool GetBoolean(llvm::StringRef key, bool defaultValue = false); |
| 45 | int64_t GetLong(llvm::StringRef key, int64_t defaultValue = 0); |
| 46 | void PutString(llvm::StringRef key, llvm::StringRef value); |
| 47 | void PutInt(llvm::StringRef key, int value); |
| 48 | void PutDouble(llvm::StringRef key, double value); |
| 49 | void PutFloat(llvm::StringRef key, float value); |
| 50 | void PutBoolean(llvm::StringRef key, bool value); |
| 51 | void PutLong(llvm::StringRef key, int64_t value); |
| 52 | WPI_DEPRECATED( |
| 53 | "Saving is now automatically performed by the NetworkTables server.") |
| 54 | void Save(); |
| 55 | bool ContainsKey(llvm::StringRef key); |
| 56 | void Remove(llvm::StringRef key); |
| 57 | |
| 58 | protected: |
| 59 | Preferences(); |
| 60 | virtual ~Preferences() = default; |
| 61 | |
| 62 | private: |
| 63 | std::shared_ptr<ITable> m_table; |
| 64 | class Listener : public ITableListener { |
| 65 | public: |
| 66 | void ValueChanged(ITable* source, llvm::StringRef key, |
| 67 | std::shared_ptr<nt::Value> value, bool isNew) override; |
| 68 | void ValueChangedEx(ITable* source, llvm::StringRef key, |
| 69 | std::shared_ptr<nt::Value> value, |
| 70 | uint32_t flags) override; |
| 71 | }; |
| 72 | Listener m_listener; |
| 73 | }; |
| 74 | |
| 75 | } // namespace frc |