xerus
a general purpose tensor library
stringFromTo.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus 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 Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include <string>
28 #include <sstream>
29 #include <iomanip>
30 #include <utility>
31 
32 #include "standard.h"
33 #include "sfinae.h"
34 #include "containerOutput.h"
35 
36 namespace xerus {
37  namespace misc {
38  // Enable all standard to string functions also in xerus misc namespace
39  using std::to_string;
40 
41  static XERUS_force_inline std::string to_string(const bool obj) {
42  return obj ? std::string("TRUE") : std::string("FALSE");
43  }
44 
45  static XERUS_force_inline std::string to_string(const std::string& obj) {
46  return obj;
47  }
48 
49  static XERUS_force_inline std::string to_string(const char * const obj) {
50  return std::string(obj);
51  }
52 
53  #if __GNUC__ > 4 || defined(__clang__)
54 
55  XERUS_GENERATE_EXISTS_FUNCTION(to_string)
56 
57 
58  template<typename T, typename std::enable_if<!sfinae::exists_to_string<T>::value, int>::type = 0>
59  std::string to_string(const T& obj) {
60  std::ostringstream stream;
61  stream << obj;
62  return stream.str();
63  }
64 
65  #else
66 
68  template<typename T>
69  std::string to_string(const T& obj) {
70  std::ostringstream stream;
71  stream << obj;
72  return stream.str();
73  }
74 
75  #endif
76 
77 
79  template<typename T>
80  std::string to_string(const T& obj, const long _precision) {
81  std::ostringstream stream;
82  stream.precision(_precision);
83  stream << std::fixed << obj;
84  return stream.str();
85  }
86 
87 
88 
89 
90 
92  template<typename T>
93  T from_string(const std::string& _s){
94  std::stringstream stream(_s);
95  T tmp;
96  stream >> tmp;
97  return tmp;
98  }
99 
100  template<>
101  XERUS_force_inline int from_string<int>(const std::string& _str) {
102  return stoi(_str);
103  }
104 
105  template<>
106  XERUS_force_inline long from_string<long>(const std::string& _str) {
107  return stol(_str);
108  }
109 
110  template<>
111  XERUS_force_inline long long from_string<long long>(const std::string& _str) {
112  return stoll(_str);
113  }
114 
115  template<>
116  XERUS_force_inline unsigned long from_string<unsigned long>(const std::string& _str) {
117  return stoul(_str);
118  }
119 
120  template<>
121  XERUS_force_inline unsigned long long from_string<unsigned long long>(const std::string& _str) {
122  return stoull(_str);
123  }
124 
125  template<>
126  XERUS_force_inline float from_string<float>(const std::string& _str) {
127  return stof(_str);
128  }
129 
130  template<>
131  XERUS_force_inline double from_string<double>(const std::string& _str) {
132  return stod(_str);
133  }
134 
135  template<>
136  XERUS_force_inline long double from_string<long double>(const std::string& _str) {
137  return stold(_str);
138  }
139  }
140 }
XERUS_force_inline double from_string< double >(const std::string &_str)
Definition: stringFromTo.h:131
XERUS_force_inline unsigned long long from_string< unsigned long long >(const std::string &_str)
Definition: stringFromTo.h:121
The main namespace of xerus.
Definition: basic.h:37
XERUS_force_inline float from_string< float >(const std::string &_str)
Definition: stringFromTo.h:126
T from_string(const std::string &_s)
: Creates an arbitary Object from string
Definition: stringFromTo.h:93
XERUS_force_inline unsigned long from_string< unsigned long >(const std::string &_str)
Definition: stringFromTo.h:116
Header file for macros that encapsulate SFINAE functionality.
std::string to_string(const T &obj, const long _precision)
: Converts an arbitary Object to string with fixed precision
Definition: stringFromTo.h:80
Header file for the standard container to standard ostream operators.
XERUS_force_inline int from_string< int >(const std::string &_str)
Definition: stringFromTo.h:101
XERUS_force_inline long double from_string< long double >(const std::string &_str)
Definition: stringFromTo.h:136
static XERUS_force_inline std::string to_string(const bool obj)
Definition: stringFromTo.h:41
XERUS_force_inline long long from_string< long long >(const std::string &_str)
Definition: stringFromTo.h:111
XERUS_force_inline long from_string< long >(const std::string &_str)
Definition: stringFromTo.h:106
#define XERUS_force_inline
Collection of attributes to force gcc to inline a specific function.
Definition: standard.h:75
Header file for global shorthand notations of elementary integer types and attribute lists...