Debian Codegraph @

src/base/typeconversion.h

raw symbols SCIP
1 /**********************************************************************
2 typeconversion.h: class to handle type conversions
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _TYPECONVERSION_H_
22 #define _TYPECONVERSION_H_
23
24 /* #ifndef WIN32 */
25 /* #include <cxxabi.h> */
26 /* #include <typeinfo> */
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <iomanip>
31 #include <stdexcept>
32 /* #include <memory> */
33 /* #include <cstdlib> */
34 #ifndef WIN32
35 #include <cxxabi.h>
36 #define mytypeid(T) abi::__cxa_demangle(typeid(T).name(),0,0,&status)
37 #else
38 #define mytypeid(T) typeid(T).name()
39 #endif
40 /* #ifndef WIN32 */
41 /* #include <cxxabi.h> */
42 /* #include <typeinfo> */
43 /* #include <iostream> */
44 /* #include <string> */
45 /* #include <memory> */
46 /* #include <cstdlib> */
47
48 /* template<typename T> std::string mytypeid(T) */
49 /* { */
50 /* int status; */
51 /* return(abi::__cxa_demangle(typeid(T).name(),0,0,&status)); */
52 /* } */
53 /* #else */
54 /* template<typename T> std::string mytypeid(T); */
55 /* { */
56 /* return(typeid(T).name()); */
57 /* } */
58 /* #endif */
59
60 class BadConversion : public std::runtime_error {
61 public:
62 BadConversion(std::string const& s)
63 : runtime_error(s)
64 { }
65 };
66
67 ///convert command line option to value of the defined type, throw exception in case of failure
68 template<typename T> inline T string2type(std::string const& s){
69 std::istringstream i(s);
70 T x;
71 if (!(i >> x) )
72 throw BadConversion(s);
73 return x;
74 }
75
76 ///convert command line option to value of the defined type, throw if entire string could not get converted
77 template<typename T> inline T string2type(std::string const& s,bool failIfLeftoverChars){
78 std::istringstream i(s);
79 char c;
80 T x;
81 if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
82 throw BadConversion(s);
83 return x;
84 }
85
86 ///serialization for help or to dump option values to screen in verbose mode
87 template<typename T> inline std::string type2string(T const& value){
88 std::ostringstream oss;
89 oss << value;
90 return oss.str();
91 }
92
93 #endif