summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-01-07 21:50:43 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-01-07 21:50:43 +0000
commitd17bf53da0ee69a8b2812ffceccf8f702b89beb0 (patch)
tree1452fa7794402bc14d298fca2ef047bec2878dc9
parentRemove useless typedef (diff)
downloadslicer-d17bf53da0ee69a8b2812ffceccf8f702b89beb0.tar.bz2
slicer-d17bf53da0ee69a8b2812ffceccf8f702b89beb0.tar.xz
slicer-d17bf53da0ee69a8b2812ffceccf8f702b89beb0.zip
Allow GetChild without a name (think JSON style) and require name to be correct (don't blindly accept empty string) in all other cases)
-rw-r--r--slicer/json/serializer.cpp6
-rw-r--r--slicer/slicer/modelParts.h45
-rw-r--r--slicer/slicer/parser.cpp14
3 files changed, 49 insertions, 16 deletions
diff --git a/slicer/json/serializer.cpp b/slicer/json/serializer.cpp
index 8494354..0c704a5 100644
--- a/slicer/json/serializer.cpp
+++ b/slicer/json/serializer.cpp
@@ -149,7 +149,7 @@ namespace Slicer {
{
modelPart->Create();
for (const auto & element : a) {
- auto emp = modelPart->GetChild(std::string());
+ auto emp = modelPart->GetChild();
if (emp) {
emp->Create();
boost::apply_visitor(DocumentTreeIterate(emp), *element);
@@ -256,7 +256,7 @@ namespace Slicer {
Glib::ustring doc(buffer.str());
Glib::ustring::const_iterator itr = doc.begin();
json::Value obj = json::parseValue(itr);
- auto mp = modelRoot->GetChild(std::string());
+ auto mp = modelRoot->GetChild();
boost::apply_visitor(DocumentTreeIterate(mp), obj);
}
@@ -282,7 +282,7 @@ namespace Slicer {
void
JsonValueDeserializer::Deserialize(ModelPartPtr modelRoot)
{
- auto mp = modelRoot->GetChild(std::string());
+ auto mp = modelRoot->GetChild();
boost::apply_visitor(DocumentTreeIterate(mp), value);
}
diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h
index 0985e3a..9386696 100644
--- a/slicer/slicer/modelParts.h
+++ b/slicer/slicer/modelParts.h
@@ -142,6 +142,7 @@ namespace Slicer {
virtual ~ModelPart() = default;
virtual void OnEachChild(const ChildHandler &) = 0;
+ virtual ModelPartPtr GetChild(const HookFilter & = HookFilter()) = 0;
virtual ModelPartPtr GetChild(const std::string & memberName, const HookFilter & = HookFilter()) = 0;
virtual ModelPartPtr GetSubclassModelPart(const std::string &);
virtual TypeId GetTypeId() const;
@@ -172,6 +173,7 @@ namespace Slicer {
{
}
virtual void OnEachChild(const ChildHandler &) { }
+ virtual ModelPartPtr GetChild(const HookFilter &) override { return NULL; }
virtual ModelPartPtr GetChild(const std::string &, const HookFilter &) override { return NULL; }
virtual void SetValue(ValueSourcePtr s) override { s->set(Member); }
virtual void GetValue(ValueTargetPtr s) override { s->get(Member); }
@@ -196,6 +198,7 @@ namespace Slicer {
{
}
virtual void OnEachChild(const ChildHandler &) { }
+ virtual ModelPartPtr GetChild(const HookFilter &) override { return NULL; }
virtual ModelPartPtr GetChild(const std::string &, const HookFilter &) override { return NULL; }
virtual void SetValue(ValueSourcePtr s) override;
virtual void GetValue(ValueTargetPtr s) override;
@@ -243,6 +246,13 @@ namespace Slicer {
modelPart->Create();
}
}
+ virtual ModelPartPtr GetChild(const HookFilter & flt) override
+ {
+ if (OptionalMember) {
+ return modelPart->GetChild(flt);
+ }
+ return NULL;
+ }
virtual ModelPartPtr GetChild(const std::string & name, const HookFilter & flt) override
{
if (OptionalMember) {
@@ -330,6 +340,15 @@ namespace Slicer {
}
}
+ virtual ModelPartPtr GetChild(const HookFilter & flt) override
+ {
+ for (const auto & h : hooks) {
+ if (!flt || flt(h)) {
+ return h->Get(GetModel());
+ }
+ }
+ return NULL;
+ }
ModelPartPtr GetChild(const std::string & name, const HookFilter & flt) override
{
for (const auto & h : hooks) {
@@ -448,10 +467,16 @@ namespace Slicer {
}
}
+ virtual ModelPartPtr GetChild(const HookFilter &) override
+ {
+ mp->Create();
+ return mp;
+ }
+
virtual ModelPartPtr GetChild(const std::string & name, const HookFilter &) override
{
- if (!name.empty() && name != rootName) {
- throw IncorrectElementName(rootName);
+ if (name != rootName) {
+ throw IncorrectElementName(name);
}
mp->Create();
return mp;
@@ -503,6 +528,12 @@ namespace Slicer {
}
}
+ ModelPartPtr GetChild(const HookFilter &) override
+ {
+ sequence.push_back(typename element_type::value_type());
+ return ModelPartFor(sequence.back());
+ }
+
ModelPartPtr GetChild(const std::string &, const HookFilter &) override;
virtual bool HasValue() const override { return true; }
@@ -578,10 +609,16 @@ namespace Slicer {
ch(pairName, new ModelPartForDictionaryElement<T>(const_cast<typename T::key_type *>(&pair.first), &pair.second), NULL);
}
}
+
+ ModelPartPtr GetChild(const HookFilter &) override
+ {
+ return new ModelPartForDictionaryElementInserter<T>(dictionary);
+ }
+
ModelPartPtr GetChild(const std::string & name, const HookFilter &) override
{
- if (!name.empty() && name != pairName) {
- throw IncorrectElementName(pairName);
+ if (name != pairName) {
+ throw IncorrectElementName(name);
}
return new ModelPartForDictionaryElementInserter<T>(dictionary);
}
diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp
index bf24bd6..467f0ba 100644
--- a/slicer/slicer/parser.cpp
+++ b/slicer/slicer/parser.cpp
@@ -294,7 +294,7 @@ namespace Slicer {
fprintf(cpp, "// Sequence %s\n", s->name().c_str());
fprintf(cpp, "template<>\n");
- fprintf(cpp, "ModelPartPtr ModelPartForSequence< %s >::GetChild(const std::string & name, const HookFilter &)\n{\n",
+ fprintf(cpp, "ModelPartPtr ModelPartForSequence< %s >::GetChild(const std::string & name, const HookFilter & flt)\n{\n",
s->scoped().c_str());
auto iname = metaDataValue("slicer:item:", s->getMetaData());
if (iname) {
@@ -304,19 +304,15 @@ namespace Slicer {
else {
fprintf(cpp, "\t(void)name;\n");
}
- fprintf(cpp, "\tsequence.push_back(typename element_type::value_type());\n");
- fprintf(cpp, "\treturn new ");
- auto etype = s->type();
- createNewModelPartPtrFor(etype);
- fprintf(cpp, "<typename element_type::value_type>(sequence.back());\n}\n\n");
+ fprintf(cpp, "\treturn GetChild(flt);\n}\n\n");
+
fprintf(cpp, "template<>\n");
fprintf(cpp, "ModelPartPtr\n");
fprintf(cpp, "ModelPartForSequence< %s >::elementModelPart(typename %s::value_type & e) const {\n",
s->scoped().c_str(),
s->scoped().c_str());
- fprintf(cpp, "\treturn new ");
- createNewModelPartPtrFor(etype);
- fprintf(cpp, "<typename element_type::value_type>(e);\n}\n\n");
+ fprintf(cpp, "\treturn ModelPartFor(e);\n}\n\n");
+
fprintf(cpp, "template<>\n");
auto ename = metaDataValue("slicer:element:", s->getMetaData());
fprintf(cpp, "std::string ModelPartForSequence< %s >::elementName(\"%s\");\n\n",