WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Preferences.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2011-2017. 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 "ErrorBase.h"
17 #include "networktables/NetworkTable.h"
18 #include "tables/ITableListener.h"
19 
20 namespace frc {
21 
36 class Preferences : public ErrorBase {
37  public:
38  static Preferences* GetInstance();
39 
40  std::vector<std::string> GetKeys();
41  std::string GetString(llvm::StringRef key, llvm::StringRef defaultValue = "");
42  int GetInt(llvm::StringRef key, int defaultValue = 0);
43  double GetDouble(llvm::StringRef key, double defaultValue = 0.0);
44  float GetFloat(llvm::StringRef key, float defaultValue = 0.0);
45  bool GetBoolean(llvm::StringRef key, bool defaultValue = false);
46  int64_t GetLong(llvm::StringRef key, int64_t defaultValue = 0);
47  void PutString(llvm::StringRef key, llvm::StringRef value);
48  void PutInt(llvm::StringRef key, int value);
49  void PutDouble(llvm::StringRef key, double value);
50  void PutFloat(llvm::StringRef key, float value);
51  void PutBoolean(llvm::StringRef key, bool value);
52  void PutLong(llvm::StringRef key, int64_t value);
53  WPI_DEPRECATED(
54  "Saving is now automatically performed by the NetworkTables server.")
55  void Save();
56  bool ContainsKey(llvm::StringRef key);
57  void Remove(llvm::StringRef key);
58 
59  protected:
60  Preferences();
61  virtual ~Preferences() = default;
62 
63  private:
64  std::shared_ptr<ITable> m_table;
65  class Listener : public ITableListener {
66  public:
67  void ValueChanged(ITable* source, llvm::StringRef key,
68  std::shared_ptr<nt::Value> value, bool isNew) override;
69  void ValueChangedEx(ITable* source, llvm::StringRef key,
70  std::shared_ptr<nt::Value> value,
71  uint32_t flags) override;
72  };
73  Listener m_listener;
74 };
75 
76 } // namespace frc
int64_t GetLong(llvm::StringRef key, int64_t defaultValue=0)
Returns the long (int64_t) at the given key.
Definition: Preferences.cpp:121
bool GetBoolean(llvm::StringRef key, bool defaultValue=false)
Returns the boolean at the given key.
Definition: Preferences.cpp:109
std::vector< std::string > GetKeys()
Returns a vector of all the keys.
Definition: Preferences.cpp:50
The preferences class provides a relatively simple way to save important values to the roboRIO to acc...
Definition: Preferences.h:36
A table whose values can be read and written to.
Definition: ITable.h:22
void PutFloat(llvm::StringRef key, float value)
Puts the given float into the preferences table.
Definition: Preferences.cpp:173
static Preferences * GetInstance()
Get the one and only Preferences object.
Definition: Preferences.cpp:40
std::string GetString(llvm::StringRef key, llvm::StringRef defaultValue="")
Returns the string at the given key.
Definition: Preferences.cpp:60
void PutBoolean(llvm::StringRef key, bool value)
Puts the given boolean into the preferences table.
Definition: Preferences.cpp:186
float GetFloat(llvm::StringRef key, float defaultValue=0.0)
Returns the float at the given key.
Definition: Preferences.cpp:97
void PutString(llvm::StringRef key, llvm::StringRef value)
Puts the given string into the preferences table.
Definition: Preferences.cpp:134
double GetDouble(llvm::StringRef key, double defaultValue=0.0)
Returns the double at the given key.
Definition: Preferences.cpp:85
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
void PutLong(llvm::StringRef key, int64_t value)
Puts the given long (int64_t) into the preferences table.
Definition: Preferences.cpp:199
bool ContainsKey(llvm::StringRef key)
Returns whether or not there is a key with the given name.
Definition: Preferences.cpp:218
void Remove(llvm::StringRef key)
Remove a preference.
Definition: Preferences.cpp:227
Base class for most objects.
Definition: ErrorBase.h:72
int GetInt(llvm::StringRef key, int defaultValue=0)
Returns the int at the given key.
Definition: Preferences.cpp:73
void PutDouble(llvm::StringRef key, double value)
Puts the given double into the preferences table.
Definition: Preferences.cpp:160
void PutInt(llvm::StringRef key, int value)
Puts the given int into the preferences table.
Definition: Preferences.cpp:147