From cdcad43ad8b497bd73df611ae53e1dfd5257400e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 12 Jun 2019 15:37:02 +0300 Subject: [PATCH] Use LoadableTypeRegistry to manage action types in Sequencer --- source/sequencer.cpp | 22 +++++------- source/sequencer.h | 79 ++++++++++---------------------------------- 2 files changed, 27 insertions(+), 74 deletions(-) diff --git a/source/sequencer.cpp b/source/sequencer.cpp index b4969bb..25182b8 100644 --- a/source/sequencer.cpp +++ b/source/sequencer.cpp @@ -189,26 +189,22 @@ Sequencer::ActionDefLoader::ActionDefLoader(Sequencer &s, Demo &d): DataFile::ObjectLoader(s), demo(d) { - for(const auto &t: obj.action_types) - add(t.first, t.second->get_def_loader_func()); -} - -void Sequencer::ActionDefLoader::finished() -{ - if(action) - action->validate(); + obj.action_registry.add_all(*this); } Sequencer::SegmentLoader::SegmentLoader(Sequencer &s, float b, float e, Demo &d): - ObjectLoader(s), + ActionDefLoader(s, d), start_beat(b), - end_beat(e), - demo(d) + end_beat(e) { add("apply", &SegmentLoader::apply); - for(const auto &t: obj.action_types) - add(t.first, t.second->get_loader_func()); +} + +void Sequencer::SegmentLoader::action_loaded() +{ + obj.add_action(*action, start_beat, end_beat); + obj.anonymous_actions.push_back(action.release()); } void Sequencer::SegmentLoader::apply(const string &n) diff --git a/source/sequencer.h b/source/sequencer.h index cb80f8f..d01ee68 100644 --- a/source/sequencer.h +++ b/source/sequencer.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -36,31 +37,15 @@ public: }; private: - class ActionDefLoader; - class SegmentLoader; - - class RegisteredAction - { - public: - typedef void (ActionDefLoader::*DefLoaderFunc)(); - typedef void (SegmentLoader::*LoaderFunc)(); - - virtual ~RegisteredAction() { } - virtual DefLoaderFunc get_def_loader_func() const = 0; - virtual LoaderFunc get_loader_func() const = 0; - }; - - template - class RegisteredActionType: public RegisteredAction - { - public: - virtual DefLoaderFunc get_def_loader_func() const; - virtual LoaderFunc get_loader_func() const; - }; - class ActionDefLoader: public Msp::DataFile::ObjectLoader { - private: + protected: + template + struct AddAction + { + static void add(ActionDefLoader &ldr, const std::string &kw) { ldr.add(kw, &ActionDefLoader::action_def); } + }; + Demo &demo; Msp::RefPtr action; @@ -69,14 +54,14 @@ private: Action *get_action() { return action.release(); } - private: - virtual void finished(); + protected: + virtual void action_loaded() { } + private: template void action_def(); - template - friend class RegisteredActionType; + friend class Sequencer; }; struct Segment @@ -86,31 +71,26 @@ private: float end_beat; }; - class SegmentLoader: public Msp::DataFile::ObjectLoader + class SegmentLoader: public ActionDefLoader { private: float start_beat; float end_beat; - Demo &demo; public: SegmentLoader(Sequencer &, float, float, Demo &); private: - template - void action(); + virtual void action_loaded(); void apply(const std::string &); - - template - friend class RegisteredActionType; }; public: sigc::signal signal_finished; private: - std::map action_types; + DataFile::LoadableTypeRegistry action_registry; std::map named_actions; std::vector anonymous_actions; @@ -147,22 +127,7 @@ public: template inline void Sequencer::register_action_type(const std::string &n) { - if(action_types.count(n)) - throw Msp::key_error(n); - - action_types[n] = new RegisteredActionType; -} - -template -Sequencer::RegisteredAction::DefLoaderFunc Sequencer::RegisteredActionType::get_def_loader_func() const -{ - return &ActionDefLoader::action_def; -} - -template -Sequencer::RegisteredAction::LoaderFunc Sequencer::RegisteredActionType::get_loader_func() const -{ - return &SegmentLoader::action; + action_registry.register_type(n); } template @@ -171,19 +136,11 @@ void Sequencer::ActionDefLoader::action_def() if(action) throw std::runtime_error("Only one action per definition is allowed"); - Msp::RefPtr act = new T; - load_sub(*act, demo); - action = act; -} - -template -void Sequencer::SegmentLoader::action() -{ Msp::RefPtr act = new T; load_sub(*act, demo); act->validate(); - obj.add_action(*act, start_beat, end_beat); - obj.anonymous_actions.push_back(act.release()); + action = act.release(); + action_loaded(); } } // namespace DemoScene -- 2.45.2