WPILibC++ 2024.3.2
Preferences.h
Go to the documentation of this file.
1// 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.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace frc {
14
15/**
16 * The preferences class provides a relatively simple way to save important
17 * values to the roboRIO to access the next time the roboRIO is booted.
18 *
19 * This class loads and saves from a file inside the roboRIO. The user cannot
20 * access the file directly, but may modify values at specific fields which will
21 * then be automatically periodically saved to the file by the NetworkTable
22 * server.
23 *
24 * This class is thread safe.
25 *
26 * This will also interact with NetworkTable by creating a table called
27 * "Preferences" with all the key-value pairs.
28 */
30 public:
31 /**
32 * Returns a vector of all the keys.
33 *
34 * @return a vector of the keys
35 */
36 static std::vector<std::string> GetKeys();
37
38 /**
39 * Returns the string at the given key. If this table does not have a value
40 * for that position, then the given defaultValue will be returned.
41 *
42 * @param key the key
43 * @param defaultValue the value to return if none exists in the table
44 * @return either the value in the table, or the defaultValue
45 */
46 static std::string GetString(std::string_view key,
47 std::string_view defaultValue = "");
48
49 /**
50 * Returns the int at the given key. If this table does not have a value for
51 * that position, then the given defaultValue value will be returned.
52 *
53 * @param key the key
54 * @param defaultValue the value to return if none exists in the table
55 * @return either the value in the table, or the defaultValue
56 */
57 static int GetInt(std::string_view key, int defaultValue = 0);
58
59 /**
60 * Returns the double 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 *
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 */
67 static double GetDouble(std::string_view key, double defaultValue = 0.0);
68
69 /**
70 * Returns the float at the given key. If this table does not have a value
71 * for that position, then the given defaultValue value will be returned.
72 *
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 */
77 static float GetFloat(std::string_view key, float defaultValue = 0.0);
78
79 /**
80 * Returns the boolean at the given key. If this table does not have a value
81 * for that position, then the given defaultValue value will be returned.
82 *
83 * @param key the key
84 * @param defaultValue the value to return if none exists in the table
85 * @return either the value in the table, or the defaultValue
86 */
87 static bool GetBoolean(std::string_view key, bool defaultValue = false);
88
89 /**
90 * Returns the long (int64_t) at the given key. If this table does not have a
91 * value for that position, then the given defaultValue value will be
92 * returned.
93 *
94 * @param key the key
95 * @param defaultValue the value to return if none exists in the table
96 * @return either the value in the table, or the defaultValue
97 */
98 static int64_t GetLong(std::string_view key, int64_t defaultValue = 0);
99
100 /**
101 * Puts the given string into the preferences table.
102 *
103 * The value may not have quotation marks, nor may the key have any whitespace
104 * nor an equals sign.
105 *
106 * @param key the key
107 * @param value the value
108 */
110
111 /**
112 * Puts the given string into the preferences table if it doesn't
113 * already exist.
114 */
116
117 /**
118 * Puts the given int into the preferences table.
119 *
120 * The key may not have any whitespace nor an equals sign.
121 *
122 * @param key the key
123 * @param value the value
124 */
125 static void SetInt(std::string_view key, int value);
126
127 /**
128 * Puts the given int into the preferences table if it doesn't
129 * already exist.
130 */
131 static void InitInt(std::string_view key, int value);
132
133 /**
134 * Puts the given double into the preferences table.
135 *
136 * The key may not have any whitespace nor an equals sign.
137 *
138 * @param key the key
139 * @param value the value
140 */
141 static void SetDouble(std::string_view key, double value);
142
143 /**
144 * Puts the given double into the preferences table if it doesn't
145 * already exist.
146 */
147 static void InitDouble(std::string_view key, double value);
148
149 /**
150 * Puts the given float into the preferences table.
151 *
152 * The key may not have any whitespace nor an equals sign.
153 *
154 * @param key the key
155 * @param value the value
156 */
157 static void SetFloat(std::string_view key, float value);
158
159 /**
160 * Puts the given float into the preferences table if it doesn't
161 * already exist.
162 */
163 static void InitFloat(std::string_view key, float value);
164
165 /**
166 * Puts the given boolean into the preferences table.
167 *
168 * The key may not have any whitespace nor an equals sign.
169 *
170 * @param key the key
171 * @param value the value
172 */
173 static void SetBoolean(std::string_view key, bool value);
174
175 /**
176 * Puts the given boolean into the preferences table if it doesn't
177 * already exist.
178 */
179 static void InitBoolean(std::string_view key, bool value);
180
181 /**
182 * Puts the given long (int64_t) into the preferences table.
183 *
184 * The key may not have any whitespace nor an equals sign.
185 *
186 * @param key the key
187 * @param value the value
188 */
189 static void SetLong(std::string_view key, int64_t value);
190
191 /**
192 * Puts the given long into the preferences table if it doesn't
193 * already exist.
194 */
195 static void InitLong(std::string_view key, int64_t value);
196
197 /**
198 * Returns whether or not there is a key with the given name.
199 *
200 * @param key the key
201 * @return if there is a value at the given key
202 */
204
205 /**
206 * Remove a preference.
207 *
208 * @param key the key
209 */
210 static void Remove(std::string_view key);
211
212 /**
213 * Remove all preferences.
214 */
215 static void RemoveAll();
216
217 private:
218 Preferences() = default;
219};
220
221} // namespace frc
The preferences class provides a relatively simple way to save important values to the roboRIO to acc...
Definition: Preferences.h:29
static void SetString(std::string_view key, std::string_view value)
Puts the given string into the preferences table.
static void InitFloat(std::string_view key, float value)
Puts the given float into the preferences table if it doesn't already exist.
static void SetDouble(std::string_view key, double value)
Puts the given double into the preferences table.
static void InitLong(std::string_view key, int64_t value)
Puts the given long into the preferences table if it doesn't already exist.
static void SetInt(std::string_view key, int value)
Puts the given int into the preferences table.
static void SetBoolean(std::string_view key, bool value)
Puts the given boolean into the preferences table.
static int64_t GetLong(std::string_view key, int64_t defaultValue=0)
Returns the long (int64_t) at the given key.
static void InitString(std::string_view key, std::string_view value)
Puts the given string into the preferences table if it doesn't already exist.
static void InitBoolean(std::string_view key, bool value)
Puts the given boolean into the preferences table if it doesn't already exist.
static bool ContainsKey(std::string_view key)
Returns whether or not there is a key with the given name.
static void SetFloat(std::string_view key, float value)
Puts the given float into the preferences table.
static float GetFloat(std::string_view key, float defaultValue=0.0)
Returns the float at the given key.
static std::vector< std::string > GetKeys()
Returns a vector of all the keys.
static void RemoveAll()
Remove all preferences.
static void SetLong(std::string_view key, int64_t value)
Puts the given long (int64_t) into the preferences table.
static void InitDouble(std::string_view key, double value)
Puts the given double into the preferences table if it doesn't already exist.
static int GetInt(std::string_view key, int defaultValue=0)
Returns the int at the given key.
static bool GetBoolean(std::string_view key, bool defaultValue=false)
Returns the boolean at the given key.
static void InitInt(std::string_view key, int value)
Puts the given int into the preferences table if it doesn't already exist.
static std::string GetString(std::string_view key, std::string_view defaultValue="")
Returns the string at the given key.
static double GetDouble(std::string_view key, double defaultValue=0.0)
Returns the double at the given key.
static void Remove(std::string_view key)
Remove a preference.
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15