Util.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
24 //----------------------------------------------------------------------
25 #ifndef ICL_CORE_CONFIG_UTIL_H_INCLUDED
26 #define ICL_CORE_CONFIG_UTIL_H_INCLUDED
27 
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31 #include <stdexcept>
32 #include <boost/algorithm/string.hpp>
33 
34 namespace icl_core {
35 namespace config {
37 namespace impl {
38 
42 template <typename T, typename U>
43 T hexical_cast(U input)
44 {
45  std::stringstream interpreter;
46  interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
47  interpreter << input;
48  T result;
49  interpreter >> result;
50  return result;
51 }
52 
59 template <typename U>
60 bool bool_cast(U input)
61 {
62  std::stringstream interpreter;
63  interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
64  interpreter << input;
65  std::string result;
66  interpreter >> result;
67  boost::algorithm::to_lower(result);
68  if (result == "1" || result == "yes" || result == "true")
69  {
70  return true;
71  }
72  else
73  {
74  return false;
75  }
76 }
77 
86 template <typename U>
87 bool strict_bool_cast(U input)
88 {
89  std::stringstream interpreter;
90  interpreter.setf(std::ios::fmtflags(), std::ios::basefield);
91  interpreter << input;
92  std::string result;
93  interpreter >> result;
94  boost::algorithm::to_lower(result);
95  if (result == "1" || result == "yes" || result == "true")
96  {
97  return true;
98  }
99  else if (result == "0" || result == "no" || result == "false")
100  {
101  return false;
102  }
103  else
104  {
105  throw std::invalid_argument("Not a boolean value: " + result);
106  }
107 }
108 
109 }
110 }
111 }
112 
113 #endif
T hexical_cast(U input)
Definition: Util.h:43
bool strict_bool_cast(U input)
Definition: Util.h:87
bool bool_cast(U input)
Definition: Util.h:60


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58