libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Module.hpp
Go to the documentation of this file.
1 #ifndef MODULE_HPP
2 #define MODULE_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <memory>
22 #include <vector>
23 #include <unordered_set>
25 #include "Signal.hpp"
26 #include "Utilities.hpp"
27 #include "LateReturn.hpp"
28 #include "Timer.hpp"
29 // Not really needed here, but all collections need that for strcmp in
30 // create instance, so including this is convinient, as all collections include
31 // at least Module.hpp
32 #include <cstring>
33 
34 // Forward declaration to strip rapidxml header dependecncy
35 namespace rapidxml{
36  template <class Ch>
37  class xml_node;
38 }
39 
40 namespace AlgAudio{
41 
42 namespace Exceptions{
47  ModuleDoesNotWantToBeCreated(std::string t) : Exception(t){};
48 };
49 } // namespace Exceptions
50 
51 class ModuleTemplate;
52 class ParamTemplate;
53 class Canvas;
54 class ModuleGUI;
55 class Window;
56 class Module;
57 class ParamController;
58 class SendReplyController;
59 
61 class Bus{
62 public:
63  int GetID() const {return id;}
68  static std::shared_ptr<Bus> CreateFake();
69  ~Bus();
70 private:
71  Bus(int id);
72  int id;
73 };
74 
76 class Group{
77 public:
78  int GetID() const {return id;}
80  static LateReturn<std::shared_ptr<Group>> CreateNew(std::shared_ptr<Group> parent);
82  static LateReturn<std::shared_ptr<Group>> CreateFake(std::shared_ptr<Group> parent);
83  ~Group();
84 private:
85  Group(int id);
86  int id;
87 };
88 
89 // =================
90 
96 class Module : public DynamicallyLoadableClass, public virtual SubscriptionsManager, public std::enable_shared_from_this<Module>, public virtual TimerHandleManager{
97 public:
99 
101  Module() {};
102  Module(void (*deleter)(void*)) : DynamicallyLoadableClass(deleter) {};
103  Module(std::shared_ptr<ModuleTemplate> t) : templ(t) {};
105 
106  Module(const Module& other) = delete;
107  virtual ~Module();
108 
114  virtual void on_init() {};
116  Relay<> r;
117  on_init();
118  return r.Return();
119  };
120  virtual void on_destroy() {};
124  virtual void on_gui_build(std::shared_ptr<ModuleGUI>) {};
126  virtual void on_param_set(std::string, float) {}
127 
131  virtual std::string state_store_string() const {return "";}
132  virtual void state_load_string(std::string) {}
135 
136  /* The template this module instance is based on. */
137  std::shared_ptr<ModuleTemplate> templ;
138 
141  int sc_id = -1;
142 
145 
151  bool enabled_by_factory = false;
152 
153  class Inlet; // Forward decl
154 
155  // TODO: Common base class
156  class Outlet{
157  public:
158  std::string id;
159  std::string name;
161  // The outlet is not the owner of the buses.
162  std::list<std::weak_ptr<Bus>> buses;
163  LateReturn<> ConnectToInlet(std::shared_ptr<Inlet> i);
164  LateReturn<> DetachFromInlet(std::shared_ptr<Inlet> i);
166  static std::shared_ptr<Outlet> Create(std::string id, std::string name, std::shared_ptr<Module> mod);
168  std::cout << "Outlet freed" << std::endl;
169  }
170  private:
171  LateReturn<> SendConnections();
172  Outlet(std::string i, std::string n, std::shared_ptr<Module> m) : id(i), name(n), mod(*m.get()) {}
173  };
174  class Inlet{
175  public:
176  // If fake is set to true, this inlet will have no corresponding bus. Pointless to use, great for debugging.
177  static LateReturn<std::shared_ptr<Inlet>> Create(std::string id, std::string name, std::shared_ptr<Module> mod, bool fake = false);
178  std::string id;
179  std::string name;
181  // The inlet is the owner of a bus.
182  std::shared_ptr<Bus> bus;
183  private:
184  Inlet(std::string i, std::string n, std::shared_ptr<Module> m, std::shared_ptr<Bus> b) : id(i), name(n), mod(*m.get()), bus(b) {}
185  };
186 
190  std::shared_ptr<ModuleGUI> GetGUI() const;
191 
203  virtual std::shared_ptr<ModuleGUI> BuildGUI(std::shared_ptr<Window> parent_window);
204 
205  void DropGUI() {modulegui.reset();}
206 
209  LateReturn<> CreateIOFromTemplate(bool fake = false);
210  std::vector<std::shared_ptr<Inlet>> inlets;
211  std::vector<std::shared_ptr<Outlet>> outlets;
212 
214 
216  void ResetControllers();
217 
218  // TODO: Make this a map?
219  std::vector<std::shared_ptr<ParamController>> param_controllers;
220 
221  std::vector<std::shared_ptr<SendReplyController>> reply_controllers;
222 
224  std::shared_ptr<ParamController> GetParamControllerByID(std::string) const;
225 
227  std::shared_ptr<Inlet > GetInletByID(std::string id) const;
229  std::shared_ptr<Outlet> GetOutletByID(std::string id) const;
230 
232  std::weak_ptr<Canvas> canvas;
233 
234 protected:
235  std::shared_ptr<ModuleGUI> modulegui;
236 };
237 
238 } // namespace AlgAudio
239 
240 #endif //MODULE_HPP
std::shared_ptr< ModuleGUI > modulegui
Definition: Module.hpp:235
LateReturn ConnectToInlet(std::shared_ptr< Inlet > i)
std::string name
Definition: Module.hpp:159
std::shared_ptr< Bus > bus
Definition: Module.hpp:182
std::vector< std::shared_ptr< Inlet > > inlets
Definition: Module.hpp:210
static std::shared_ptr< Outlet > Create(std::string id, std::string name, std::shared_ptr< Module > mod)
void PrepareParamControllers()
virtual void state_load_xml(rapidxml::xml_node< char > *)
Definition: Module.hpp:134
std::vector< std::shared_ptr< ParamController > > param_controllers
Definition: Module.hpp:219
Module(std::shared_ptr< ModuleTemplate > t)
Definition: Module.hpp:103
virtual std::string state_store_string() const
Definition: Module.hpp:131
virtual void on_destroy()
Definition: Module.hpp:120
std::shared_ptr< ModuleGUI > GetGUI() const
Definition: Module.hpp:156
static LateReturn< std::shared_ptr< Bus > > CreateNew()
std::shared_ptr< Inlet > GetInletByID(std::string id) const
~Outlet()
Definition: Module.hpp:167
const Relay & Return(Types...args) const
Definition: LateReturn.hpp:437
Definition: Signal.hpp:247
std::shared_ptr< ParamController > GetParamControllerByID(std::string) const
Definition: Timer.hpp:97
LateReturn CreateIOFromTemplate(bool fake=false)
std::list< std::weak_ptr< Bus > > buses
Definition: Module.hpp:162
std::vector< std::shared_ptr< SendReplyController > > reply_controllers
Definition: Module.hpp:221
int GetID() const
Definition: Module.hpp:63
LateReturn DetachFromInlet(std::shared_ptr< Inlet > i)
std::string id
Definition: Module.hpp:178
int GetID() const
Definition: Module.hpp:78
Definition: LateReturn.hpp:37
std::weak_ptr< Canvas > canvas
Definition: Module.hpp:232
Module()
Definition: Module.hpp:101
Definition: Module.hpp:76
Point2D position_in_canvas
Definition: Module.hpp:144
virtual std::shared_ptr< ModuleGUI > BuildGUI(std::shared_ptr< Window > parent_window)
Definition: Module.hpp:96
virtual LateReturn on_init_latereturn()
Definition: Module.hpp:115
Module(void(*deleter)(void *))
Definition: Module.hpp:102
Definition: Module.hpp:61
Definition: Module.hpp:37
std::shared_ptr< Outlet > GetOutletByID(std::string id) const
static std::shared_ptr< Bus > CreateFake()
std::vector< std::shared_ptr< Outlet > > outlets
Definition: Module.hpp:211
Definition: Alertable.hpp:26
static LateReturn< std::shared_ptr< Group > > CreateNew(std::shared_ptr< Group > parent)
Definition: DynamicallyLoadableClass.hpp:29
virtual ~Module()
Module & mod
Definition: Module.hpp:160
int sc_id
Definition: Module.hpp:141
virtual void state_store_xml(rapidxml::xml_node< char > *) const
Definition: Module.hpp:133
static LateReturn< std::shared_ptr< Group > > CreateFake(std::shared_ptr< Group > parent)
LateReturn DetachFromAll()
static LateReturn< std::shared_ptr< Inlet > > Create(std::string id, std::string name, std::shared_ptr< Module > mod, bool fake=false)
std::shared_ptr< ModuleTemplate > templ
Definition: Module.hpp:137
virtual void on_gui_build(std::shared_ptr< ModuleGUI >)
Definition: Module.hpp:124
void ResetControllers()
virtual void on_param_set(std::string, float)
Definition: Module.hpp:126
Module & mod
Definition: Module.hpp:180
virtual void on_init()
Definition: Module.hpp:114
ModuleDoesNotWantToBeCreated(std::string t)
Definition: Module.hpp:47
virtual void state_load_string(std::string)
Definition: Module.hpp:132
Definition: LateReturn.hpp:35
Definition: Module.hpp:35
bool enabled_by_factory
Definition: Module.hpp:151
Definition: Exception.hpp:29
std::string id
Definition: Module.hpp:158
Definition: Module.hpp:174
std::string name
Definition: Module.hpp:179
void DropGUI()
Definition: Module.hpp:205