From b8d0b274b1874b70bf77c1cd539721425c462384 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 15 Jul 2014 23:31:48 -0700 Subject: [PATCH] Implement templated VectorParser --- src/yaml/vector_parser.hh | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/yaml/vector_parser.hh b/src/yaml/vector_parser.hh index e69de29..dd52c20 100644 --- a/src/yaml/vector_parser.hh +++ b/src/yaml/vector_parser.hh @@ -0,0 +1,76 @@ +/* vector_parser.hh + * vim: set tw=80: + * Eryn Wells + */ +/** + * A VectorParser is a utility parser that processes a sequence of identically + * typed values and calls back with a vector containing those values + */ + +#ifndef __YAML_VECTOR_PARSER_HH__ +#define __YAML_VECTOR_PARSER_HH__ + +#include +#include + +#include "parsers.hh" + + +namespace yaml { + +/** + * Parse a YAML sequence of homogeneous scalars into a vector of the templated + * type. + */ +template +struct VectorParser + : public UtilityParser > +{ + typedef T Type; + typedef std::vector VectorType; + + /** Constructor */ + VectorParser(Scene& scene, ParserStack& parsers, + typename UtilityParser::CallbackFunction callback) + : UtilityParser(scene, parsers, callback), + mVector() + { } + + /** + * Handle a YAML parser event. + * + * @param [in] event The parser event + */ + void + HandleEvent(yaml_event_t& event) + { + if (event.type == YAML_SEQUENCE_END_EVENT) { + /* + * XXX: Need to prefix with this-> for some reason. Maybe the + * compiler can't figure out the type properly? + */ + this->SetDone(true); + + /* We have a completed vector. Notify the caller. */ + this->Notify(mVector); + return; + } + + if (event.type != YAML_SCALAR_EVENT) { + assert(event.type != YAML_SCALAR_EVENT); + } + + Type value; + if (!ParseScalar((const char *)event.data.scalar.value, value)) { + assert(false); + } + mVector.push_back(value); + } + +private: + VectorType mVector; +}; + +} /* namespace yaml */ + +#endif /* __YAML_VECTOR_PARSER_HH__ */