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.
106 lines
2.7 KiB
106 lines
2.7 KiB
13 years ago
|
//
|
||
|
// 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
|