From b2b5116d8c3451f4f5699e328b46beea6c994d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Gl=C3=B6ckner?= Date: Fri, 22 Jan 2016 12:55:09 +0100 Subject: improved engine factory structure --- drumgizmo/drumgizmoc.cc | 20 +++++++++++++---- drumgizmo/enginefactory.cc | 56 +++++++++++++++++++++++++++++++--------------- drumgizmo/enginefactory.h | 54 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 101 insertions(+), 29 deletions(-) diff --git a/drumgizmo/drumgizmoc.cc b/drumgizmo/drumgizmoc.cc index 14ebe64..25012f8 100644 --- a/drumgizmo/drumgizmoc.cc +++ b/drumgizmo/drumgizmoc.cc @@ -24,6 +24,8 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +#include + #include #include @@ -226,9 +228,19 @@ int CliMain::run(int argc, char *argv[]) return 1; } - JackClientPtr client{nullptr}; - - auto ie = createInputEngine(client, inputengine); + EngineFactory factory; + std::cout << "Available Input Engines = { "; + for (auto const & name: factory.getInputEngines()) { + std::cout << name << " "; + } + std::cout << "}\n"; + std::cout << "Available Output Engines = { "; + for (auto const & name: factory.getOutputEngines()) { + std::cout << name << " "; + } + std::cout << "}\n"; + + auto ie = factory.createInput(inputengine); if(ie == NULL) { printf("Invalid input engine: %s\n", inputengine.c_str()); @@ -267,7 +279,7 @@ int CliMain::run(int argc, char *argv[]) return 1; } - auto oe = createOutputEngine(client, outputengine); + auto oe = factory.createOutput(outputengine); if(oe == NULL) { printf("Invalid output engine: %s\n", outputengine.c_str()); diff --git a/drumgizmo/enginefactory.cc b/drumgizmo/enginefactory.cc index 8c40a28..121abdd 100644 --- a/drumgizmo/enginefactory.cc +++ b/drumgizmo/enginefactory.cc @@ -29,31 +29,53 @@ #include "enginefactory.h" #include "jackclient.h" +EngineFactory::EngineFactory() + : input{} + , output{} +#ifdef USE_JACK + , jack{nullptr} +#endif +{ + // list available input engines #ifdef HAVE_INPUT_DUMMY -#include "input/inputdummy.h" + input.push_back("inputdummy"); #endif - #ifdef HAVE_INPUT_MIDIFILE -#include "input/midifile.h" + input.push_back("midifile"); #endif - + + // list available output engines #ifdef HAVE_OUTPUT_DUMMY -#include "output/outputdummy.h" + output.push_back("outputdummy"); #endif - #ifdef HAVE_OUTPUT_WAVFILE -#include "output/wavfile.h" + output.push_back("wavfile"); #endif - #ifdef HAVE_OUTPUT_ALSA -#include "output/alsa.h" + output.push_back("alsa"); #endif - #ifdef HAVE_OUTPUT_JACKAUDIO -#include "output/jackaudio.h" + output.push_back("jackaudio"); +#endif +} + +#ifdef USE_JACK +void EngineFactory::prepareJack() { + if (jack == nullptr) { + jack = std::make_unique(); + } +} #endif -InputEnginePtr createInputEngine(JackClientPtr& jack, std::string const & name) { +std::list const & EngineFactory::getInputEngines() const { + return input; +} + +std::list const & EngineFactory::getOutputEngines() const { + return output; +} + +std::unique_ptr EngineFactory::createInput(std::string const & name) { #ifdef HAVE_INPUT_DUMMY if (name == "dummy") { return std::make_unique(); @@ -67,11 +89,11 @@ InputEnginePtr createInputEngine(JackClientPtr& jack, std::string const & name) // todo: add more engines - std::cerr << "Unsupported input engine '" << name << "'\n"; + std::cerr << "[EngineFactory] Unsupported input engine '" << name << "'\n"; return nullptr; } -OutputEnginePtr createOutputEngine(JackClientPtr& jack, std::string const & name) { +std::unique_ptr EngineFactory::createOutput(std::string const & name) { #ifdef HAVE_OUTPUT_DUMMY if (name == "dummy") { return std::make_unique(); @@ -89,15 +111,13 @@ OutputEnginePtr createOutputEngine(JackClientPtr& jack, std::string const & name #endif #ifdef HAVE_OUTPUT_JACKAUDIO if (name == "jackaudio") { - if (jack.get() == nullptr) { - jack = std::make_unique(); - } + prepareJack(); return std::make_unique(*jack); } #endif // todo: add more engines - std::cerr << "Unsupported output engine '" << name << "'\n"; + std::cerr << "[EngineFactory] Unsupported output engine '" << name << "'\n"; return nullptr; } diff --git a/drumgizmo/enginefactory.h b/drumgizmo/enginefactory.h index 93ce67b..c599040 100644 --- a/drumgizmo/enginefactory.h +++ b/drumgizmo/enginefactory.h @@ -25,18 +25,58 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #pragma once +#include +#include #include #include "cpp11fix.h" // required for c++11 #include "audioinputengine.h" #include "audiooutputengine.h" -// todo: ifdef jack enabled -#include "jackclient.h" +#if defined(HAVE_INPUT_JACKMIDI) || defined(HAVE_OUTPUT_JACKAUDIO) + #define USE_JACK + #include "jackclient.h" +#endif -using JackClientPtr = std::unique_ptr; -using InputEnginePtr = std::unique_ptr; -using OutputEnginePtr = std::unique_ptr; +#ifdef HAVE_INPUT_DUMMY + #include "input/inputdummy.h" +#endif -InputEnginePtr createInputEngine(JackClientPtr& jack, std::string const & name); -OutputEnginePtr createOutputEngine(JackClientPtr& jack, std::string const & name); +#ifdef HAVE_INPUT_MIDIFILE + #include "input/midifile.h" +#endif + +#ifdef HAVE_OUTPUT_DUMMY + #include "output/outputdummy.h" +#endif + +#ifdef HAVE_OUTPUT_WAVFILE + #include "output/wavfile.h" +#endif + +#ifdef HAVE_OUTPUT_ALSA + #include "output/alsa.h" +#endif + +#ifdef HAVE_OUTPUT_JACKAUDIO + #include "output/jackaudio.h" +#endif + +class EngineFactory { + private: + std::list input, output; // available engines +#ifdef USE_JACK + std::unique_ptr jack; + + void prepareJack(); +#endif + + public: + EngineFactory(); + + std::list const & getInputEngines() const; + std::list const & getOutputEngines() const; + + std::unique_ptr createInput(std::string const & name); + std::unique_ptr createOutput(std::string const & name); +}; -- cgit v1.2.3