blob: 1e1beff13b158541c9338b219f54b5d69020d44e [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2011-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#pragma once
9
10#include "ErrorBase.h"
11#include "Task.h"
12#include <map>
13#include <string>
14#include <vector>
15#include "HAL/cpp/Semaphore.hpp"
16#include "tables/ITableListener.h"
17#include "networktables/NetworkTable.h"
18
19/**
20 * The preferences class provides a relatively simple way to save important
21 * values to
22 * the RoboRIO to access the next time the RoboRIO is booted.
23 *
24 * <p>This class loads and saves from a file
25 * inside the RoboRIO. The user can not access the file directly, but may
26 * modify values at specific
27 * fields which will then be automatically periodically saved to the file
28 * by the NetworkTable 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 */
35class 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 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 unsigned int flags) override;
71 };
72 Listener m_listener;
73};