tools.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_tools_hh
22 #define mia_core_tools_hh
23 
24 #include <sstream>
25 #include <string>
26 #include <boost/call_traits.hpp>
27 #include <memory>
28 #include <cmath>
29 #include <mia/core/defines.hh>
30 #include <mia/core/errormacro.hh>
31 
33 
41 template <typename T>
43  virtual void operator () (T *) {
44  }
45 };
46 
47 
57 template <typename Data>
59 public:
60  typedef std::shared_ptr<Data> PData;
61  PData operator () (Data& d) const {
62  return PData(&d, void_destructor<Data>());
63  }
64 };
65 
66 
77 template <typename T>
78 bool from_string(const char *s, T& result)
79 {
80  std::istringstream sx(s);
81  sx >> result;
82  if (sx.fail())
83  return false;
84  if (sx.eof())
85  return true;
86 
87  std::string remaining;
88  sx >> remaining;
89  bool retval = true;
90  for(auto i = remaining.begin(); i != remaining.end(); ++i)
91  retval &= isspace(remaining[0]);
92  return retval;
93 
94 }
95 
106 template <typename T>
107 bool from_string(const std::string& s, T& result)
108 {
109  return from_string(s.c_str(), result);
110 }
111 
112 
121 template <typename T>
122 const std::string to_string(typename boost::call_traits<T>::param_type v)
123 {
124  std::stringstream result;
125  result << v;
126  return result.str();
127 }
128 
129 
130 /*
131  The special handling of floating point values is needed,
132  because c++ writes out inf/-inf for infinity, but XML
133  expects uppercase INF/-INF.
134 */
135 template <typename T>
136 const std::string to_string_fp(T v)
137 {
138  std::stringstream result;
139  int inf = std::isinf(v);
140  if (!inf) {
141  result << v;
142  } else {
143  if (inf < 0)
144  result << "-";
145  result << "INF";
146  }
147  return result.str();
148 }
149 
150 template <>
151 inline const std::string to_string<float>(boost::call_traits<float>::param_type v)
152 {
153  return to_string_fp(v);
154 }
155 
156 template <>
157 inline const std::string to_string<double>(boost::call_traits<double>::param_type v)
158 {
159  return to_string_fp(v);
160 }
161 
162 
164 
165 #endif
const std::string to_string(typename boost::call_traits< T >::param_type v)
Definition: tools.hh:122
virtual void operator()(T *)
Definition: tools.hh:43
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
A helper class to make it possible to store a non-pointer object or a pointer that must not be freed ...
Definition: tools.hh:42
functor to wrap statically allocated data a shared pointer representation
Definition: tools.hh:58
const std::string to_string< double >(boost::call_traits< double >::param_type v)
Definition: tools.hh:157
const std::string to_string_fp(T v)
Definition: tools.hh:136
const std::string to_string< float >(boost::call_traits< float >::param_type v)
Definition: tools.hh:151
std::shared_ptr< Data > PData
Definition: tools.hh:60
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
bool from_string(const char *s, T &result)
Definition: tools.hh:78