OpenPolygon  1.0.0
OpenPolygon is a Rendering Engine
configmanager.h
1 /*
2  Copyright (C) 2014 - 2016 Mutzii
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
18 #ifndef CONFIGMANAGER_H
19 #define CONFIGMANAGER_H
20 
21 #include "index.h"
22 #include "eutils.h"
23 
24 namespace Engine
25 {
26 
27  template< class InConfig >
28  struct Callback
29  {
30  InConfig * object;
31  void (InConfig::*callback)( const std::string & file_line_input );
32  };
33 
34 
35  template< class InConfig >
37  {
38 
39  using InConfigMap = std::map< string , Callback< InConfig > * >;
40 
41  public:
42 
43  friend class ConfigManager;
44 
45  explicit InstanceConfig ( const std::string & file_name )
46  : mFileName( file_name ){}
47 
48  ~InstanceConfig(){}
49 
50  void load(void)
51  {
52  std::ifstream file;
53  std::string line;
54  std::string value;
55  std::string content;
56 
57  file.open(( mFileName ).c_str());
58 
59  if(file.is_open())
60  {
61  while(file.good())
62  {
63  getline(file, line);
64 
65  if( line.c_str()[0] == '#' )
66  continue;
67 
68  Strings items = Utils::StringSplit( line , "=" );
69 
70  if( items.size() >= 2 )
71  {
72  value = items[0];
73  content = items[1];
74 
75  //std::pair< const string , Callback<InConfig> * > pair = * mConfigMap.find( value );
76 
77  for( auto pair : mConfigMap )
78  {
79  if( pair.first == value )
80  {
81  //Get custom function
82  Callback<InConfig> * call = pair.second;
83 
84  //call custom function
85  ((call->object)->* (call->callback))( content );
86  }
87  }
88  }
89 
90  }// #WHILE
91  }// #FILE_OPEN
92 
93  // Clean up
94  free();
95  }
96 
97  void addFunction( const std::string & value , Callback< InConfig > * callback )
98  {
99  std::pair< const string , Callback<InConfig> * > pair ( value , callback );
100 
101  mConfigMap.insert( pair );
102  }
103 
104  struct Callback<InConfig> * callback( InConfig * object , void (InConfig::*callback)( const std::string & file_line_input ))
105  {
106  struct Callback<InConfig> * sCallback = new struct Callback<InConfig>();
107  sCallback->object = object;
108  sCallback->callback = callback;
109  return sCallback;
110  }
111 
112  void free(void)
113  {
114  for( auto pair : mConfigMap )
115  {
116  Callback<InConfig> * call = pair.second;
117  delete call;
118  }
119 
120  mConfigMap.clear();
121  }
122 
123 
124  protected:
125 
126  std::string mFileName;
127 
128  InConfigMap mConfigMap;
129  };
130 
131 
132 
133  //das hier...
134  typedef void(*CONFIG_FUNC)( const std::string & file_line_input );
135 
136 
137 
138 
139  using ConfigMap = std::map< std::string , CONFIG_FUNC >;
140 
141  class Config
142  {
143  public:
144 
145  friend class ConfigManager;
146 
147  explicit Config( const std::string & file_name );
148 
149  virtual ~Config(){}
150 
151  virtual void load(void);
152 
153  void addFunction( const std::string & value , CONFIG_FUNC func );
154 
155 
156  protected:
157 
158  std::string mFileName;
159 
160  ConfigMap mConfigMap;
161  };
162 
163  using ConfigList = std::list< Config *>;
164 
166  {
167  private:
168 
169  static ConfigManager * ptrInstance;
170 
171  ConfigManager();
172  ConfigManager( const ConfigManager&){}
173  void operator =( const ConfigManager&){}
174 
175  public:
176 
177  static ConfigManager * getSingeltonPtr(void);
178 
179  Config * createConfig( const std::string & file_name );
180 
181  //Add Function and Read (Load) File
182  void addFunction( Config * config , const std::string & value , CONFIG_FUNC func );
183  void loadConfig( Config * config );
184 
185  //Get
186  Config * getConfig( const std::string & file_name );
187 
188  //Memory Management
189  void saveConfig( Config * config );
190  void removeConfig( Config * config );
191  void removeAll(void);
192 
193  private:
194 
195  ConfigList mConfigList;
196 
197  };
198 
199 }
200 #endif // CONFIGMANAGER_H
Definition: configmanager.h:141
Definition: element.h:23
Definition: configmanager.h:165
Definition: configmanager.h:28
Definition: configmanager.h:36