Implemented conversion for std::string, including a bypass-accessor to the scalar value
This commit is contained in:
parent
1ab16bac62
commit
a95baeafd6
@ -20,9 +20,10 @@ namespace YAML
|
||||
public:
|
||||
explicit node(shared_node_ref pRef): m_pRef(pRef) {}
|
||||
|
||||
ValueType::value type() const { return m_pRef->type(); }
|
||||
|
||||
bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; }
|
||||
ValueType::value type() const { return m_pRef->type(); }
|
||||
|
||||
const std::string& scalar() const { return m_pRef->scalar(); }
|
||||
|
||||
void set_ref(const node& rhs) { m_pRef = rhs.m_pRef; }
|
||||
void set_data(const node& rhs) { m_pRef->set_data(*rhs.m_pRef); }
|
||||
|
@ -27,7 +27,7 @@ namespace YAML
|
||||
void set_scalar(const std::string& scalar);
|
||||
|
||||
ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; }
|
||||
const std::string scalar() const { return m_scalar; }
|
||||
const std::string& scalar() const { return m_scalar; }
|
||||
|
||||
// indexing
|
||||
template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) const;
|
||||
|
@ -21,6 +21,7 @@ namespace YAML
|
||||
node_ref(): m_pData(new node_data) {}
|
||||
|
||||
ValueType::value type() const { return m_pData->type(); }
|
||||
const std::string& scalar() const { return m_pData->scalar(); }
|
||||
|
||||
void set_data(const node_ref& rhs) { m_pData = rhs.m_pData; }
|
||||
|
||||
|
@ -56,6 +56,19 @@ namespace YAML
|
||||
throw std::runtime_error("Unable to convert to type");
|
||||
}
|
||||
|
||||
template<>
|
||||
inline const std::string Value::as() const
|
||||
{
|
||||
if(Type() != ValueType::Scalar)
|
||||
throw std::runtime_error("Unable to convert to string, not a scalar");
|
||||
return scalar();
|
||||
}
|
||||
|
||||
inline const std::string& Value::scalar() const
|
||||
{
|
||||
return m_pNode->scalar();
|
||||
}
|
||||
|
||||
// assignment
|
||||
inline bool Value::is(const Value& rhs) const
|
||||
{
|
||||
|
@ -29,6 +29,7 @@ namespace YAML
|
||||
|
||||
// access
|
||||
template<typename T> const T as() const;
|
||||
const std::string& scalar() const;
|
||||
|
||||
// assignment
|
||||
bool is(const Value& rhs) const;
|
||||
|
17
src/value/convert.cpp
Normal file
17
src/value/convert.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include "yaml-cpp/value.h"
|
||||
|
||||
namespace YAML
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
@ -4,6 +4,11 @@ int main()
|
||||
{
|
||||
YAML::Value value;
|
||||
value["key"] = "value";
|
||||
std::cout << value["key"].as<std::string>() << "\n";
|
||||
value["key"]["key"] = "value";
|
||||
std::cout << value["key"]["key"].as<std::string>() << "\n";
|
||||
// value[5] = "monkey";
|
||||
// std::cout << value[5].as<std::string>() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user