mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 12:41:17 +00:00
Implemented conversion for std::string, including a bypass-accessor to the scalar value
This commit is contained in:
@@ -20,9 +20,10 @@ namespace YAML
|
|||||||
public:
|
public:
|
||||||
explicit node(shared_node_ref pRef): m_pRef(pRef) {}
|
explicit node(shared_node_ref pRef): m_pRef(pRef) {}
|
||||||
|
|
||||||
|
bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; }
|
||||||
ValueType::value type() const { return m_pRef->type(); }
|
ValueType::value type() const { return m_pRef->type(); }
|
||||||
|
|
||||||
bool is(const node& rhs) const { return m_pRef == rhs.m_pRef; }
|
const std::string& scalar() const { return m_pRef->scalar(); }
|
||||||
|
|
||||||
void set_ref(const node& rhs) { m_pRef = rhs.m_pRef; }
|
void set_ref(const node& rhs) { m_pRef = rhs.m_pRef; }
|
||||||
void set_data(const node& rhs) { m_pRef->set_data(*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);
|
void set_scalar(const std::string& scalar);
|
||||||
|
|
||||||
ValueType::value type() const { return m_isDefined ? m_type : ValueType::Undefined; }
|
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
|
// indexing
|
||||||
template<typename Key> shared_node get(const Key& key, shared_memory_holder pMemory) const;
|
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) {}
|
node_ref(): m_pData(new node_data) {}
|
||||||
|
|
||||||
ValueType::value type() const { return m_pData->type(); }
|
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; }
|
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");
|
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
|
// assignment
|
||||||
inline bool Value::is(const Value& rhs) const
|
inline bool Value::is(const Value& rhs) const
|
||||||
{
|
{
|
||||||
|
@@ -29,6 +29,7 @@ namespace YAML
|
|||||||
|
|
||||||
// access
|
// access
|
||||||
template<typename T> const T as() const;
|
template<typename T> const T as() const;
|
||||||
|
const std::string& scalar() const;
|
||||||
|
|
||||||
// assignment
|
// assignment
|
||||||
bool is(const Value& rhs) const;
|
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;
|
YAML::Value value;
|
||||||
value["key"] = "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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user