WPILib 2012
WPILibRoboticsLibraryforFRC
|
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2011. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #ifndef __PREFERENCES_H__ 00008 #define __PREFERENCES_H__ 00009 00010 #include "ErrorBase.h" 00011 #include "NetworkTables/NetworkTableChangeListener.h" 00012 #include "Task.h" 00013 #include <map> 00014 #include <semLib.h> 00015 #include <string> 00016 #include <vector> 00017 00033 class Preferences : public ErrorBase, public NetworkTableChangeListener 00034 { 00035 public: 00036 static Preferences *GetInstance(); 00037 00038 std::vector<std::string> GetKeys(); 00039 std::string GetString(const char *key, const char *defaultValue = ""); 00040 int GetString(const char *key, char *value, int valueSize, const char *defaultValue = ""); 00041 int GetInt(const char *key, int defaultValue = 0); 00042 double GetDouble(const char *key, double defaultValue = 0.0); 00043 float GetFloat(const char *key, float defaultValue = 0.0); 00044 bool GetBoolean(const char *key, bool defaultValue = false); 00045 INT64 GetLong(const char *key, INT64 defaultValue = 0); 00046 void PutString(const char *key, const char *value); 00047 void PutInt(const char *key, int value); 00048 void PutDouble(const char *key, double value); 00049 void PutFloat(const char *key, float value); 00050 void PutBoolean(const char *key, bool value); 00051 void PutLong(const char *key, INT64 value); 00052 void Save(); 00053 bool ContainsKey(const char *key); 00054 void Remove(const char *key); 00055 00056 protected: 00057 Preferences(); 00058 virtual ~Preferences(); 00059 00060 private: 00061 std::string Get(const char *key); 00062 void Put(const char *key, std::string value); 00063 00064 void ReadTaskRun(); 00065 void WriteTaskRun(); 00066 00067 virtual void ValueChanged(NetworkTable *table, const char *name, NetworkTables_Types type); 00068 virtual void ValueConfirmed(NetworkTable *table, const char *name, NetworkTables_Types type); 00069 00070 static int InitReadTask(Preferences *obj) {obj->ReadTaskRun();return 0;} 00071 static int InitWriteTask(Preferences *obj) {obj->WriteTaskRun();return 0;} 00072 00073 static Preferences *_instance; 00074 00076 SEM_ID m_fileLock; 00078 SEM_ID m_fileOpStarted; 00080 SEM_ID m_tableLock; 00081 typedef std::map<std::string, std::string> StringMap; 00083 StringMap m_values; 00085 std::vector<std::string> m_keys; 00087 StringMap m_comments; 00089 std::string m_endComment; 00090 Task m_readTask; 00091 Task m_writeTask; 00092 }; 00093 00094 #endif