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:
jbeder
2008-09-24 23:29:00 +00:00
parent ecba08e240
commit bf01059c38
5 changed files with 123 additions and 92 deletions

View File

@@ -32,45 +32,35 @@ namespace YAML
Iterator end() const; Iterator end() const;
unsigned size() const; unsigned size() const;
// extraction of scalars
bool Read(std::string& s) const;
bool Read(int& i) const;
bool Read(unsigned& u) const;
bool Read(long& l) const;
bool Read(float& f) const;
bool Read(double& d) const;
bool Read(char& c) const;
// so you can specialize for other values
template <typename T> template <typename T>
const Node& GetValue(const T& key) const { friend bool Read(const Node& node, T& value);
if(!m_pContent)
throw BadDereference();
for(Iterator it=begin();it!=end();++it) {
T t;
try {
it.first() >> t;
if(key == t)
return it.second();
} catch(RepresentationException&) {
}
}
throw BadDereference();
}
template <typename T> template <typename T>
const Node& operator [] (const T& key) const { friend void operator >> (const Node& node, T& value);
return GetValue(key);
}
const Node& operator [] (const char *key) const { // just for maps
return GetValue(std::string(key)); template <typename T>
} const Node& GetValue(const T& key) const;
template <typename T>
const Node& operator [] (const T& key) const;
const Node& operator [] (const char *key) const;
// just for sequences
const Node& operator [] (unsigned u) const; const Node& operator [] (unsigned u) const;
const Node& operator [] (int i) const; const Node& operator [] (int i) const;
// extraction
friend void operator >> (const Node& node, std::string& s);
friend void operator >> (const Node& node, int& i);
friend void operator >> (const Node& node, unsigned& u);
friend void operator >> (const Node& node, long& l);
friend void operator >> (const Node& node, float& f);
friend void operator >> (const Node& node, double& d);
friend void operator >> (const Node& node, char& c);
// insertion // insertion
friend std::ostream& operator << (std::ostream& out, const Node& node); friend std::ostream& operator << (std::ostream& out, const Node& node);
@@ -94,4 +84,46 @@ namespace YAML
Content *m_pContent; Content *m_pContent;
bool m_alias; bool m_alias;
}; };
// templated things we need to keep inline in the header
template <typename T>
inline bool Read(const Node& node, T& value)
{
return node.Read(value);
}
template <typename T>
inline void operator >> (const Node& node, T& value)
{
if(!Read(node, value))
throw InvalidScalar();
}
template <typename T>
inline const Node& Node::GetValue(const T& key) const
{
if(!m_pContent)
throw BadDereference();
for(Iterator it=begin();it!=end();++it) {
T t;
if(YAML::Read(it.first(), t)) {
if(key == t)
return it.second();
}
}
throw BadDereference();
}
template <typename T>
inline const Node& Node::operator [] (const T& key) const
{
return GetValue(key);
}
inline const Node& Node::operator [] (const char *key) const
{
return GetValue(std::string(key));
}
} }

View File

@@ -36,13 +36,13 @@ namespace YAML
virtual bool IsSequence() const { return false; } virtual bool IsSequence() const { return false; }
// extraction // extraction
virtual void Read(std::string& s) { throw InvalidScalar(); } virtual bool Read(std::string& s) const { return false; }
virtual void Read(int& i) { throw InvalidScalar(); } virtual bool Read(int& i) const { return false; }
virtual void Read(unsigned& u) { throw InvalidScalar(); } virtual bool Read(unsigned& u) const { return false; }
virtual void Read(long& l) { throw InvalidScalar(); } virtual bool Read(long& l) const { return false; }
virtual void Read(float& f) { throw InvalidScalar(); } virtual bool Read(float& f) const { return false; }
virtual void Read(double& d) { throw InvalidScalar(); } virtual bool Read(double& d) const { return false; }
virtual void Read(char& c) { throw InvalidScalar(); } virtual bool Read(char& c) const { return false; }
// ordering // ordering
virtual int Compare(Content *pContent) { return 0; } virtual int Compare(Content *pContent) { return 0; }

View File

@@ -232,61 +232,65 @@ namespace YAML
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
// Extraction // 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; 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) if(!m_pContent)
throw; return false;
node.m_pContent->Read(c); return m_pContent->Read(c);
} }
std::ostream& operator << (std::ostream& out, const Node& node) std::ostream& operator << (std::ostream& out, const Node& node)

View File

@@ -38,57 +38,52 @@ namespace YAML
out << "\"\n"; out << "\"\n";
} }
void Scalar::Read(std::string& s) bool Scalar::Read(std::string& s) const
{ {
s = m_data; s = m_data;
return true;
} }
void Scalar::Read(int& i) bool Scalar::Read(int& i) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> i; data >> i;
if(!data) return !data.fail();
throw InvalidScalar();
} }
void Scalar::Read(unsigned& u) bool Scalar::Read(unsigned& u) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> u; data >> u;
if(!data) return !data.fail();
throw InvalidScalar();
} }
void Scalar::Read(long& l) bool Scalar::Read(long& l) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> l; data >> l;
if(!data) return !data.fail();
throw InvalidScalar();
} }
void Scalar::Read(float& f) bool Scalar::Read(float& f) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> f; data >> f;
if(!data) return !data.fail();
throw InvalidScalar();
} }
void Scalar::Read(double& d) bool Scalar::Read(double& d) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> d; data >> d;
if(!data) return !data.fail();
throw InvalidScalar();
} }
void Scalar::Read(char& c) bool Scalar::Read(char& c) const
{ {
std::stringstream data(m_data); std::stringstream data(m_data);
data >> c; data >> c;
if(!data) return !data.fail();
throw InvalidScalar();
} }
int Scalar::Compare(Content *pContent) int Scalar::Compare(Content *pContent)

View File

@@ -17,13 +17,13 @@ namespace YAML
virtual bool IsScalar() const { return true; } virtual bool IsScalar() const { return true; }
// extraction // extraction
virtual void Read(std::string& s); virtual bool Read(std::string& s) const;
virtual void Read(int& i); virtual bool Read(int& i) const;
virtual void Read(unsigned& u); virtual bool Read(unsigned& u) const;
virtual void Read(long& l); virtual bool Read(long& l) const;
virtual void Read(float& f); virtual bool Read(float& f) const;
virtual void Read(double& d); virtual bool Read(double& d) const;
virtual void Read(char& c); virtual bool Read(char& c) const;
// ordering // ordering
virtual int Compare(Content *pContent); virtual int Compare(Content *pContent);