From 33a71151ca595a0e96f3324ac536078d6cddaba5 Mon Sep 17 00:00:00 2001 From: beder Date: Sat, 10 Sep 2011 16:50:44 -0500 Subject: [PATCH] Added helper emitter functions, but we have a problem: YAML::Value is already a manipulator --- include/yaml-cpp/value.h | 1 + include/yaml-cpp/value/emit.h | 21 +++++++++++++++++++++ src/value/emit.cpp | 23 +++++++++++++++++++++++ src/value/valueevents.h | 2 +- util/value.cpp | 4 +--- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 include/yaml-cpp/value/emit.h create mode 100644 src/value/emit.cpp diff --git a/include/yaml-cpp/value.h b/include/yaml-cpp/value.h index 9f11204..f4b3d61 100644 --- a/include/yaml-cpp/value.h +++ b/include/yaml-cpp/value.h @@ -12,5 +12,6 @@ #include "yaml-cpp/value/iterator.h" #include "yaml-cpp/value/detail/impl.h" #include "yaml-cpp/value/parse.h" +#include "yaml-cpp/value/emit.h" #endif // VALUE_H_62B23520_7C8E_11DE_8A39_0800200C9A66 diff --git a/include/yaml-cpp/value/emit.h b/include/yaml-cpp/value/emit.h new file mode 100644 index 0000000..056b65e --- /dev/null +++ b/include/yaml-cpp/value/emit.h @@ -0,0 +1,21 @@ +#ifndef VALUE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 +#define VALUE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + +#if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4 +#pragma once +#endif + +#include +#include + +namespace YAML +{ + class Emitter; + class Value; + + Emitter& operator << (Emitter& out, const foo& value); + std::ostream& operator << (std::ostream& out, const Value& value); +} + +#endif // VALUE_EMIT_H_62B23520_7C8E_11DE_8A39_0800200C9A66 + diff --git a/src/value/emit.cpp b/src/value/emit.cpp new file mode 100644 index 0000000..3cfc13d --- /dev/null +++ b/src/value/emit.cpp @@ -0,0 +1,23 @@ +#include "yaml-cpp/value/emit.h" +#include "yaml-cpp/emitfromevents.h" +#include "yaml-cpp/emitter.h" +#include "valueevents.h" + +namespace YAML +{ + Emitter& operator << (Emitter& out, const Value& value) + { + EmitFromEvents emitFromEvents(out); + ValueEvents events(value); + events.Emit(emitFromEvents); + return out; + } + + std::ostream& operator << (std::ostream& out, const Value& value) + { + Emitter emitter; + emitter << value; + out << emitter.c_str(); + return out; + } +} diff --git a/src/value/valueevents.h b/src/value/valueevents.h index eeb74d6..4a7761f 100644 --- a/src/value/valueevents.h +++ b/src/value/valueevents.h @@ -18,7 +18,7 @@ namespace YAML class ValueEvents { public: - ValueEvents(const Value& value); + explicit ValueEvents(const Value& value); void Emit(EventHandler& handler); diff --git a/util/value.cpp b/util/value.cpp index f5a1307..164d569 100644 --- a/util/value.cpp +++ b/util/value.cpp @@ -4,9 +4,7 @@ int main() { YAML::Value value = YAML::Parse("{foo: bar, monkey: value}"); - for(YAML::const_iterator it=value.begin();it!=value.end();++it) { - std::cout << it->first.as() << " -> " << it->second.as() << "\n"; - } + std::cout << value << "\n"; return 0; }