Joshua Moerman
13 years ago
10 changed files with 316 additions and 108 deletions
@ -0,0 +1,55 @@ |
|||
//
|
|||
// 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 |
@ -0,0 +1,105 @@ |
|||
//
|
|||
// stf_input.hpp
|
|||
// AwesomeAttract0r
|
|||
//
|
|||
// Created by Joshua Moerman on 1/2/12.
|
|||
// Copyright (c) 2012 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef AwesomeAttract0r_stf_input_hpp |
|||
#define AwesomeAttract0r_stf_input_hpp |
|||
|
|||
#include <sstream> |
|||
#include <vector> |
|||
#include <array> |
|||
|
|||
#include "stf.hpp" |
|||
#include "std_string_ext.hpp" |
|||
|
|||
namespace stfu { |
|||
// prototypes
|
|||
template <typename T> |
|||
T from_stf(node const & n); |
|||
|
|||
// implemtations
|
|||
namespace { |
|||
template <typename T, typename Enable = void> |
|||
struct from_stf_struct { |
|||
T operator()(node const & n){ |
|||
return T::from_stf(n); |
|||
} |
|||
}; |
|||
|
|||
template <typename T> |
|||
struct from_stf_struct<std::vector<T>, typename std::enable_if<!std::is_fundamental<T>::value>::type>; |
|||
template <typename T> |
|||
struct from_stf_struct<std::vector<T>, typename std::enable_if<std::is_fundamental<T>::value>::type>; |
|||
template <typename T, size_t N> |
|||
struct from_stf_struct<std::array<T, N>, typename std::enable_if<!std::is_fundamental<T>::value>::type>; |
|||
template <typename T, size_t N> |
|||
struct from_stf_struct<std::array<T, N>, typename std::enable_if<std::is_fundamental<T>::value>::type>; |
|||
|
|||
template <typename T> |
|||
struct from_stf_struct<std::vector<T>, typename std::enable_if<!std::is_fundamental<T>::value>::type>{ |
|||
std::vector<T> operator()(node const & n){ |
|||
std::vector<T> v; |
|||
bool done = false; |
|||
for(unsigned int i = 0; !done; ++i){ |
|||
try { |
|||
v.push_back(from_stf<T>(n.getChild(i))); |
|||
} catch (std::out_of_range& e) { |
|||
done = true; |
|||
} |
|||
} |
|||
return v; |
|||
} |
|||
}; |
|||
|
|||
template <typename T> |
|||
struct from_stf_struct<std::vector<T>, typename std::enable_if<std::is_fundamental<T>::value>::type>{ |
|||
std::vector<T> operator()(node const & n){ |
|||
std::vector<T> v; |
|||
bool done = false; |
|||
for(unsigned int i = 0; !done; ++i){ |
|||
try { |
|||
v.push_back(std::from_string<T>(n.getValue(i))); |
|||
} catch (std::out_of_range& e) { |
|||
done = true; |
|||
} |
|||
} |
|||
return v; |
|||
} |
|||
}; |
|||
|
|||
template <typename T, size_t N> |
|||
struct from_stf_struct<std::array<T, N>, typename std::enable_if<!std::is_fundamental<T>::value>::type>{ |
|||
std::array<T, N> operator()(node const & n){ |
|||
std::array<T, N> v; |
|||
for(unsigned int i = 0; i < N; ++i){ |
|||
v[i] = from_stf<T>(n.getChild(i)); |
|||
} |
|||
return v; |
|||
} |
|||
}; |
|||
|
|||
template <typename T, size_t N> |
|||
struct from_stf_struct<std::array<T, N>, typename std::enable_if<std::is_fundamental<T>::value>::type>{ |
|||
std::array<T, N> operator()(node const & n){ |
|||
std::array<T, N> v; |
|||
for(unsigned int i = 0; i < N; ++i){ |
|||
v[i] = std::from_string<T>(n.getValue(i)); |
|||
} |
|||
return v; |
|||
} |
|||
}; |
|||
|
|||
} |
|||
|
|||
template <typename T> |
|||
T from_stf(node const & n){ |
|||
return from_stf_struct<T>()(n); |
|||
} |
|||
|
|||
} // namespace stfu
|
|||
|
|||
#endif |
@ -0,0 +1,85 @@ |
|||
//
|
|||
// stf_output.hpp
|
|||
// AwesomeAttract0r
|
|||
//
|
|||
// Created by Joshua Moerman on 1/2/12.
|
|||
// Copyright (c) 2012 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef AwesomeAttract0r_stf_output_hpp |
|||
#define AwesomeAttract0r_stf_output_hpp |
|||
|
|||
#include <sstream> |
|||
#include <vector> |
|||
#include <array> |
|||
|
|||
#include "stf.hpp" |
|||
#include "std_string_ext.hpp" |
|||
|
|||
namespace stfu { |
|||
// Prototypes
|
|||
#define to_stf_container_proto(x) \ |
|||
template <typename T> \ |
|||
typename std::enable_if<std::is_fundamental<T>::value, node>::type to_stf(x<T> const & array); \ |
|||
template <typename T> \ |
|||
typename std::enable_if<!std::is_fundamental<T>::value, node>::type to_stf(x<T> const & array); |
|||
|
|||
to_stf_container_proto(std::vector) |
|||
|
|||
#undef to_stf_container_proto |
|||
|
|||
template <typename T, size_t N> |
|||
typename std::enable_if<std::is_fundamental<T>::value, node>::type to_stf(std::array<T, N> const & array); |
|||
template <typename T, size_t N> |
|||
typename std::enable_if<!std::is_fundamental<T>::value, node>::type to_stf(std::array<T, N> const & array); |
|||
template <typename T> |
|||
node to_stf(T const & x); |
|||
|
|||
// implementations
|
|||
#define to_stf_container(x) \ |
|||
template <typename T> \ |
|||
typename std::enable_if<std::is_fundamental<T>::value, node>::type to_stf(x<T> const & array) { \ |
|||
node node; \ |
|||
for (auto it = array.cbegin(); it != array.cend(); ++it) \ |
|||
node.addValue() = std::to_string(*it); \ |
|||
return node; \ |
|||
} \ |
|||
\ |
|||
template <typename T> \ |
|||
typename std::enable_if<!std::is_fundamental<T>::value, node>::type to_stf(x<T> const & array) { \ |
|||
node node; \ |
|||
for (auto it = array.cbegin(); it != array.cend(); ++it) \ |
|||
node.addChild() = to_stf(*it); \ |
|||
return node; \ |
|||
} |
|||
|
|||
to_stf_container(std::vector) |
|||
|
|||
#undef to_stf_container |
|||
|
|||
template <typename T, size_t N> |
|||
typename std::enable_if<std::is_fundamental<T>::value, node>::type to_stf(std::array<T, N> const & array) { |
|||
node node; |
|||
for (auto it = array.cbegin(); it != array.cend(); ++it){ |
|||
node.addValue() = std::to_string(*it); |
|||
} |
|||
return node; |
|||
} |
|||
|
|||
template <typename T, size_t N> |
|||
typename std::enable_if<!std::is_fundamental<T>::value, node>::type to_stf(std::array<T, N> const & array) { |
|||
node node; |
|||
for (auto it = array.cbegin(); it != array.cend(); ++it){ |
|||
node.addChild() = to_stf(*it); |
|||
} |
|||
return node; |
|||
} |
|||
|
|||
template <typename T> |
|||
node to_stf(T const & x){ |
|||
return x.to_stf(); |
|||
} |
|||
|
|||
} // namespace stfu
|
|||
|
|||
#endif |
Reference in new issue