jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 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 |
|
| 19 | /**
|
| 20 | * The preferences class provides a relatively simple way to save important values to
|
| 21 | * the cRIO to access the next time the cRIO is booted.
|
| 22 | *
|
| 23 | * <p>This class loads and saves from a file
|
| 24 | * inside the cRIO. The user can not access the file directly, but may modify values at specific
|
| 25 | * fields which will then be saved to the file when {@link Preferences#Save() Save()} is called.</p>
|
| 26 | *
|
| 27 | * <p>This class is thread safe.</p>
|
| 28 | *
|
| 29 | * <p>This will also interact with {@link NetworkTable} by creating a table called "Preferences" with all the
|
| 30 | * key-value pairs. To save using {@link NetworkTable}, simply set the boolean at position "~S A V E~" to true.
|
| 31 | * Also, if the value of any variable is " in the {@link NetworkTable}, then that represents non-existence in the
|
| 32 | * {@link Preferences} table</p>
|
| 33 | */
|
| 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 |
|
| 75 | /** The semaphore for accessing the file */
|
| 76 | SEM_ID m_fileLock;
|
| 77 | /** The semaphore for beginning reads and writes to the file */
|
| 78 | SEM_ID m_fileOpStarted;
|
| 79 | /** The semaphore for reading from the table */
|
| 80 | SEM_ID m_tableLock;
|
| 81 | typedef std::map<std::string, std::string> StringMap;
|
| 82 | /** The actual values (String->String) */
|
| 83 | StringMap m_values;
|
| 84 | /** The keys in the order they were read from the file */
|
| 85 | std::vector<std::string> m_keys;
|
| 86 | /** The comments that were in the file sorted by which key they appeared over (String->Comment) */
|
| 87 | StringMap m_comments;
|
| 88 | /** The comment at the end of the file */
|
| 89 | std::string m_endComment;
|
| 90 | Task m_readTask;
|
| 91 | Task m_writeTask;
|
| 92 | };
|
| 93 |
|
| 94 | #endif
|