blob: 60a5520e3b10a4e03e9c25607e19b643edc62bf7 [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/*----------------------------------------------------------------------------*/
7
8#include "Preferences.h"
9
10#include "WPIErrors.h"
11#include "HAL/HAL.hpp"
12
13#include <stdio.h>
14#include <algorithm>
15
16/** The Preferences table name */
17static const char *kTableName = "Preferences";
18
19void Preferences::Listener::ValueChanged(ITable* source, llvm::StringRef key,
20 std::shared_ptr<nt::Value> value,
21 bool isNew) {}
22void Preferences::Listener::ValueChangedEx(ITable* source, llvm::StringRef key,
23 std::shared_ptr<nt::Value> value,
24 unsigned int flags) {
25 source->SetPersistent(key);
26}
27
28Preferences::Preferences() : m_table(NetworkTable::GetTable(kTableName)) {
29 m_table->AddTableListenerEx(&m_listener, NT_NOTIFY_NEW | NT_NOTIFY_IMMEDIATE);
30 HALReport(HALUsageReporting::kResourceType_Preferences, 0);
31}
32
33/**
34 * Get the one and only {@link Preferences} object
35 * @return pointer to the {@link Preferences}
36 */
37Preferences *Preferences::GetInstance() {
38 static Preferences instance;
39 return &instance;
40}
41
42/**
43 * Returns a vector of all the keys
44 * @return a vector of the keys
45 */
46std::vector<std::string> Preferences::GetKeys() { return m_table->GetKeys(); }
47
48/**
49 * Returns the string at the given key. If this table does not have a value
50 * for that position, then the given defaultValue will be returned.
51 * @param key the key
52 * @param defaultValue the value to return if none exists in the table
53 * @return either the value in the table, or the defaultValue
54 */
55std::string Preferences::GetString(llvm::StringRef key,
56 llvm::StringRef defaultValue) {
57 return m_table->GetString(key, defaultValue);
58}
59
60/**
61 * Returns the int at the given key. If this table does not have a value
62 * for that position, then the given defaultValue value will be returned.
63 * @param key the key
64 * @param defaultValue the value to return if none exists in the table
65 * @return either the value in the table, or the defaultValue
66 */
67int Preferences::GetInt(llvm::StringRef key, int defaultValue) {
68 return static_cast<int>(m_table->GetNumber(key, defaultValue));
69}
70
71/**
72 * Returns the double at the given key. If this table does not have a value
73 * for that position, then the given defaultValue value will be returned.
74 * @param key the key
75 * @param defaultValue the value to return if none exists in the table
76 * @return either the value in the table, or the defaultValue
77 */
78double Preferences::GetDouble(llvm::StringRef key, double defaultValue) {
79 return m_table->GetNumber(key, defaultValue);
80}
81
82/**
83 * Returns the float at the given key. If this table does not have a value
84 * for that position, then the given defaultValue value will be returned.
85 * @param key the key
86 * @param defaultValue the value to return if none exists in the table
87 * @return either the value in the table, or the defaultValue
88 */
89float Preferences::GetFloat(llvm::StringRef key, float defaultValue) {
90 return static_cast<float>(m_table->GetNumber(key, defaultValue));
91}
92
93/**
94 * Returns the boolean at the given key. If this table does not have a value
95 * for that position, then the given defaultValue value will be returned.
96 * @param key the key
97 * @param defaultValue the value to return if none exists in the table
98 * @return either the value in the table, or the defaultValue
99 */
100bool Preferences::GetBoolean(llvm::StringRef key, bool defaultValue) {
101 return m_table->GetBoolean(key, defaultValue);
102}
103
104/**
105 * Returns the long (int64_t) at the given key. If this table does not have a
106 * value
107 * for that position, then the given defaultValue value will be returned.
108 * @param key the key
109 * @param defaultValue the value to return if none exists in the table
110 * @return either the value in the table, or the defaultValue
111 */
112int64_t Preferences::GetLong(llvm::StringRef key, int64_t defaultValue) {
113 return static_cast<int64_t>(m_table->GetNumber(key, defaultValue));
114}
115
116/**
117 * Puts the given string into the preferences table.
118 *
119 * <p>The value may not have quotation marks, nor may the key
120 * have any whitespace nor an equals sign</p>
121 *
122 * @param key the key
123 * @param value the value
124 */
125void Preferences::PutString(llvm::StringRef key, llvm::StringRef value) {
126 m_table->PutString(key, value);
127 m_table->SetPersistent(key);
128}
129
130/**
131 * Puts the given int into the preferences table.
132 *
133 * <p>The key may not have any whitespace nor an equals sign</p>
134 *
135 * @param key the key
136 * @param value the value
137 */
138void Preferences::PutInt(llvm::StringRef key, int value) {
139 m_table->PutNumber(key, value);
140 m_table->SetPersistent(key);
141}
142
143/**
144 * Puts the given double into the preferences table.
145 *
146 * <p>The key may not have any whitespace nor an equals sign</p>
147 *
148 * @param key the key
149 * @param value the value
150 */
151void Preferences::PutDouble(llvm::StringRef key, double value) {
152 m_table->PutNumber(key, value);
153 m_table->SetPersistent(key);
154}
155
156/**
157 * Puts the given float into the preferences table.
158 *
159 * <p>The key may not have any whitespace nor an equals sign</p>
160 *
161 * @param key the key
162 * @param value the value
163 */
164void Preferences::PutFloat(llvm::StringRef key, float value) {
165 m_table->PutNumber(key, value);
166 m_table->SetPersistent(key);
167}
168
169/**
170 * Puts the given boolean into the preferences table.
171 *
172 * <p>The key may not have any whitespace nor an equals sign</p>
173 *
174 * @param key the key
175 * @param value the value
176 */
177void Preferences::PutBoolean(llvm::StringRef key, bool value) {
178 m_table->PutBoolean(key, value);
179 m_table->SetPersistent(key);
180}
181
182/**
183 * Puts the given long (int64_t) into the preferences table.
184 *
185 * <p>The key may not have any whitespace nor an equals sign</p>
186 *
187 * @param key the key
188 * @param value the value
189 */
190void Preferences::PutLong(llvm::StringRef key, int64_t value) {
191 m_table->PutNumber(key, value);
192 m_table->SetPersistent(key);
193}
194
195/**
196 * This function is no longer required, as NetworkTables automatically
197 * saves persistent values (which all Preferences values are) periodically
198 * when running as a server.
199 * @deprecated backwards compatibility shim
200 */
201void Preferences::Save() {}
202
203/**
204 * Returns whether or not there is a key with the given name.
205 * @param key the key
206 * @return if there is a value at the given key
207 */
208bool Preferences::ContainsKey(llvm::StringRef key) {
209 return m_table->ContainsKey(key);
210}
211
212/**
213 * Remove a preference
214 * @param key the key
215 */
216void Preferences::Remove(llvm::StringRef key) {
217 m_table->Delete(key);
218}