blob: 60147757c46199385277bb209540d268b6259e0f [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4b81d302019-12-14 20:53:14 -08002/* Copyright (c) 2011-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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#include "frc/Preferences.h"
9
10#include <algorithm>
11
James Kuszmaul4b81d302019-12-14 20:53:14 -080012#include <hal/FRCUsageReporting.h>
Brian Silverman41cdd3e2019-01-19 19:48:58 -080013#include <networktables/NetworkTableInstance.h>
14#include <wpi/StringRef.h>
15
16#include "frc/WPIErrors.h"
17
18using namespace frc;
19
20// The Preferences table name
21static wpi::StringRef kTableName{"Preferences"};
22
23Preferences* Preferences::GetInstance() {
24 static Preferences instance;
25 return &instance;
26}
27
28std::vector<std::string> Preferences::GetKeys() { return m_table->GetKeys(); }
29
30std::string Preferences::GetString(wpi::StringRef key,
31 wpi::StringRef defaultValue) {
32 return m_table->GetString(key, defaultValue);
33}
34
35int Preferences::GetInt(wpi::StringRef key, int defaultValue) {
36 return static_cast<int>(m_table->GetNumber(key, defaultValue));
37}
38
39double Preferences::GetDouble(wpi::StringRef key, double defaultValue) {
40 return m_table->GetNumber(key, defaultValue);
41}
42
43float Preferences::GetFloat(wpi::StringRef key, float defaultValue) {
44 return m_table->GetNumber(key, defaultValue);
45}
46
47bool Preferences::GetBoolean(wpi::StringRef key, bool defaultValue) {
48 return m_table->GetBoolean(key, defaultValue);
49}
50
51int64_t Preferences::GetLong(wpi::StringRef key, int64_t defaultValue) {
52 return static_cast<int64_t>(m_table->GetNumber(key, defaultValue));
53}
54
55void Preferences::PutString(wpi::StringRef key, wpi::StringRef value) {
56 auto entry = m_table->GetEntry(key);
57 entry.SetString(value);
58 entry.SetPersistent();
59}
60
61void Preferences::PutInt(wpi::StringRef key, int value) {
62 auto entry = m_table->GetEntry(key);
63 entry.SetDouble(value);
64 entry.SetPersistent();
65}
66
67void Preferences::PutDouble(wpi::StringRef key, double value) {
68 auto entry = m_table->GetEntry(key);
69 entry.SetDouble(value);
70 entry.SetPersistent();
71}
72
73void Preferences::PutFloat(wpi::StringRef key, float value) {
74 auto entry = m_table->GetEntry(key);
75 entry.SetDouble(value);
76 entry.SetPersistent();
77}
78
79void Preferences::PutBoolean(wpi::StringRef key, bool value) {
80 auto entry = m_table->GetEntry(key);
81 entry.SetBoolean(value);
82 entry.SetPersistent();
83}
84
85void Preferences::PutLong(wpi::StringRef key, int64_t value) {
86 auto entry = m_table->GetEntry(key);
87 entry.SetDouble(value);
88 entry.SetPersistent();
89}
90
91bool Preferences::ContainsKey(wpi::StringRef key) {
92 return m_table->ContainsKey(key);
93}
94
95void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); }
96
97void Preferences::RemoveAll() {
98 for (auto preference : GetKeys()) {
99 if (preference != ".type") {
100 Remove(preference);
101 }
102 }
103}
104
105Preferences::Preferences()
106 : m_table(nt::NetworkTableInstance::GetDefault().GetTable(kTableName)) {
107 m_table->GetEntry(".type").SetString("RobotPreferences");
108 m_listener = m_table->AddEntryListener(
109 [=](nt::NetworkTable* table, wpi::StringRef name,
110 nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value,
111 int flags) { entry.SetPersistent(); },
112 NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
113 HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
114}