blob: 0c82f545cb81cff01da82235234b57bf1928aa70 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/Preferences.h"
6
7#include <algorithm>
8
9#include <hal/FRCUsageReporting.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070010#include <networktables/NetworkTable.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080011#include <networktables/NetworkTableInstance.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080012
13using namespace frc;
14
15// The Preferences table name
Austin Schuh812d0d12021-11-04 20:16:48 -070016static constexpr std::string_view kTableName{"Preferences"};
17
18namespace {
19struct Instance {
20 Instance();
21
22 std::shared_ptr<nt::NetworkTable> table{
23 nt::NetworkTableInstance::GetDefault().GetTable(kTableName)};
24 NT_EntryListener listener;
25};
26} // namespace
27
28static Instance& GetInstance() {
29 static Instance instance;
30 return instance;
31}
Brian Silverman8fce7482020-01-05 13:18:21 -080032
33Preferences* Preferences::GetInstance() {
Austin Schuh812d0d12021-11-04 20:16:48 -070034 ::GetInstance();
Brian Silverman8fce7482020-01-05 13:18:21 -080035 static Preferences instance;
36 return &instance;
37}
38
Austin Schuh812d0d12021-11-04 20:16:48 -070039std::vector<std::string> Preferences::GetKeys() {
40 return ::GetInstance().table->GetKeys();
Brian Silverman8fce7482020-01-05 13:18:21 -080041}
42
Austin Schuh812d0d12021-11-04 20:16:48 -070043std::string Preferences::GetString(std::string_view key,
44 std::string_view defaultValue) {
45 return ::GetInstance().table->GetString(key, defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080046}
47
Austin Schuh812d0d12021-11-04 20:16:48 -070048int Preferences::GetInt(std::string_view key, int defaultValue) {
49 return static_cast<int>(::GetInstance().table->GetNumber(key, defaultValue));
Brian Silverman8fce7482020-01-05 13:18:21 -080050}
51
Austin Schuh812d0d12021-11-04 20:16:48 -070052double Preferences::GetDouble(std::string_view key, double defaultValue) {
53 return ::GetInstance().table->GetNumber(key, defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080054}
55
Austin Schuh812d0d12021-11-04 20:16:48 -070056float Preferences::GetFloat(std::string_view key, float defaultValue) {
57 return ::GetInstance().table->GetNumber(key, defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080058}
59
Austin Schuh812d0d12021-11-04 20:16:48 -070060bool Preferences::GetBoolean(std::string_view key, bool defaultValue) {
61 return ::GetInstance().table->GetBoolean(key, defaultValue);
Brian Silverman8fce7482020-01-05 13:18:21 -080062}
63
Austin Schuh812d0d12021-11-04 20:16:48 -070064int64_t Preferences::GetLong(std::string_view key, int64_t defaultValue) {
65 return static_cast<int64_t>(
66 ::GetInstance().table->GetNumber(key, defaultValue));
67}
68
69void Preferences::SetString(std::string_view key, std::string_view value) {
70 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -080071 entry.SetString(value);
72 entry.SetPersistent();
73}
74
Austin Schuh812d0d12021-11-04 20:16:48 -070075void Preferences::PutString(std::string_view key, std::string_view value) {
76 SetString(key, value);
77}
78
79void Preferences::InitString(std::string_view key, std::string_view value) {
80 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -080081 entry.SetDefaultString(value);
82}
83
Austin Schuh812d0d12021-11-04 20:16:48 -070084void Preferences::SetInt(std::string_view key, int value) {
85 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -080086 entry.SetDouble(value);
87 entry.SetPersistent();
88}
89
Austin Schuh812d0d12021-11-04 20:16:48 -070090void Preferences::PutInt(std::string_view key, int value) {
91 SetInt(key, value);
92}
93
94void Preferences::InitInt(std::string_view key, int value) {
95 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -080096 entry.SetDefaultDouble(value);
97}
98
Austin Schuh812d0d12021-11-04 20:16:48 -070099void Preferences::SetDouble(std::string_view key, double value) {
100 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800101 entry.SetDouble(value);
102 entry.SetPersistent();
103}
104
Austin Schuh812d0d12021-11-04 20:16:48 -0700105void Preferences::PutDouble(std::string_view key, double value) {
106 SetDouble(key, value);
107}
108
109void Preferences::InitDouble(std::string_view key, double value) {
110 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800111 entry.SetDefaultDouble(value);
112}
113
Austin Schuh812d0d12021-11-04 20:16:48 -0700114void Preferences::SetFloat(std::string_view key, float value) {
115 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800116 entry.SetDouble(value);
117 entry.SetPersistent();
118}
119
Austin Schuh812d0d12021-11-04 20:16:48 -0700120void Preferences::PutFloat(std::string_view key, float value) {
121 SetFloat(key, value);
122}
123
124void Preferences::InitFloat(std::string_view key, float value) {
125 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800126 entry.SetDefaultDouble(value);
127}
128
Austin Schuh812d0d12021-11-04 20:16:48 -0700129void Preferences::SetBoolean(std::string_view key, bool value) {
130 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800131 entry.SetBoolean(value);
132 entry.SetPersistent();
133}
134
Austin Schuh812d0d12021-11-04 20:16:48 -0700135void Preferences::PutBoolean(std::string_view key, bool value) {
136 SetBoolean(key, value);
137}
138
139void Preferences::InitBoolean(std::string_view key, bool value) {
140 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800141 entry.SetDefaultBoolean(value);
142}
143
Austin Schuh812d0d12021-11-04 20:16:48 -0700144void Preferences::SetLong(std::string_view key, int64_t value) {
145 auto entry = ::GetInstance().table->GetEntry(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800146 entry.SetDouble(value);
147 entry.SetPersistent();
148}
149
Austin Schuh812d0d12021-11-04 20:16:48 -0700150void Preferences::PutLong(std::string_view key, int64_t value) {
151 SetLong(key, value);
152}
153
154void Preferences::InitLong(std::string_view key, int64_t value) {
155 auto entry = ::GetInstance().table->GetEntry(key);
Austin Schuh1e69f942020-11-14 15:06:14 -0800156 entry.SetDefaultDouble(value);
157}
158
Austin Schuh812d0d12021-11-04 20:16:48 -0700159bool Preferences::ContainsKey(std::string_view key) {
160 return ::GetInstance().table->ContainsKey(key);
Brian Silverman8fce7482020-01-05 13:18:21 -0800161}
162
Austin Schuh812d0d12021-11-04 20:16:48 -0700163void Preferences::Remove(std::string_view key) {
164 ::GetInstance().table->Delete(key);
165}
Brian Silverman8fce7482020-01-05 13:18:21 -0800166
167void Preferences::RemoveAll() {
168 for (auto preference : GetKeys()) {
169 if (preference != ".type") {
170 Remove(preference);
171 }
172 }
173}
174
Austin Schuh812d0d12021-11-04 20:16:48 -0700175Instance::Instance() {
176 table->GetEntry(".type").SetString("RobotPreferences");
177 listener = table->AddEntryListener(
178 [=](nt::NetworkTable* table, std::string_view name,
Brian Silverman8fce7482020-01-05 13:18:21 -0800179 nt::NetworkTableEntry entry, std::shared_ptr<nt::Value> value,
180 int flags) { entry.SetPersistent(); },
181 NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
182 HAL_Report(HALUsageReporting::kResourceType_Preferences, 0);
183}