mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 20:51:16 +00:00
Added streamable conversions
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
#include "yaml-cpp/value.h"
|
#include "yaml-cpp/value.h"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace YAML
|
namespace YAML
|
||||||
{
|
{
|
||||||
|
// std::string
|
||||||
template<>
|
template<>
|
||||||
Value convert(const std::string& rhs) {
|
Value convert(const std::string& rhs) {
|
||||||
return Value(rhs);
|
return Value(rhs);
|
||||||
@@ -14,4 +16,36 @@ namespace YAML
|
|||||||
rhs = value.scalar();
|
rhs = value.scalar();
|
||||||
return true;
|
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
|
||||||
}
|
}
|
||||||
|
@@ -7,8 +7,10 @@ int main()
|
|||||||
std::cout << value["key"].as<std::string>() << "\n";
|
std::cout << value["key"].as<std::string>() << "\n";
|
||||||
value["key"]["key"] = "value";
|
value["key"]["key"] = "value";
|
||||||
std::cout << value["key"]["key"].as<std::string>() << "\n";
|
std::cout << value["key"]["key"].as<std::string>() << "\n";
|
||||||
// value[5] = "monkey";
|
value[5] = "monkey";
|
||||||
// std::cout << value[5].as<std::string>() << "\n";
|
std::cout << value[5].as<std::string>() << "\n";
|
||||||
|
value["monkey"] = 5;
|
||||||
|
std::cout << value["monkey"].as<int>() << "\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user