2014-07-15 23:31:48 -07:00
|
|
|
/* vector_parser.hh
|
|
|
|
* vim: set tw=80:
|
|
|
|
* Eryn Wells <eryn@erynwells.me>
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* 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 <cstdio>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "parsers.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a YAML sequence of homogeneous scalars into a vector of the templated
|
|
|
|
* type.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2014-07-19 17:17:33 -07:00
|
|
|
struct ScalarSequenceParser
|
2014-07-15 23:31:48 -07:00
|
|
|
: public UtilityParser<std::vector<T> >
|
|
|
|
{
|
|
|
|
typedef T Type;
|
|
|
|
typedef std::vector<T> VectorType;
|
2014-07-19 17:17:33 -07:00
|
|
|
typedef typename UtilityParser<VectorType>::CallbackFunction CallbackFunction;
|
|
|
|
|
|
|
|
ScalarSequenceParser(Scene& scene,
|
|
|
|
ParserStack& parsers)
|
|
|
|
: UtilityParser<VectorType>(scene, parsers),
|
|
|
|
mVector()
|
|
|
|
{ }
|
2014-07-15 23:31:48 -07:00
|
|
|
|
|
|
|
/** Constructor */
|
2014-07-19 17:17:33 -07:00
|
|
|
ScalarSequenceParser(Scene& scene,
|
|
|
|
ParserStack& parsers,
|
|
|
|
CallbackFunction callback)
|
2014-07-15 23:31:48 -07:00
|
|
|
: UtilityParser<VectorType>(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;
|
2014-07-19 11:40:02 -07:00
|
|
|
std::string valueString((char*)event.data.scalar.value,
|
|
|
|
event.data.scalar.length);
|
|
|
|
if (!ParseScalar<Type>(valueString, value)) {
|
2014-07-15 23:31:48 -07:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
mVector.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
VectorType mVector;
|
|
|
|
};
|
|
|
|
|
2014-07-19 17:17:33 -07:00
|
|
|
|
|
|
|
struct Vector3Parser
|
|
|
|
: ScalarSequenceParser<double>
|
|
|
|
{
|
|
|
|
typedef std::function<void (Vector3)> CallbackFunction;
|
|
|
|
|
|
|
|
Vector3Parser(Scene& scene, ParserStack& parsers, CallbackFunction onDone);
|
|
|
|
~Vector3Parser();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct ColorParser
|
|
|
|
: ScalarSequenceParser<double>
|
|
|
|
{
|
|
|
|
typedef std::function<void (Color)> CallbackFunction;
|
|
|
|
|
|
|
|
ColorParser(Scene& scene, ParserStack& parsers, CallbackFunction onDone);
|
|
|
|
~ColorParser();
|
|
|
|
};
|
|
|
|
|
2014-07-15 23:31:48 -07:00
|
|
|
} /* namespace yaml */
|
|
|
|
|
|
|
|
#endif /* __YAML_VECTOR_PARSER_HH__ */
|