blob: a2c3bc4d4f2de99e8c6963cb427406dd9b43358f [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2011. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7#pragma once
8
9#include "ErrorBase.h"
10#include "Task.h"
11#include <map>
12#include <string>
13#include <vector>
14#include "HAL/cpp/Semaphore.hpp"
15#include "tables/ITableListener.h"
16#include "networktables/NetworkTable.h"
17
18/**
19 * The preferences class provides a relatively simple way to save important
20 * values to
21 * the RoboRIO to access the next time the RoboRIO is booted.
22 *
23 * <p>This class loads and saves from a file
24 * inside the RoboRIO. The user can not access the file directly, but may
25 * modify values at specific
26 * fields which will then be automatically periodically saved to the file
27 * by the NetworkTable server.</p>
28 *
29 * <p>This class is thread safe.</p>
30 *
31 * <p>This will also interact with {@link NetworkTable} by creating a table
32 * called "Preferences" with all the key-value pairs.</p>
33 */
34class Preferences : public ErrorBase {
35 public:
36 static Preferences *GetInstance();
37
38 std::vector<std::string> GetKeys();
39 std::string GetString(llvm::StringRef key, llvm::StringRef defaultValue = "");
40 int GetInt(llvm::StringRef key, int defaultValue = 0);
41 double GetDouble(llvm::StringRef key, double defaultValue = 0.0);
42 float GetFloat(llvm::StringRef key, float defaultValue = 0.0);
43 bool GetBoolean(llvm::StringRef key, bool defaultValue = false);
44 int64_t GetLong(llvm::StringRef key, int64_t defaultValue = 0);
45 void PutString(llvm::StringRef key, llvm::StringRef value);
46 void PutInt(llvm::StringRef key, int value);
47 void PutDouble(llvm::StringRef key, double value);
48 void PutFloat(llvm::StringRef key, float value);
49 void PutBoolean(llvm::StringRef key, bool value);
50 void PutLong(llvm::StringRef key, int64_t value);
51 DEPRECATED(
52 "Saving is now automatically performed by the NetworkTables server.")
53 void Save();
54 bool ContainsKey(llvm::StringRef key);
55 void Remove(llvm::StringRef key);
56
57 protected:
58 Preferences();
59 virtual ~Preferences() = default;
60
61 private:
62 std::shared_ptr<ITable> m_table;
63 class Listener : public ITableListener {
64 public:
65 void ValueChanged(ITable* source, llvm::StringRef key,
66 std::shared_ptr<nt::Value> value, bool isNew) override;
67 void ValueChangedEx(ITable* source, llvm::StringRef key,
68 std::shared_ptr<nt::Value> value,
69 unsigned int flags) override;
70 };
71 Listener m_listener;
72};