Switched convert to a templated struct that can be specialized (so we can partially specialize it)

This commit is contained in:
beder
2011-09-08 02:02:15 -05:00
parent 21fbb461c0
commit bb2eafc387
7 changed files with 80 additions and 58 deletions

View File

@@ -1,51 +1,45 @@
#include "yaml-cpp/value.h"
#include <sstream>
#include "yaml-cpp/value/convert.h"
namespace YAML
{
// std::string
template<>
Value convert(const std::string& rhs) {
return Value(rhs);
}
template<>
bool convert(const Value& value, std::string& rhs) {
if(value.Type() != ValueType::Scalar)
return false;
rhs = value.scalar();
return true;
}
#define YAML_DEFINE_CONVERT_STREAMABLE(type)\
template<> Value convert(const type& rhs) {\
std::stringstream stream;\
stream << rhs;\
return Value(stream.str());\
}\
template<> bool convert(const Value& value, type& rhs) {\
if(value.Type() != ValueType::Scalar)\
return false;\
std::stringstream stream(value.scalar());\
stream >> rhs;\
return !!stream;\
}
YAML_DEFINE_CONVERT_STREAMABLE(int)
YAML_DEFINE_CONVERT_STREAMABLE(unsigned)
YAML_DEFINE_CONVERT_STREAMABLE(short)
YAML_DEFINE_CONVERT_STREAMABLE(unsigned short)
YAML_DEFINE_CONVERT_STREAMABLE(long)
YAML_DEFINE_CONVERT_STREAMABLE(unsigned long)
YAML_DEFINE_CONVERT_STREAMABLE(long long)
YAML_DEFINE_CONVERT_STREAMABLE(unsigned long long)
YAML_DEFINE_CONVERT_STREAMABLE(char)
YAML_DEFINE_CONVERT_STREAMABLE(unsigned char)
YAML_DEFINE_CONVERT_STREAMABLE(float)
YAML_DEFINE_CONVERT_STREAMABLE(double)
YAML_DEFINE_CONVERT_STREAMABLE(long double)
#undef YAML_DEFINE_CONVERT_STREAMABLE
//#define YAML_DEFINE_CONVERT_STREAMABLE(type)\
// template<> Value convert(const type& rhs) {\
// std::stringstream stream;\
// stream << rhs;\
// return Value(stream.str());\
// }\
// template<> bool convert(const Value& value, type& rhs) {\
// if(value.Type() != ValueType::Scalar)\
// return false;\
// std::stringstream stream(value.scalar());\
// stream >> rhs;\
// return !!stream;\
// }
//
// YAML_DEFINE_CONVERT_STREAMABLE(int)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned)
// YAML_DEFINE_CONVERT_STREAMABLE(short)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned short)
// YAML_DEFINE_CONVERT_STREAMABLE(long)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long)
// YAML_DEFINE_CONVERT_STREAMABLE(long long)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned long long)
//
// YAML_DEFINE_CONVERT_STREAMABLE(char)
// YAML_DEFINE_CONVERT_STREAMABLE(unsigned char)
//
// YAML_DEFINE_CONVERT_STREAMABLE(float)
// YAML_DEFINE_CONVERT_STREAMABLE(double)
// YAML_DEFINE_CONVERT_STREAMABLE(long double)
//
//#undef YAML_DEFINE_CONVERT_STREAMABLE
//
// template<typename K, typename V>
// Value convert<std::map<K, V> >(const std::map<K, V>& rhs) {
// Value value(ValueType::Map);
// for(std::map<K, V>::const_iterator it=rhs.begin();it!=rhs.end();++it)
// value[it->first] = it->second;
// return value;
// }
}