diff options
author | randomdan <randomdan@localhost> | 2014-06-09 12:00:01 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-06-09 12:00:01 +0000 |
commit | c8c91494a2507213f05515bd1faa2e5cedeaadf0 (patch) | |
tree | 79112a06beb2ad979b0f7c8a8a49b7d68e4a5525 | |
parent | Hooks is now a vector, not a map, to maintain the output order according to t... (diff) | |
download | slicer-c8c91494a2507213f05515bd1faa2e5cedeaadf0.tar.bz2 slicer-c8c91494a2507213f05515bd1faa2e5cedeaadf0.tar.xz slicer-c8c91494a2507213f05515bd1faa2e5cedeaadf0.zip |
Move the name into the hook
-rw-r--r-- | slicer/slicer/modelParts.h | 27 | ||||
-rw-r--r-- | slicer/slicer/parser.cpp | 26 |
2 files changed, 34 insertions, 19 deletions
diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index b845348..9c2bf45 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -160,40 +160,55 @@ namespace Slicer { class HookBase : public IceUtil::Shared { public: virtual ModelPartPtr Get(T * t) const = 0; + + virtual std::string PartName() const = 0; }; typedef IceUtil::Handle<HookBase> HookPtr; template <typename MT, MT T::*M, typename MP> class Hook : public HookBase { public: + Hook(const std::string & n) : + name(n) + { + } + ModelPartPtr Get(T * t) const override { return t ? new MP(t->*M) : NULL; } + + std::string PartName() const override + { + return name; + } + + private: + const std::string name; }; virtual void OnEachChild(const ChildHandler & ch) { - for (auto h = hooks.begin(); h != hooks.end(); h++) { - auto modelPart = h->second->Get(GetModel()); - ch(h->first, modelPart && modelPart->HasValue() ? modelPart : ModelPartPtr()); + BOOST_FOREACH (const auto & h, hooks) { + auto modelPart = h->Get(GetModel()); + ch(h->PartName(), modelPart && modelPart->HasValue() ? modelPart : ModelPartPtr()); } } ModelPartPtr GetChild(const std::string & name) override { auto childitr = std::find_if(hooks.begin(), hooks.end(), [&name](const typename Hooks::value_type & h) { - return h.first == name; + return h->PartName() == name; }); if (childitr != hooks.end()) { - return childitr->second->Get(GetModel()); + return (*childitr)->Get(GetModel()); } return NULL; } virtual T * GetModel() = 0; - typedef std::vector<std::pair<const std::string, HookPtr> > Hooks; + typedef std::vector<HookPtr> Hooks; private: static Hooks hooks; diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index 680ca2f..c874f57 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -55,8 +55,7 @@ namespace Slicer { modulePath().c_str(), c->name().c_str()); BOOST_FOREACH (const auto & dm, c->allDataMembers()) { auto name = metaDataValue("slicer:name:", dm->getMetaData()); - fprintf(cpp, "\t\t{ \"%s\", ", - name ? name->c_str() : dm->name().c_str()); + fprintf(cpp, "\t\t{ "); auto type = dm->type(); fprintf(cpp, "new ModelPartForClass< %s::%sPtr >::Hook< ", modulePath().c_str(), c->name().c_str()); @@ -79,7 +78,8 @@ namespace Slicer { if (dm->optional()) { fprintf(cpp, " > "); } - fprintf(cpp, " > >() },\n"); + fprintf(cpp, " > >(\"%s\") },\n", + name ? name->c_str() : dm->name().c_str()); } fprintf(cpp, "\t};\n"); @@ -104,16 +104,16 @@ namespace Slicer { modulePath().c_str(), c->name().c_str()); BOOST_FOREACH (const auto & dm, c->dataMembers()) { auto name = metaDataValue("slicer:name:", dm->getMetaData()); - fprintf(cpp, "\t\t{ \"%s\", ", - name ? name->c_str() : dm->name().c_str()); + fprintf(cpp, "\t\t{ "); auto type = dm->type(); fprintf(cpp, "new ModelPartForStruct< %s::%s >::Hook< %s, &%s::%s::%s, ", modulePath().c_str(), c->name().c_str(), Slice::typeToString(type).c_str(), modulePath().c_str(), c->name().c_str(), dm->name().c_str()); createNewModelPartPtrFor(type); - fprintf(cpp, " < %s > >() },\n", - Slice::typeToString(type).c_str()); + fprintf(cpp, "< %s > >(\"%s\") },\n", + Slice::typeToString(type).c_str(), + name ? name->c_str() : dm->name().c_str()); } fprintf(cpp, "\t};\n\n"); @@ -179,25 +179,25 @@ namespace Slicer { modulePath().c_str(), d->name().c_str()); auto kname = metaDataValue("slicer:key:", d->getMetaData()); auto vname = metaDataValue("slicer:value:", d->getMetaData()); - fprintf(cpp, "\t\t{ \"%s\", ", - kname ? kname->c_str() : "key"); + fprintf(cpp, "\t\t{ "); auto ktype = d->keyType(); fprintf(cpp, "new ModelPartForDictionaryElement< %s::%s >::Hook< %s*, &ModelPartForDictionaryElement< %s::%s >::key, ", modulePath().c_str(), d->name().c_str(), Slice::typeToString(ktype).c_str(), modulePath().c_str(), d->name().c_str()); createNewModelPartPtrFor(ktype); - fprintf(cpp, "< %s > >() },\n\t\t{ \"%s\", ", + fprintf(cpp, "< %s > >(\"%s\") },\n\t\t{ ", Slice::typeToString(ktype).c_str(), - vname ? vname->c_str() : "value"); + kname ? kname->c_str() : "key"); auto vtype = d->valueType(); fprintf(cpp, "new ModelPartForDictionaryElement< %s::%s >::Hook< %s*, &ModelPartForDictionaryElement< %s::%s >::value, ", modulePath().c_str(), d->name().c_str(), Slice::typeToString(vtype).c_str(), modulePath().c_str(), d->name().c_str()); createNewModelPartPtrFor(vtype); - fprintf(cpp, "< %s > >() },\n", - Slice::typeToString(vtype).c_str()); + fprintf(cpp, "< %s > >(\"%s\") },\n", + Slice::typeToString(vtype).c_str(), + vname ? vname->c_str() : "value"); fprintf(cpp, "\t};\n"); fprintf(cpp, "\n"); } |