The key and value pairs config wrapper class for managing app setting.

Dependents:   dconfig_demo mbed_controller_demo

Committer:
hillkim7
Date:
Tue Feb 17 14:37:18 2015 +0000
Revision:
0:7e982de4d3f5
Child:
1:4f7c80fbd4a1
Utility c++ class for device config data managing(setting data).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hillkim7 0:7e982de4d3f5 1 /**
hillkim7 0:7e982de4d3f5 2 * @file dconfig.h
hillkim7 0:7e982de4d3f5 3 *
hillkim7 0:7e982de4d3f5 4 * @author hillkim7@gmail.com
hillkim7 0:7e982de4d3f5 5 * @brief The key and value pairs config wrapper class for managing app setting.
hillkim7 0:7e982de4d3f5 6 * Most of programs need to hold application specific setting.
hillkim7 0:7e982de4d3f5 7 * The dconfig class can provide functionality for that.
hillkim7 0:7e982de4d3f5 8 */
hillkim7 0:7e982de4d3f5 9
hillkim7 0:7e982de4d3f5 10 #pragma once
hillkim7 0:7e982de4d3f5 11
hillkim7 0:7e982de4d3f5 12 #include <string>
hillkim7 0:7e982de4d3f5 13 #include <map>
hillkim7 0:7e982de4d3f5 14 #include <stdlib.h>
hillkim7 0:7e982de4d3f5 15
hillkim7 0:7e982de4d3f5 16 /** Example:
hillkim7 0:7e982de4d3f5 17 * @code
hillkim7 0:7e982de4d3f5 18 *
hillkim7 0:7e982de4d3f5 19 * #include <stdio.h>
hillkim7 0:7e982de4d3f5 20 * #include "dconfig.h"
hillkim7 0:7e982de4d3f5 21 *
hillkim7 0:7e982de4d3f5 22 * class MyConfig : public DConfig
hillkim7 0:7e982de4d3f5 23 * {
hillkim7 0:7e982de4d3f5 24 * public:
hillkim7 0:7e982de4d3f5 25 * virtual void reset_default(void)
hillkim7 0:7e982de4d3f5 26 * {
hillkim7 0:7e982de4d3f5 27 * (*this)["tz"] = "9";
hillkim7 0:7e982de4d3f5 28 * (*this)["name"] = "my_app";
hillkim7 0:7e982de4d3f5 29 * }
hillkim7 0:7e982de4d3f5 30 * };
hillkim7 0:7e982de4d3f5 31 *
hillkim7 0:7e982de4d3f5 32 * int main(int argc, char* argv[])
hillkim7 0:7e982de4d3f5 33 * {
hillkim7 0:7e982de4d3f5 34 * MyConfig mycfg;
hillkim7 0:7e982de4d3f5 35 * std::string serialized_data;
hillkim7 0:7e982de4d3f5 36 *
hillkim7 0:7e982de4d3f5 37 * // setup initial key & value
hillkim7 0:7e982de4d3f5 38 * mycfg.reset_default();
hillkim7 0:7e982de4d3f5 39 *
hillkim7 0:7e982de4d3f5 40 * printf("name=%s\r\n", mycfg.lookup_as_cstr("name", ""));
hillkim7 0:7e982de4d3f5 41 * printf("tz=%d\r\n", mycfg.lookup_as_int("tz", 0));
hillkim7 0:7e982de4d3f5 42 * return 0;
hillkim7 0:7e982de4d3f5 43 * }
hillkim7 0:7e982de4d3f5 44
hillkim7 0:7e982de4d3f5 45 */
hillkim7 0:7e982de4d3f5 46
hillkim7 0:7e982de4d3f5 47 typedef bool (*DConfigOutput_t) (void *user_data, char c);
hillkim7 0:7e982de4d3f5 48
hillkim7 0:7e982de4d3f5 49 class DConfig : public std::map<std::string, std::string>
hillkim7 0:7e982de4d3f5 50 {
hillkim7 0:7e982de4d3f5 51 public:
hillkim7 0:7e982de4d3f5 52 /*** lookup value by key and return value as int if a key exists. */
hillkim7 0:7e982de4d3f5 53 int lookup_as_int(const std::string &key_name, int default_value) const;
hillkim7 0:7e982de4d3f5 54
hillkim7 0:7e982de4d3f5 55 /*** lookup value by key and return value as C string if a key exists. */
hillkim7 0:7e982de4d3f5 56 const char *lookup_as_cstr(const std::string &key_name, const char *default_value) const;
hillkim7 0:7e982de4d3f5 57
hillkim7 0:7e982de4d3f5 58 /*** lookup value by key */
hillkim7 0:7e982de4d3f5 59 bool lookup(const std::string &key_name, std::string &value) const;
hillkim7 0:7e982de4d3f5 60
hillkim7 0:7e982de4d3f5 61 /*** lookup value by key and return value as C string if a key exists. */
hillkim7 0:7e982de4d3f5 62 std::string lookup(const std::string &key_name) const;
hillkim7 0:7e982de4d3f5 63
hillkim7 0:7e982de4d3f5 64 /*** load key & value pairs from memory.
hillkim7 0:7e982de4d3f5 65 * Both key and value are saved in the form of C string which includes null terminated char('\0').
hillkim7 0:7e982de4d3f5 66 */
hillkim7 0:7e982de4d3f5 67 bool load_from(const char *ptr, size_t ptr_len);
hillkim7 0:7e982de4d3f5 68
hillkim7 0:7e982de4d3f5 69 /*** save all key & value pairs by calling callback function. */
hillkim7 0:7e982de4d3f5 70 bool save_to(DConfigOutput_t func, void *user_data) const;
hillkim7 0:7e982de4d3f5 71
hillkim7 0:7e982de4d3f5 72 /*** Check if a key exists. */
hillkim7 0:7e982de4d3f5 73 bool has_key(const std::string &key_name) const
hillkim7 0:7e982de4d3f5 74 {
hillkim7 0:7e982de4d3f5 75 return this->find(key_name) != this->end();
hillkim7 0:7e982de4d3f5 76 }
hillkim7 0:7e982de4d3f5 77
hillkim7 0:7e982de4d3f5 78 /*** virtual function for default setting. */
hillkim7 0:7e982de4d3f5 79 virtual void reset_default(void)
hillkim7 0:7e982de4d3f5 80 {
hillkim7 0:7e982de4d3f5 81 }
hillkim7 0:7e982de4d3f5 82 };
hillkim7 0:7e982de4d3f5 83
hillkim7 0:7e982de4d3f5 84