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:
parent
729fb4d30b
commit
07443495c8
@ -53,6 +53,9 @@ namespace YAML
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T Read() const;
|
const T Read() const;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
operator T() const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
friend void operator >> (const Node& node, T& value);
|
friend void operator >> (const Node& node, T& value);
|
||||||
@ -109,6 +112,24 @@ namespace YAML
|
|||||||
const Node *m_pIdentity;
|
const Node *m_pIdentity;
|
||||||
mutable bool m_referenced;
|
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"
|
#include "nodeimpl.h"
|
||||||
|
@ -25,6 +25,11 @@ namespace YAML
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Node::operator T() const {
|
||||||
|
return Read<T>();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline void operator >> (const Node& node, T& value) {
|
inline void operator >> (const Node& node, T& value) {
|
||||||
if(!node.Read(value))
|
if(!node.Read(value))
|
||||||
@ -80,6 +85,43 @@ namespace YAML
|
|||||||
inline const Node& Node::operator [] (const char *key) const {
|
inline const Node& Node::operator [] (const char *key) const {
|
||||||
return GetValue(std::string(key));
|
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
|
#endif // NODEIMPL_H_62B23520_7C8E_11DE_8A39_0800200C9A66
|
||||||
|
@ -53,6 +53,11 @@ namespace YAML
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t Map::GetSize() const
|
||||||
|
{
|
||||||
|
return m_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
void Map::Parse(Scanner *pScanner, const ParserState& state)
|
void Map::Parse(Scanner *pScanner, const ParserState& state)
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
|
@ -26,6 +26,7 @@ namespace YAML
|
|||||||
|
|
||||||
virtual bool GetBegin(std::map <Node *, Node *, ltnode>::const_iterator& it) const;
|
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 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 Parse(Scanner *pScanner, const ParserState& state);
|
||||||
virtual void Write(Emitter& out) const;
|
virtual void Write(Emitter& out) const;
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#define YAML_ASSERT(cond) do { if(!(cond)) return false; } while(false)
|
||||||
|
|
||||||
namespace Test {
|
namespace Test {
|
||||||
namespace {
|
namespace {
|
||||||
void RunSpecTest(bool (*test)(), const std::string& index, const std::string& name, bool& passed) {
|
void RunSpecTest(bool (*test)(), const std::string& index, const std::string& name, bool& passed) {
|
||||||
@ -34,23 +36,14 @@ namespace Test {
|
|||||||
"- Sammy Sosa\n"
|
"- Sammy Sosa\n"
|
||||||
"- Ken Griffey";
|
"- Ken Griffey";
|
||||||
std::stringstream stream(input);
|
std::stringstream stream(input);
|
||||||
|
|
||||||
YAML::Parser parser(stream);
|
YAML::Parser parser(stream);
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
if(doc.size() != 3)
|
YAML_ASSERT(doc.size() == 3);
|
||||||
return false;
|
YAML_ASSERT(doc[0] == "Mark McGwire");
|
||||||
std::string output;
|
YAML_ASSERT(doc[1] == "Sammy Sosa");
|
||||||
doc[0] >> output;
|
YAML_ASSERT(doc[2] == "Ken Griffey");
|
||||||
if(output != "Mark McGwire")
|
|
||||||
return false;
|
|
||||||
doc[1] >> output;
|
|
||||||
if(output != "Sammy Sosa")
|
|
||||||
return false;
|
|
||||||
doc[2] >> output;
|
|
||||||
if(output != "Ken Griffey")
|
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,21 +53,14 @@ namespace Test {
|
|||||||
"avg: 0.278 # Batting average\n"
|
"avg: 0.278 # Batting average\n"
|
||||||
"rbi: 147 # Runs Batted In";
|
"rbi: 147 # Runs Batted In";
|
||||||
std::stringstream stream(input);
|
std::stringstream stream(input);
|
||||||
|
|
||||||
YAML::Parser parser(stream);
|
YAML::Parser parser(stream);
|
||||||
YAML::Node doc;
|
YAML::Node doc;
|
||||||
parser.GetNextDocument(doc);
|
parser.GetNextDocument(doc);
|
||||||
|
|
||||||
std::string output;
|
YAML_ASSERT(doc.size() == 3);
|
||||||
doc["hr"] >> output;
|
YAML_ASSERT(doc["hr"] == "65");
|
||||||
if(output != "65")
|
YAML_ASSERT(doc["avg"] == "0.278");
|
||||||
return false;
|
YAML_ASSERT(doc["rbi"] == "147");
|
||||||
doc["avg"] >> output;
|
|
||||||
if(output != "0.278")
|
|
||||||
return false;
|
|
||||||
doc["rbi"] >> output;
|
|
||||||
if(output != "147")
|
|
||||||
return false;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user