WPILib  2014.0
WPIRoboticsLibraryforFRC
 All Classes Functions Variables Pages
Preferences.h
1 /*----------------------------------------------------------------------------*/
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 #ifndef __PREFERENCES_H__
8 #define __PREFERENCES_H__
9 
10 #include "ErrorBase.h"
11 #include "Task.h"
12 #include <map>
13 #include <semLib.h>
14 #include <string>
15 #include <vector>
16 #include "tables/ITableListener.h"
17 #include "networktables/NetworkTable.h"
18 
34 class Preferences : public ErrorBase, public ITableListener
35 {
36 public:
37  static Preferences *GetInstance();
38 
39  std::vector<std::string> GetKeys();
40  std::string GetString(const char *key, const char *defaultValue = "");
41  int GetString(const char *key, char *value, int valueSize, const char *defaultValue = "");
42  int GetInt(const char *key, int defaultValue = 0);
43  double GetDouble(const char *key, double defaultValue = 0.0);
44  float GetFloat(const char *key, float defaultValue = 0.0);
45  bool GetBoolean(const char *key, bool defaultValue = false);
46  INT64 GetLong(const char *key, INT64 defaultValue = 0);
47  void PutString(const char *key, const char *value);
48  void PutInt(const char *key, int value);
49  void PutDouble(const char *key, double value);
50  void PutFloat(const char *key, float value);
51  void PutBoolean(const char *key, bool value);
52  void PutLong(const char *key, INT64 value);
53  void Save();
54  bool ContainsKey(const char *key);
55  void Remove(const char *key);
56 
57  void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
58 
59 protected:
60  Preferences();
61  virtual ~Preferences();
62 
63 private:
64  std::string Get(const char *key);
65  void Put(const char *key, std::string value);
66 
67  void ReadTaskRun();
68  void WriteTaskRun();
69 
70  static int InitReadTask(Preferences *obj) {obj->ReadTaskRun();return 0;}
71  static int InitWriteTask(Preferences *obj) {obj->WriteTaskRun();return 0;}
72 
73  static Preferences *_instance;
74 
76  SEM_ID m_fileLock;
78  SEM_ID m_fileOpStarted;
80  SEM_ID m_tableLock;
81  typedef std::map<std::string, std::string> StringMap;
83  StringMap m_values;
85  std::vector<std::string> m_keys;
87  StringMap m_comments;
89  std::string m_endComment;
90  Task m_readTask;
91  Task m_writeTask;
92 };
93 
94 #endif
std::vector< std::string > GetKeys()
Definition: Preferences.cpp:74
void Save()
Definition: Preferences.cpp:324
bool GetBoolean(const char *key, bool defaultValue=false)
Definition: Preferences.cpp:163
INT64 GetLong(const char *key, INT64 defaultValue=0)
Definition: Preferences.cpp:185
void PutBoolean(const char *key, bool value)
Definition: Preferences.cpp:290
std::string GetString(const char *key, const char *defaultValue="")
Definition: Preferences.cpp:86
void Remove(const char *key)
Definition: Preferences.cpp:345
void PutDouble(const char *key, double value)
Definition: Preferences.cpp:254
Definition: ErrorBase.h:37
float GetFloat(const char *key, float defaultValue=0.0)
Definition: Preferences.cpp:147
void PutInt(const char *key, int value)
Definition: Preferences.cpp:236
void PutLong(const char *key, INT64 value)
Definition: Preferences.cpp:306
void PutString(const char *key, const char *value)
Definition: Preferences.cpp:210
void PutFloat(const char *key, float value)
Definition: Preferences.cpp:272
double GetDouble(const char *key, double defaultValue=0.0)
Definition: Preferences.cpp:131
static Preferences * GetInstance()
Definition: Preferences.cpp:63
int GetInt(const char *key, int defaultValue=0)
Definition: Preferences.cpp:115
bool ContainsKey(const char *key)
Definition: Preferences.cpp:336
Definition: Preferences.h:34
Definition: Task.h:17