icedb  version 0.5.1
Snow particle scattering database API
options.cpp
Go to the documentation of this file.
1 #include <boost/lexical_cast.hpp>
2 #include <map>
3 #include <complex>
4 #include "../icedb/defs.h"
5 #include "../private/options.hpp"
6 #include "../icedb/logging.hpp"
7 #include "../icedb/error.hpp"
8 
9 namespace icedb {
10  namespace registry {
11  class options_inner {
12  public:
13  std::map<std::string, std::string> _mapStr;
14  };
16  p = std::shared_ptr<options_inner>(new options_inner);
17  }
18  std::shared_ptr<options> options::generate() {
19  auto res = std::shared_ptr<options>(new options); return res;
20  }
22  void options::enumVals(std::ostream &out) const {
23  //out << "\tName\t\tValue" << std::endl;
24  for (const auto &v : p->_mapStr)
25  {
26  if (v.first != "password")
27  {
28  out << "\t" << v.first << ":\t" << v.second << std::endl;
29  }
30  else {
31  out << "\t" << v.first << ":\t" << "********" << std::endl;
32  }
33  }
34  }
35  bool options::hasVal(const std::string &key) const {
36  if (p->_mapStr.count(key)) return true;
37  return false;
38  }
39  template <class T> T options::getVal(const std::string &key) const
40  {
41  if (!hasVal(key)) return boost::lexical_cast<T>(std::string(""));
42  //RDthrow(Ryan_Debug::ICEDB_LOG_ERROR::xMissingKey())
43  // << Ryan_Debug::ICEDB_LOG_ERROR::key(key);
44  std::string valS = p->_mapStr.at(key);
45  T res = boost::lexical_cast<T>(valS);
46  return res;
47  }
48  template <class T> T options::getVal(const std::string &key, const T& defaultval) const
49  {
50  if (!hasVal(key)) return defaultval;
51  return getVal<T>(key);
52  }
53  template <class T> options_ptr options::setVal(const std::string &key, const T &value)
54  {
55  std::string valS = boost::lexical_cast<std::string>(value);
56  p->_mapStr[key] = valS;
57  return this->shared_from_this();
58  }
59  template <class T> options_ptr options::add(const std::string &key, const T &value)
60  {
62  .add<std::string>("key", key)
63  .add<T>("newValue", value);
64  return this->setVal<T>(key, value);
65  }
66 
67 #define DOTYPES(f) f(int); f(float); f(double); f(long); f(long long); \
68  f(unsigned int); f(unsigned long); f(unsigned long long); f(std::string); f(bool); f(std::complex<double>);
69 
70 #define IMPL_OPTS_SETVAL(T) template options_ptr options::setVal<T>(const std::string&, const T&);
71 #define IMPL_OPTS_ADD(T) template options_ptr options::add<T>(const std::string&, const T&);
72 #define IMPL_OPTS_GETVAL_A(T) template T options::getVal<T>(const std::string&) const;
73 #define IMPL_OPTS_GETVAL_B(T) template T options::getVal<T>(const std::string&, const T&) const;
74 #define IMPL_OPTS(T) IMPL_OPTS_SETVAL(T); IMPL_OPTS_GETVAL_A(T);IMPL_OPTS_GETVAL_B(T);IMPL_OPTS_ADD(T);
76 
77  }
78 
79 }
void enumVals(std::ostream &out) const
Definition: options.cpp:22
options_ptr setVal(const std::string &key, const T &value)
Adds or replaces an option.
Definition: options.cpp:53
bool hasVal(const std::string &key) const
Definition: options.cpp:35
static std::shared_ptr< options > generate()
Definition: options.cpp:18
T getVal(const std::string &key) const
Retrieves an option. Throws if nonexistant.
Definition: options.cpp:39
#define IMPL_OPTS(T)
Definition: options.cpp:74
#define ICEDB_throw(x)
Definition: error.hpp:88
options_ptr add(const std::string &key, const T &value)
Adds an option. Throws if the same name already exists.
Definition: options.cpp:59
std::shared_ptr< options > options_ptr
std::map< std::string, std::string > _mapStr
Definition: options.cpp:13
DOTYPES(IMPL_OPTS)