Replace dimensions code in SceneParser with a few lines of VectorParser magic
Neat!
This commit is contained in:
parent
bdd0afddf5
commit
2834fa45c6
2 changed files with 16 additions and 56 deletions
|
@ -10,6 +10,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "scene_parser.hh"
|
#include "scene_parser.hh"
|
||||||
|
#include "vector_parser.hh"
|
||||||
|
|
||||||
|
|
||||||
namespace yaml {
|
namespace yaml {
|
||||||
|
@ -17,8 +18,7 @@ namespace yaml {
|
||||||
SceneParser::SceneParser(Scene& scene,
|
SceneParser::SceneParser(Scene& scene,
|
||||||
ParserStack& parsers)
|
ParserStack& parsers)
|
||||||
: Parser(scene, parsers),
|
: Parser(scene, parsers),
|
||||||
mSection(SceneParser::NoSection),
|
mSection(SceneParser::NoSection)
|
||||||
mDimension(SceneParser::NoDimension)
|
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,55 +85,22 @@ SceneParser::HandleTopLevelEvent(yaml_event_t& event)
|
||||||
void
|
void
|
||||||
SceneParser::HandleDimensionsEvent(yaml_event_t& event)
|
SceneParser::HandleDimensionsEvent(yaml_event_t& event)
|
||||||
{
|
{
|
||||||
int dim;
|
auto onDone = [this](std::vector<int> dimensions) {
|
||||||
|
if (dimensions.size() < 2) {
|
||||||
|
assert(dimensions.size() < 2);
|
||||||
|
}
|
||||||
|
Scene& sc = GetScene();
|
||||||
|
sc.set_width(dimensions.at(0));
|
||||||
|
sc.set_height(dimensions.at(1));
|
||||||
|
};
|
||||||
|
|
||||||
switch (mDimension) {
|
|
||||||
case NoDimension:
|
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case YAML_SEQUENCE_START_EVENT:
|
case YAML_SEQUENCE_START_EVENT:
|
||||||
printf(" start dimensions\n");
|
GetParsers().push(new VectorParser<int>(GetScene(), GetParsers(), onDone));
|
||||||
mDimension = SceneParser::WidthDimension;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* TODO: Fail gracefully. */
|
/* TODO: Fail gracefully. */
|
||||||
abort();
|
assert(false);
|
||||||
}
|
|
||||||
break;
|
|
||||||
case WidthDimension:
|
|
||||||
switch (event.type) {
|
|
||||||
case YAML_SCALAR_EVENT:
|
|
||||||
printf(" width = %s\n", event.data.scalar.value);
|
|
||||||
sscanf((char *)event.data.scalar.value, "%d", &dim);
|
|
||||||
GetScene().set_width(dim);
|
|
||||||
mDimension = SceneParser::HeightDimension;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
/* TODO: Fail gracefully. */
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case HeightDimension:
|
|
||||||
if (event.type == YAML_SCALAR_EVENT) {
|
|
||||||
printf(" height = %s\n", event.data.scalar.value);
|
|
||||||
sscanf((char *)event.data.scalar.value, "%d", &dim);
|
|
||||||
GetScene().set_height(dim);
|
|
||||||
mDimension = SceneParser::DoneDimension;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* TODO: Fail gracefully. */
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DoneDimension:
|
|
||||||
if (event.type == YAML_SEQUENCE_END_EVENT) {
|
|
||||||
printf(" end dimensions\n");
|
|
||||||
mSection = NoSection;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
/* TODO: Fail gracefully. */
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,13 +31,6 @@ private:
|
||||||
DimensionsSection,
|
DimensionsSection,
|
||||||
ObjectsSection,
|
ObjectsSection,
|
||||||
} mSection;
|
} mSection;
|
||||||
|
|
||||||
enum {
|
|
||||||
NoDimension,
|
|
||||||
HeightDimension,
|
|
||||||
WidthDimension,
|
|
||||||
DoneDimension
|
|
||||||
} mDimension;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace yaml */
|
} /* namespace yaml */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue