diff --git a/include/node.h b/include/node.h index 1d39ede..808d67f 100644 --- a/include/node.h +++ b/include/node.h @@ -53,6 +53,9 @@ namespace YAML template const T Read() const; + + template + operator T() const; template friend void operator >> (const Node& node, T& value); @@ -109,6 +112,24 @@ namespace YAML const Node *m_pIdentity; mutable bool m_referenced; }; + + // comparisons with auto-conversion + template + bool operator == (const T& value, const Node& node); + + template + bool operator == (const Node& node, const T& value); + + template + bool operator != (const T& value, const Node& node); + + template + bool operator != (const Node& node, const T& value); + + bool operator == (const char *value, const Node& node); + bool operator == (const Node& node, const char *value); + bool operator != (const char *value, const Node& node); + bool operator != (const Node& node, const char *value); } #include "nodeimpl.h" diff --git a/include/nodeimpl.h b/include/nodeimpl.h index f513fd5..b5a656e 100644 --- a/include/nodeimpl.h +++ b/include/nodeimpl.h @@ -25,6 +25,11 @@ namespace YAML return value; } + template + Node::operator T() const { + return Read(); + } + template inline void operator >> (const Node& node, T& value) { if(!node.Read(value)) @@ -80,6 +85,43 @@ namespace YAML inline const Node& Node::operator [] (const char *key) const { return GetValue(std::string(key)); } + + template + inline bool operator == (const T& value, const Node& node) { + return value == node.Read(); + } + + template + inline bool operator == (const Node& node, const T& value) { + return value == node.Read(); + } + + template + inline bool operator != (const T& value, const Node& node) { + return value != node.Read(); + } + + template + inline bool operator != (const Node& node, const T& value) { + return value != node.Read(); + } + + inline bool operator == (const char *value, const Node& node) { + return std::string(value) == node; + } + + inline bool operator == (const Node& node, const char *value) { + return std::string(value) == node; + } + + inline bool operator != (const char *value, const Node& node) { + return std::string(value) != node; + } + + inline bool operator != (const Node& node, const char *value) { + return std::string(value) != node; + } + } #endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/src/map.cpp b/src/map.cpp index 8aadec9..6a57009 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -53,6 +53,11 @@ namespace YAML return true; } + std::size_t Map::GetSize() const + { + return m_data.size(); + } + void Map::Parse(Scanner *pScanner, const ParserState& state) { Clear(); diff --git a/src/map.h b/src/map.h index fc448b5..8cb67cb 100644 --- a/src/map.h +++ b/src/map.h @@ -26,6 +26,7 @@ namespace YAML virtual bool GetBegin(std::map ::const_iterator& it) const; virtual bool GetEnd(std::map ::const_iterator& it) const; + virtual std::size_t GetSize() const; virtual void Parse(Scanner *pScanner, const ParserState& state); virtual void Write(Emitter& out) const; diff --git a/yaml-reader/spectests.cpp b/yaml-reader/spectests.cpp index 3169b43..50bb4d6 100644 --- a/yaml-reader/spectests.cpp +++ b/yaml-reader/spectests.cpp @@ -5,6 +5,8 @@ #include #include +#define YAML_ASSERT(cond) do { if(!(cond)) return false; } while(false) + namespace Test { namespace { void RunSpecTest(bool (*test)(), const std::string& index, const std::string& name, bool& passed) { @@ -34,23 +36,14 @@ namespace Test { "- Sammy Sosa\n" "- Ken Griffey"; std::stringstream stream(input); - YAML::Parser parser(stream); YAML::Node doc; parser.GetNextDocument(doc); - if(doc.size() != 3) - return false; - std::string output; - doc[0] >> output; - if(output != "Mark McGwire") - return false; - doc[1] >> output; - if(output != "Sammy Sosa") - return false; - doc[2] >> output; - if(output != "Ken Griffey") - return false; + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc[0] == "Mark McGwire"); + YAML_ASSERT(doc[1] == "Sammy Sosa"); + YAML_ASSERT(doc[2] == "Ken Griffey"); return true; } @@ -60,21 +53,14 @@ namespace Test { "avg: 0.278 # Batting average\n" "rbi: 147 # Runs Batted In"; std::stringstream stream(input); - YAML::Parser parser(stream); YAML::Node doc; parser.GetNextDocument(doc); - std::string output; - doc["hr"] >> output; - if(output != "65") - return false; - doc["avg"] >> output; - if(output != "0.278") - return false; - doc["rbi"] >> output; - if(output != "147") - return false; + YAML_ASSERT(doc.size() == 3); + YAML_ASSERT(doc["hr"] == "65"); + YAML_ASSERT(doc["avg"] == "0.278"); + YAML_ASSERT(doc["rbi"] == "147"); return true; } }