WPILibC++  2020.2.2-33-g65eab93
Preferences.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2011-2018 FIRST. 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 the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include <networktables/NetworkTable.h>
17 
18 #include "frc/ErrorBase.h"
19 
20 namespace frc {
21 
36 class Preferences : public ErrorBase {
37  public:
43  static Preferences* GetInstance();
44 
50  std::vector<std::string> GetKeys();
51 
60  std::string GetString(wpi::StringRef key, wpi::StringRef defaultValue = "");
61 
70  int GetInt(wpi::StringRef key, int defaultValue = 0);
71 
80  double GetDouble(wpi::StringRef key, double defaultValue = 0.0);
81 
90  float GetFloat(wpi::StringRef key, float defaultValue = 0.0);
91 
100  bool GetBoolean(wpi::StringRef key, bool defaultValue = false);
101 
111  int64_t GetLong(wpi::StringRef key, int64_t defaultValue = 0);
112 
122  void PutString(wpi::StringRef key, wpi::StringRef value);
123 
132  void PutInt(wpi::StringRef key, int value);
133 
142  void PutDouble(wpi::StringRef key, double value);
143 
152  void PutFloat(wpi::StringRef key, float value);
153 
162  void PutBoolean(wpi::StringRef key, bool value);
163 
172  void PutLong(wpi::StringRef key, int64_t value);
173 
180  bool ContainsKey(wpi::StringRef key);
181 
187  void Remove(wpi::StringRef key);
188 
192  void RemoveAll();
193 
194  protected:
195  Preferences();
196  virtual ~Preferences() = default;
197 
198  Preferences(Preferences&&) = default;
199  Preferences& operator=(Preferences&&) = default;
200 
201  private:
202  std::shared_ptr<nt::NetworkTable> m_table;
203  NT_EntryListener m_listener;
204 };
205 
206 } // namespace frc
frc::Preferences::PutBoolean
void PutBoolean(wpi::StringRef key, bool value)
Puts the given boolean into the preferences table.
frc::Preferences::ContainsKey
bool ContainsKey(wpi::StringRef key)
Returns whether or not there is a key with the given name.
frc::Preferences::GetDouble
double GetDouble(wpi::StringRef key, double defaultValue=0.0)
Returns the double at the given key.
frc::Preferences::GetLong
int64_t GetLong(wpi::StringRef key, int64_t defaultValue=0)
Returns the long (int64_t) at the given key.
frc::Preferences::PutFloat
void PutFloat(wpi::StringRef key, float value)
Puts the given float into the preferences table.
frc::Preferences::Remove
void Remove(wpi::StringRef key)
Remove a preference.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
wpi::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
frc::Preferences::PutDouble
void PutDouble(wpi::StringRef key, double value)
Puts the given double into the preferences table.
frc::Preferences::GetKeys
std::vector< std::string > GetKeys()
Returns a vector of all the keys.
frc::Preferences::GetBoolean
bool GetBoolean(wpi::StringRef key, bool defaultValue=false)
Returns the boolean at the given key.
frc::Preferences::RemoveAll
void RemoveAll()
Remove all preferences.
frc::Preferences::PutLong
void PutLong(wpi::StringRef key, int64_t value)
Puts the given long (int64_t) into the preferences table.
frc::Preferences::PutInt
void PutInt(wpi::StringRef key, int value)
Puts the given int into the preferences table.
frc::Preferences::GetFloat
float GetFloat(wpi::StringRef key, float defaultValue=0.0)
Returns the float at the given key.
frc::Preferences::GetInstance
static Preferences * GetInstance()
Get the one and only Preferences object.
frc
WPILib FRC namespace.
Definition: AnalogOutSim.h:16
frc::Preferences::GetInt
int GetInt(wpi::StringRef key, int defaultValue=0)
Returns the int at the given key.
frc::Preferences::GetString
std::string GetString(wpi::StringRef key, wpi::StringRef defaultValue="")
Returns the string at the given key.
frc::Preferences
The preferences class provides a relatively simple way to save important values to the roboRIO to acc...
Definition: Preferences.h:36
frc::Preferences::PutString
void PutString(wpi::StringRef key, wpi::StringRef value)
Puts the given string into the preferences table.