mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-10 04:51:17 +00:00
Added Read() functions for Node that return true/false, so we can easily check if a read is successful without throwing.
But we still have operator >> that throws on failure.
This commit is contained in:
@@ -36,13 +36,13 @@ namespace YAML
|
||||
virtual bool IsSequence() const { return false; }
|
||||
|
||||
// extraction
|
||||
virtual void Read(std::string& s) { throw InvalidScalar(); }
|
||||
virtual void Read(int& i) { throw InvalidScalar(); }
|
||||
virtual void Read(unsigned& u) { throw InvalidScalar(); }
|
||||
virtual void Read(long& l) { throw InvalidScalar(); }
|
||||
virtual void Read(float& f) { throw InvalidScalar(); }
|
||||
virtual void Read(double& d) { throw InvalidScalar(); }
|
||||
virtual void Read(char& c) { throw InvalidScalar(); }
|
||||
virtual bool Read(std::string& s) const { return false; }
|
||||
virtual bool Read(int& i) const { return false; }
|
||||
virtual bool Read(unsigned& u) const { return false; }
|
||||
virtual bool Read(long& l) const { return false; }
|
||||
virtual bool Read(float& f) const { return false; }
|
||||
virtual bool Read(double& d) const { return false; }
|
||||
virtual bool Read(char& c) const { return false; }
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *pContent) { return 0; }
|
||||
|
60
src/node.cpp
60
src/node.cpp
@@ -232,61 +232,65 @@ namespace YAML
|
||||
|
||||
///////////////////////////////////////////////////////
|
||||
// Extraction
|
||||
// Note: these Read() functions are identical, but
|
||||
// they're not templated because they use a Content virtual
|
||||
// function, so we'd have to #include that in node.h, and
|
||||
// I don't want to.
|
||||
|
||||
void operator >> (const Node& node, std::string& s)
|
||||
bool Node::Read(std::string& s) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(s);
|
||||
return m_pContent->Read(s);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, int& i)
|
||||
bool Node::Read(int& i) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(i);
|
||||
return m_pContent->Read(i);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, unsigned& u)
|
||||
bool Node::Read(unsigned& u) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(u);
|
||||
return m_pContent->Read(u);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, long& l)
|
||||
bool Node::Read(long& l) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(l);
|
||||
return m_pContent->Read(l);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, float& f)
|
||||
bool Node::Read(float& f) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(f);
|
||||
return m_pContent->Read(f);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, double& d)
|
||||
bool Node::Read(double& d) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(d);
|
||||
return m_pContent->Read(d);
|
||||
}
|
||||
|
||||
void operator >> (const Node& node, char& c)
|
||||
bool Node::Read(char& c) const
|
||||
{
|
||||
if(!node.m_pContent)
|
||||
throw;
|
||||
if(!m_pContent)
|
||||
return false;
|
||||
|
||||
node.m_pContent->Read(c);
|
||||
return m_pContent->Read(c);
|
||||
}
|
||||
|
||||
std::ostream& operator << (std::ostream& out, const Node& node)
|
||||
|
@@ -38,57 +38,52 @@ namespace YAML
|
||||
out << "\"\n";
|
||||
}
|
||||
|
||||
void Scalar::Read(std::string& s)
|
||||
bool Scalar::Read(std::string& s) const
|
||||
{
|
||||
s = m_data;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Scalar::Read(int& i)
|
||||
bool Scalar::Read(int& i) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> i;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
void Scalar::Read(unsigned& u)
|
||||
bool Scalar::Read(unsigned& u) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> u;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
void Scalar::Read(long& l)
|
||||
bool Scalar::Read(long& l) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> l;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
void Scalar::Read(float& f)
|
||||
bool Scalar::Read(float& f) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> f;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
void Scalar::Read(double& d)
|
||||
bool Scalar::Read(double& d) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> d;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
void Scalar::Read(char& c)
|
||||
bool Scalar::Read(char& c) const
|
||||
{
|
||||
std::stringstream data(m_data);
|
||||
data >> c;
|
||||
if(!data)
|
||||
throw InvalidScalar();
|
||||
return !data.fail();
|
||||
}
|
||||
|
||||
int Scalar::Compare(Content *pContent)
|
||||
|
14
src/scalar.h
14
src/scalar.h
@@ -17,13 +17,13 @@ namespace YAML
|
||||
virtual bool IsScalar() const { return true; }
|
||||
|
||||
// extraction
|
||||
virtual void Read(std::string& s);
|
||||
virtual void Read(int& i);
|
||||
virtual void Read(unsigned& u);
|
||||
virtual void Read(long& l);
|
||||
virtual void Read(float& f);
|
||||
virtual void Read(double& d);
|
||||
virtual void Read(char& c);
|
||||
virtual bool Read(std::string& s) const;
|
||||
virtual bool Read(int& i) const;
|
||||
virtual bool Read(unsigned& u) const;
|
||||
virtual bool Read(long& l) const;
|
||||
virtual bool Read(float& f) const;
|
||||
virtual bool Read(double& d) const;
|
||||
virtual bool Read(char& c) const;
|
||||
|
||||
// ordering
|
||||
virtual int Compare(Content *pContent);
|
||||
|
Reference in New Issue
Block a user