Labyrinth
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
configuration.hpp
1 //////////////////////////////
2 //
3 // File: lxs/system/configuration.hpp
4 // Author: Maciej Kaminski
5 // E-mail: maciej.a.kaminski@gmail.com
6 // Website: maciejkaminski.pl
7 //
8 // File creation date: Nov 27 2012
9 // This file is part of Project Labyrinth
10 // This file is going to be released under terms of GNU GPL in near future
11 //
12 //////////////////////////////
13 
14 #ifndef __HLP_CONFIG_
15 #define __HLP_CONFIG_
16 #include <string>
17 #include <map>
18 #include "lxs/system/global_config_definitions.hpp"
19 #include "lxs/system/servicethread.hpp"
20 namespace LxS
21 {
22  namespace System
23  {
24 
25  class MyMutex;
26  //! @brief Name-Type-Value database of configuration settings
27  //!
28  //! as it is a global one-instance class, it's access is simplified by following macros:
29  //! - GET_CONFIG_STRING(c)
30  //! - GET_CONFIG_FLOAT(c)
31  //! - GET_CONFIG_BOOL(c)
32  //! - SET_CONFIG_STRING(c,v)
33  //! - SET_CONFIG_FLOAT(c,v)
34  //! - SET_CONFIG_BOOL(c,v)
35  //! - SAVE_CONFIG()
37  {
38  Configuration * global_instance;
39  typedef std::map <std::string, bool> BoolsMap;
40  BoolsMap bools;
41  BoolsMap edited;
42  typedef std::map <std::string, float> FloatsMap;
43  FloatsMap floats;
44  typedef std::map <std::string, std::string> StringsMap;
45  StringsMap strings;
46  MyMutex mutex;
47  bool initialized;
48  // read single line from config file;
49  // return 0 upon success, nonzero upon fail
50  int ReadConfigLine(char * line);
51 
52  void ReadDefaultConfig(); // read readonly config from assets
53  void ReadCurrentConfig(); // read any alterations to the default config
54 
55  void Initialize();
56  ~Configuration();
57  Configuration();
58  public:
59  //! save configuration
60  void Save();
61  //! use - GET_CONFIG_STRING(c) instead
62  const std::string& GetString(const std::string& name);
63  //! use - GET_CONFIG_STRING(c) instead
64  void SetString(const std::string& name,std::string value);
65  //! use - GET_CONFIG_FLOAT(c) instead
66  float GetFloat(const std::string& name);
67  //! use - SET_CONFIG_FLOAT(c,v) instead
68  void SetFloat(const std::string& name, float value);
69  //! use - GET_CONFIG_BOOL(c) instead
70  bool GetBool(const std::string& name);
71  //! use - SET_CONFIG_BOOL(c,v) instead
72  void SetBool(const std::string& name, bool value);
73  //! return singular global instance
74  static Configuration * GetInstance();
75  };
76  //end of Configuration class
77  }
78 }
79 // shorthand for common call
80 //! return configuration value that is known to be a string
81 #define GET_CONFIG_STRING(c) (::LxS::System::Configuration::GetInstance()->GetString(c))
82 //! return configuration value that is known to be a float
83 #define GET_CONFIG_FLOAT(c) (::LxS::System::Configuration::GetInstance()->GetFloat(c))
84 //! return configuration value that is known to be a bool
85 #define GET_CONFIG_BOOL(c) (::LxS::System::Configuration::GetInstance()->GetBool(c))
86 //! store a string configuration value (not implemented)
87 #define SET_CONFIG_STRING(c,v) (::LxS::System::Configuration::GetInstance()->SetString((c),(v)))
88 //! save a float configuration value
89 #define SET_CONFIG_FLOAT(c,v) (::LxS::System::Configuration::GetInstance()->SetFloat((c),(v)))
90 //! save a bool configuration value
91 #define SET_CONFIG_BOOL(c,v) (::LxS::System::Configuration::GetInstance()->SetBool((c),(v)))
92 //! store all the config
93 #define SAVE_CONFIG() (::LxS::System::Configuration::GetInstance()->Save())
94 
95 #endif