Fixed up ScalarSequenceParser

Also, Vector3Parser and ColorParser. Go me!
This commit is contained in:
Eryn Wells 2014-07-28 17:21:49 -07:00
parent 1f51ba2dd7
commit a393a8fb5a
3 changed files with 74 additions and 62 deletions

View file

@ -196,7 +196,6 @@ private:
};
#if 0
/**
* UtilityParsers handle small reusable bits of YAML. Their constructors take a
* C++11 lambda, which will be called back with the result of the parsed data.
@ -205,16 +204,16 @@ template<typename T>
struct UtilityParser
: public Parser
{
typedef std::function<void (T)> CallbackFunction;
typedef std::function<void(T)> CallbackFunction;
UtilityParser(Scene& scene,
ParserStack& parsers)
Parser::Stack& parsers)
: Parser(scene, parsers),
mCallback()
{ }
UtilityParser(Scene& scene,
ParserStack& parsers,
Parser::Stack& parsers,
CallbackFunction callback)
: Parser(scene, parsers),
mCallback(callback)
@ -243,17 +242,6 @@ private:
};
/**
* Defines traits for the ParseScalar function. In particular, defines the
* format strings for supported scalar types.
*/
template<typename T>
struct ScalarParserTraits
{
static const char* fmt;
};
/**
* Parse a YAML scalar value into a native datatype.
*
@ -269,7 +257,6 @@ ParseScalar(const std::string& scalar,
std::stringstream s(scalar);
return bool(s >> value);
}
#endif
} /* namespace yaml */
} /* namespace charles */

View file

@ -8,10 +8,49 @@
#include "yaml/vectorParser.hh"
namespace charles {
namespace yaml {
template<typename T>
bool
ScalarSequenceParser<T>::HandleScalar(const std::string& anchor,
const std::string& tag,
const std::string& value,
bool plainImplicit,
bool quotedImplicit,
Parser::ScalarStyle style,
const Parser::Mark& startMark,
const Parser::Mark& endMark)
{
Type scalarValue;
if (!ParseScalar<Type>(value, scalarValue)) {
assert(false);
return false;
}
mVector.push_back(scalarValue);
return true;
}
template<typename T>
bool
ScalarSequenceParser<T>::HandleSequenceEnd(const Parser::Mark& startMark,
const Parser::Mark& endMark)
{
/*
* 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 true;
}
Vector3Parser::Vector3Parser(Scene& scene,
ParserStack& parsers,
Parser::Stack& parsers,
CallbackFunction onDone)
: ScalarSequenceParser(scene, parsers)
{
@ -26,12 +65,8 @@ Vector3Parser::Vector3Parser(Scene& scene,
}
Vector3Parser::~Vector3Parser()
{ }
ColorParser::ColorParser(Scene& scene,
ParserStack& parsers,
Parser::Stack& parsers,
CallbackFunction onDone)
: ScalarSequenceParser(scene, parsers)
{
@ -50,8 +85,5 @@ ColorParser::ColorParser(Scene& scene,
SetCallback(onSeqDone);
}
ColorParser::~ColorParser()
{ }
} /* namespace yaml */
} /* namespace charles */

View file

@ -16,6 +16,7 @@
#include "parsers.hh"
namespace charles {
namespace yaml {
/**
@ -24,57 +25,49 @@ namespace yaml {
*/
template<typename T>
struct ScalarSequenceParser
: public UtilityParser<std::vector<T> >
: public UtilityParser<std::vector<T>>
{
typedef T Type;
typedef std::vector<T> VectorType;
typedef typename UtilityParser<VectorType>::CallbackFunction CallbackFunction;
ScalarSequenceParser(Scene& scene,
ParserStack& parsers)
Parser::Stack& parsers)
: UtilityParser<VectorType>(scene, parsers),
mVector()
{ }
/** Constructor */
ScalarSequenceParser(Scene& scene,
ParserStack& parsers,
Parser::Stack& parsers,
CallbackFunction callback)
: UtilityParser<VectorType>(scene, parsers, callback),
mVector()
{ }
/**
* Handle a YAML parser event.
*
* @param [in] event The parser event
*/
void
HandleEvent(yaml_event_t& event)
bool HandleScalar(const std::string& anchor,
const std::string& tag,
const std::string& value,
bool plainImplicit,
bool quotedImplicit,
Parser::ScalarStyle style,
const Parser::Mark& startMark,
const Parser::Mark& endMark);
bool
HandleSequenceEnd(const Parser::Mark& startMark,
const Parser::Mark& endMark)
{
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);
/*
* 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;
}
/* We have a completed vector. Notify the caller. */
this->Notify(mVector);
if (event.type != YAML_SCALAR_EVENT) {
assert(event.type != YAML_SCALAR_EVENT);
}
Type value;
std::string valueString((char*)event.data.scalar.value,
event.data.scalar.length);
if (!ParseScalar<Type>(valueString, value)) {
assert(false);
}
mVector.push_back(value);
return true;
}
private:
@ -85,22 +78,22 @@ private:
struct Vector3Parser
: ScalarSequenceParser<double>
{
typedef std::function<void (Vector3)> CallbackFunction;
typedef std::function<void(Vector3)> CallbackFunction;
Vector3Parser(Scene& scene, ParserStack& parsers, CallbackFunction onDone);
~Vector3Parser();
Vector3Parser(Scene& scene, Parser::Stack& parsers, CallbackFunction onDone);
};
struct ColorParser
: ScalarSequenceParser<double>
{
typedef std::function<void (Color)> CallbackFunction;
typedef std::function<void(Color)> CallbackFunction;
ColorParser(Scene& scene, ParserStack& parsers, CallbackFunction onDone);
ColorParser(Scene& scene, Parser::Stack& parsers, CallbackFunction onDone);
~ColorParser();
};
} /* namespace yaml */
} /* namespace charles */
#endif /* __YAML_VECTOR_PARSER_HH__ */