My Project
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>
31
33
41template <typename T>
43 virtual void operator () (T *)
44 {
45 }
46};
47
48
58template <typename Data>
60{
61public:
62 typedef std::shared_ptr<Data> PData;
63 PData operator () (Data& d) const
64 {
65 return PData(&d, void_destructor<Data>());
66 }
67};
68
69
80template <typename T>
81bool from_string(const char *s, T& result)
82{
83 std::istringstream sx(s);
84 sx >> result;
85
86 if (sx.fail())
87 return false;
88
89 if (sx.eof())
90 return true;
91
92 std::string remaining;
93 sx >> remaining;
94 bool retval = true;
95
96 for (auto i = remaining.begin(); i != remaining.end(); ++i)
97 retval &= isspace(remaining[0]);
98
99 return retval;
100}
101
112template <typename T>
113bool from_string(const std::string& s, T& result)
114{
115 return from_string(s.c_str(), result);
116}
117
118
127template <typename T>
128const std::string to_string(typename boost::call_traits<T>::param_type v)
129{
130 std::stringstream result;
131 result << v;
132 return result.str();
133}
134
135
136/*
137 The special handling of floating point values is needed,
138 because c++ writes out inf/-inf for infinity, but XML
139 expects uppercase INF/-INF.
140*/
141template <typename T>
142const std::string to_string_fp(T v)
143{
144 std::stringstream result;
145 int inf = std::isinf(v);
146
147 if (!inf) {
148 result << v;
149 } else {
150 if (inf < 0)
151 result << "-";
152
153 result << "INF";
154 }
155
156 return result.str();
157}
158
159template <>
160inline const std::string to_string<float>(boost::call_traits<float>::param_type v)
161{
162 return to_string_fp(v);
163}
164
165template <>
166inline const std::string to_string<double>(boost::call_traits<double>::param_type v)
167{
168 return to_string_fp(v);
169}
170
171
173
174#endif
functor to wrap statically allocated data a shared pointer representation
Definition: tools.hh:60
std::shared_ptr< Data > PData
Definition: tools.hh:62
PData operator()(Data &d) const
Definition: tools.hh:63
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
#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:81
const std::string to_string(typename boost::call_traits< T >::param_type v)
Definition: tools.hh:128
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
virtual void operator()(T *)
Definition: tools.hh:43
const std::string to_string< double >(boost::call_traits< double >::param_type v)
Definition: tools.hh:166
const std::string to_string_fp(T v)
Definition: tools.hh:142
const std::string to_string< float >(boost::call_traits< float >::param_type v)
Definition: tools.hh:160