blob: 8c1eb87b75814c75dec00cbeced45c6415903583 [file] [log] [blame]
Brian Silverman8fce7482020-01-05 13:18:21 -08001/*----------------------------------------------------------------------------*/
Austin Schuh1e69f942020-11-14 15:06:14 -08002/* Copyright (c) 2011-2020 FIRST. All Rights Reserved. */
Brian Silverman8fce7482020-01-05 13:18:21 -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
12#include <hal/FRCUsageReporting.h>
13#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
Austin Schuh1e69f942020-11-14 15:06:14 -080061void Preferences::InitString(wpi::StringRef key, wpi::StringRef value) {
62 auto entry = m_table->GetEntry(key);
63 entry.SetDefaultString(value);
64}
65
Brian Silverman8fce7482020-01-05 13:18:21 -080066void Preferences::PutInt(wpi::StringRef key, int value) {
67 auto entry = m_table->GetEntry(key);
68 entry.SetDouble(value);
69 entry.SetPersistent();
70}
71
Austin Schuh1e69f942020-11-14 15:06:14 -080072void Preferences::InitInt(wpi::StringRef key, int value) {
73 auto entry = m_table->GetEntry(key);
74 entry.SetDefaultDouble(value);
75}
76
Brian Silverman8fce7482020-01-05 13:18:21 -080077void Preferences::PutDouble(wpi::StringRef key, double value) {
78 auto entry = m_table->GetEntry(key);
79 entry.SetDouble(value);
80 entry.SetPersistent();
81}
82
Austin Schuh1e69f942020-11-14 15:06:14 -080083void Preferences::InitDouble(wpi::StringRef key, double value) {
84 auto entry = m_table->GetEntry(key);
85 entry.SetDefaultDouble(value);
86}
87
Brian Silverman8fce7482020-01-05 13:18:21 -080088void Preferences::PutFloat(wpi::StringRef key, float value) {
89 auto entry = m_table->GetEntry(key);
90 entry.SetDouble(value);
91 entry.SetPersistent();
92}
93
Austin Schuh1e69f942020-11-14 15:06:14 -080094void Preferences::InitFloat(wpi::StringRef key, float value) {
95 auto entry = m_table->GetEntry(key);
96 entry.SetDefaultDouble(value);
97}
98
Brian Silverman8fce7482020-01-05 13:18:21 -080099void Preferences::PutBoolean(wpi::StringRef key, bool value) {
100 auto entry = m_table->GetEntry(key);
101 entry.SetBoolean(value);
102 entry.SetPersistent();
103}
104
Austin Schuh1e69f942020-11-14 15:06:14 -0800105void Preferences::InitBoolean(wpi::StringRef key, bool value) {
106 auto entry = m_table->GetEntry(key);
107 entry.SetDefaultBoolean(value);
108}
109
Brian Silverman8fce7482020-01-05 13:18:21 -0800110void Preferences::PutLong(wpi::StringRef key, int64_t value) {
111 auto entry = m_table->GetEntry(key);
112 entry.SetDouble(value);
113 entry.SetPersistent();
114}
115
Austin Schuh1e69f942020-11-14 15:06:14 -0800116void Preferences::InitLong(wpi::StringRef key, int64_t value) {
117 auto entry = m_table->GetEntry(key);
118 entry.SetDefaultDouble(value);
119}
120
Brian Silverman8fce7482020-01-05 13:18:21 -0800121bool Preferences::ContainsKey(wpi::StringRef key) {
122 return m_table->ContainsKey(key);
123}
124
125void Preferences::Remove(wpi::StringRef key) { m_table->Delete(key); }
126
127void Preferences::RemoveAll() {
128 for (auto preference : GetKeys()) {
129 if (preference != ".type") {
130 Remove(preference);
131 }
132 }
133}
134
135Preferences::Preferences()
136 : m_table(nt::NetworkTableInstance::GetDefault().GetTable(kTableName)) {
137 m_table->GetEntry(".type").SetString("RobotPreferences");
138 m_listener = m_table->AddEntryListener(
139 [=](nt::NetworkTable* table, wpi::StringRef name,
140 nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value,
141 int flags) { entry.SetPersistent(); },
142 NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
143 HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
144}