You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
//
|
|
// std_string_ext.hpp
|
|
// AwesomeAttract0r
|
|
//
|
|
// Created by Joshua Moerman on 1/2/12.
|
|
// Copyright (c) 2012 Vadovas. All rights reserved.
|
|
//
|
|
|
|
#ifndef AwesomeAttract0r_std_string_ext_hpp
|
|
#define AwesomeAttract0r_std_string_ext_hpp
|
|
|
|
namespace std {
|
|
template <typename T>
|
|
std::string to_string(T const & x){
|
|
std::stringstream ss;
|
|
ss << x;
|
|
return ss.str();
|
|
}
|
|
|
|
namespace {
|
|
template <typename T>
|
|
struct from_string_struct {
|
|
T operator()(string const & s){
|
|
stringstream ss(s);
|
|
T t;
|
|
ss >> t;
|
|
return t;
|
|
}
|
|
};
|
|
|
|
#define from_string_fund(x, y) \
|
|
template <> \
|
|
struct from_string_struct<x> { \
|
|
x operator()(string const & s){ return y(s); } \
|
|
};
|
|
from_string_fund(float, stof)
|
|
from_string_fund(double, stod)
|
|
from_string_fund(long double, stold)
|
|
from_string_fund(int, stoi)
|
|
from_string_fund(long int, stol)
|
|
from_string_fund(long unsigned int, stoul)
|
|
from_string_fund(long long int, stoll)
|
|
from_string_fund(long long unsigned int, stoull)
|
|
from_string_fund(string, )
|
|
#undef from_string_fund
|
|
} // namespace
|
|
|
|
template <typename T>
|
|
T from_string(string const & s){
|
|
return from_string_struct<T>()(s);
|
|
}
|
|
}
|
|
|
|
|
|
#endif
|
|
|