Swarm-NG  1.1
config.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
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. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
24 #pragma once
25 #include <stdexcept>
26 
27 namespace swarm {
28 
29 
31 struct key_not_found: std::runtime_error {
32  key_not_found(const std::string &key): std::runtime_error("Key not found: \"" + key + "\"") {}
33 };
34 
44 class config : public std::map<std::string, std::string> {
45 public:
46 
47  config(){}
48 
59  template< class T >
60  config(const T& key_value_pairs){
61  initialize(key_value_pairs, sizeof(T)/sizeof(char*[2]));
62  }
63 
64 
65 
72  void initialize(const char* const key_value_pairs[][2], const int n);
73 
87  static config load(const std::string &fn,config cfg = config() );
88 
90  static bool valid_value(const std::string& v, std::string) {
91  return v.size() > 0;
92  }
94  static std::string parse(const std::string& v, std::string) {
95  return v;
96  }
97 
99  static bool valid_value(const std::string& v, double) {
100  double d = 0;
101  int r = sscanf(v.c_str(),"%lg", &d);
102  return r == 1;
103  }
105  static double parse(const std::string& v, double) {
106  double d = 0;
107  int r = sscanf(v.c_str(),"%lg", &d);
108  return d;
109  }
110 
112  static bool valid_value(const std::string& v, float) {
113  float d = 0;
114  int r = sscanf(v.c_str(),"%g", &d);
115  return r == 1;
116  }
118  static float parse(const std::string& v, float) {
119  float d = 0;
120  int r = sscanf(v.c_str(),"%g", &d);
121  return d;
122  }
123 
125  static bool valid_value(const std::string& v, int) {
126  int d = 0;
127  int r = sscanf(v.c_str(),"%i", &d);
128  return r == 1;
129  }
131  static int parse(const std::string& v, int) {
132  int d = 0;
133  int r = sscanf(v.c_str(),"%i", &d);
134  return d;
135  }
136 
138  bool valid(const std::string& key)const{
139  return count(key) && (at(key).size() > 0);
140  }
141 
148  template<typename T> bool valid(const std::string& key , T t )const{
149  return valid(key) && valid_value(at(key),t);
150  }
151 
159  template<typename T> T optional(const std::string& key , const T& default_value)const{
160  if( count(key) ) {
161  std::string value = at(key);
162  return valid_value(value,T()) ? parse(value,T()) : default_value;
163  } else
164  return default_value;
165  }
166 
181  template<typename T> T require(const std::string& key, const T& = T() )const{
182  if( count(key) && valid_value(at(key),T()) )
183  return parse(at(key),T());
184  else
185  throw key_not_found(key);
186  }
187 };
188 
189 }
190 
191