Added templated casting to nodes, as well as operator == and != (for quick checks, especially to help in testing). Implemented size() on a map node to return the number of key/value pairs (as in std::map)

This commit is contained in:
Jesse Beder 2009-09-06 15:54:11 +00:00
parent 729fb4d30b
commit 07443495c8
5 changed files with 79 additions and 24 deletions

View File

@ -54,6 +54,9 @@ namespace YAML
template <typename T>
const T Read() const;
template <typename T>
operator T() const;
template <typename T>
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 <typename T>
bool operator == (const T& value, const Node& node);
template <typename T>
bool operator == (const Node& node, const T& value);
template <typename T>
bool operator != (const T& value, const Node& node);
template <typename T>
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"

View File

@ -25,6 +25,11 @@ namespace YAML
return value;
}
template <typename T>
Node::operator T() const {
return Read<T>();
}
template <typename T>
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 <typename T>
inline bool operator == (const T& value, const Node& node) {
return value == node.Read<T>();
}
template <typename T>
inline bool operator == (const Node& node, const T& value) {
return value == node.Read<T>();
}
template <typename T>
inline bool operator != (const T& value, const Node& node) {
return value != node.Read<T>();
}
template <typename T>
inline bool operator != (const Node& node, const T& value) {
return value != node.Read<T>();
}
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

View File

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

View File

@ -26,6 +26,7 @@ namespace YAML
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
virtual bool GetEnd(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
virtual std::size_t GetSize() const;
virtual void Parse(Scanner *pScanner, const ParserState& state);
virtual void Write(Emitter& out) const;

View File

@ -5,6 +5,8 @@
#include <vector>
#include <iostream>
#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;
}
}