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