00001 #ifndef _theplu_yat_utility_utility_
00002 #define _theplu_yat_utility_utility_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00033
00034 #include "deprecate.h"
00035
00036 #include <cmath>
00037 #include <limits>
00038 #include <string>
00039 #include <stdexcept>
00040 #include <sstream>
00041 #include <utility>
00042 #include <vector>
00043
00044 namespace theplu {
00045 namespace yat {
00046 namespace utility {
00047
00058 template<typename InputIterator, typename OutputIterator>
00059 bool binary_weight(InputIterator first, InputIterator last,
00060 OutputIterator result);
00061
00062
00068 template<typename T>
00069 T convert(const std::string& s);
00070
00076 template<typename T>
00077 bool is(const std::string& s);
00078
00085 bool is_double(const std::string&) YAT_DEPRECATE;
00086
00092 bool is_equal(std::string s, std::string other);
00093
00100 bool is_float(const std::string&) YAT_DEPRECATE;
00101
00108 bool is_int(const std::string&) YAT_DEPRECATE;
00109
00113 bool is_nan(const std::string& s);
00114
00115
00116 template<typename InputIterator, typename OutputIterator>
00117 bool binary_weight(InputIterator first, InputIterator last,
00118 OutputIterator result)
00119 {
00120 bool nan=false;
00121 while (first!=last) {
00122 if (std::isnan(*first)) {
00123 *result=0;
00124 nan=true;
00125 }
00126 else
00127 *result = 1.0;
00128 ++first;
00129 ++result;
00130 }
00131 return nan;
00132 }
00133
00134
00135
00136 template<typename T>
00137 T convert(const std::string& s)
00138 {
00139 if (is_nan(s))
00140 return std::numeric_limits<T>::quiet_NaN();
00141 if (is_equal(s, "inf"))
00142 return std::numeric_limits<T>::infinity();
00143 if (is_equal(s, "-inf"))
00144 if (std::numeric_limits<T>::is_signed)
00145 return -std::numeric_limits<T>::infinity();
00146 else
00147 throw std::runtime_error(std::string("convert(\"")+s+
00148 std::string("\"): type is unsigned") );
00149 std::stringstream ss(s);
00150 T a;
00151 ss >> a;
00152 bool ok = true;
00153 if(ss.fail())
00154 ok = false;
00155
00156 std::string b;
00157 ss >> b;
00158 if (!b.empty() || !ok)
00159 throw std::runtime_error(std::string("convert(\"")+s+std::string("\")"));
00160 return a;
00161 }
00162
00163 template<typename T>
00164 bool is(const std::string& s)
00165 {
00166 if (is_nan(s))
00167 return std::numeric_limits<T>::has_quiet_NaN;
00168 if (is_equal(s, "inf"))
00169 return std::numeric_limits<T>::has_infinity;
00170 if (is_equal(s, "-inf"))
00171 return std::numeric_limits<T>::has_infinity &&
00172 std::numeric_limits<T>::is_signed;
00173 std::stringstream ss(s);
00174 T a;
00175 ss >> a;
00176 if(ss.fail())
00177 return false;
00178
00179 std::string b;
00180 ss >> b;
00181 return b.empty();
00182 }
00183
00184 }}}
00185
00186 #endif