Implemented conversion for std::string, including a bypass-accessor to the scalar value

This commit is contained in:
beder
2011-09-07 15:49:01 -05:00
parent a9c7f8cc5a
commit 3b0cc619b2
7 changed files with 41 additions and 3 deletions

View File

@@ -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); }

View File

@@ -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;

View File

@@ -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; }

View File

@@ -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
{ {

View File

@@ -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
View 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;
}
}

View File

@@ -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;
} }