diff options
author | David Robillard <d@drobilla.net> | 2013-01-12 23:38:03 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2013-01-12 23:38:03 +0000 |
commit | 1dad5b5aaa139993fe19e266d08dfc55844e6804 (patch) | |
tree | fd2bed5971853b429f1b74369a778a4d608e6925 /src/engine/machina | |
parent | 8f048287d06afd7d3c2e90f4a503d7666a9cb6fa (diff) | |
download | machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.tar.gz machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.tar.bz2 machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.zip |
Remove Raul::SharedPtr and switch to std::shared_ptr.
Use project local short type aliases for shared_ptr and friends.
Move Raul::Disposable and Raul::Manageable into Raul::Maid.
Use sets to store machina nodes and edges to avoid O(n) searches.
git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4939 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine/machina')
-rw-r--r-- | src/engine/machina/Context.hpp | 2 | ||||
-rw-r--r-- | src/engine/machina/Controller.hpp | 22 | ||||
-rw-r--r-- | src/engine/machina/Driver.hpp | 16 | ||||
-rw-r--r-- | src/engine/machina/Engine.hpp | 38 | ||||
-rw-r--r-- | src/engine/machina/Evolver.hpp | 19 | ||||
-rw-r--r-- | src/engine/machina/Loader.hpp | 5 | ||||
-rw-r--r-- | src/engine/machina/Machine.hpp | 53 | ||||
-rw-r--r-- | src/engine/machina/Updates.hpp | 17 | ||||
-rw-r--r-- | src/engine/machina/types.hpp | 26 |
9 files changed, 111 insertions, 87 deletions
diff --git a/src/engine/machina/Context.hpp b/src/engine/machina/Context.hpp index 8f86602..8195a0e 100644 --- a/src/engine/machina/Context.hpp +++ b/src/engine/machina/Context.hpp @@ -18,8 +18,8 @@ #ifndef MACHINA_CONTEXT_HPP #define MACHINA_CONTEXT_HPP +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeSlice.hpp" namespace Machina { diff --git a/src/engine/machina/Controller.hpp b/src/engine/machina/Controller.hpp index ddd1d4c..eb04c59 100644 --- a/src/engine/machina/Controller.hpp +++ b/src/engine/machina/Controller.hpp @@ -22,8 +22,8 @@ #include <set> -#include "raul/SharedPtr.hpp" #include "raul/RingBuffer.hpp" +#include "raul/Maid.hpp" #include "machina/types.hpp" #include "machina/URIs.hpp" @@ -31,7 +31,7 @@ #include "Stateful.hpp" namespace Raul { -class Atom; class Maid; +class Atom; } namespace Machina { @@ -47,7 +47,7 @@ class ClientModel; class ClientObject; class Controller { public: - Controller(SharedPtr<Engine> engine, Client::ClientModel& client_model); + Controller(SPtr<Engine> engine, Client::ClientModel& client_model); uint64_t create(const Client::ClientObject& obj); uint64_t connect(uint64_t tail_id, uint64_t head_id); @@ -56,32 +56,32 @@ public: URIInt key, const Raul::Atom& value); - void learn(SharedPtr<Raul::Maid> maid, uint64_t node_id); + void learn(SPtr<Raul::Maid> maid, uint64_t node_id); void disconnect(uint64_t tail_id, uint64_t head_id); void erase(uint64_t id); - void announce(SharedPtr<Machine> machine); + void announce(SPtr<Machine> machine); void process_updates(); private: - SharedPtr<Stateful> find(uint64_t id); + SPtr<Stateful> find(uint64_t id); struct StatefulComparator { - inline bool operator()(SharedPtr<Stateful> lhs, - SharedPtr<Stateful> rhs) const { + inline bool operator()(SPtr<Stateful> lhs, + SPtr<Stateful> rhs) const { return lhs->id() < rhs->id(); } }; - typedef std::set<SharedPtr<Stateful>, StatefulComparator> Objects; + typedef std::set<SPtr<Stateful>, StatefulComparator> Objects; Objects _objects; - SharedPtr<Engine> _engine; + SPtr<Engine> _engine; Client::ClientModel& _client_model; - SharedPtr<UpdateBuffer> _updates; + SPtr<UpdateBuffer> _updates; }; } diff --git a/src/engine/machina/Driver.hpp b/src/engine/machina/Driver.hpp index 778fd22..ea0bf8f 100644 --- a/src/engine/machina/Driver.hpp +++ b/src/engine/machina/Driver.hpp @@ -32,22 +32,22 @@ class Driver : public MIDISink { public: - Driver(Raul::Forge& forge, SharedPtr<Machine> machine) + Driver(Raul::Forge& forge, SPtr<Machine> machine) : _forge(forge) , _machine(machine) {} virtual ~Driver() {} - SharedPtr<Machine> machine() { return _machine; } + SPtr<Machine> machine() { return _machine; } - virtual void set_machine(SharedPtr<Machine> machine) { + virtual void set_machine(SPtr<Machine> machine) { _machine = machine; } - SharedPtr<UpdateBuffer> update_sink() { return _updates; } + SPtr<UpdateBuffer> update_sink() { return _updates; } - void set_update_sink(SharedPtr<UpdateBuffer> b) { + void set_update_sink(SPtr<UpdateBuffer> b) { _updates = b; } @@ -65,9 +65,9 @@ public: virtual void finish_record() {} protected: - Raul::Forge& _forge; - SharedPtr<Machine> _machine; - SharedPtr<UpdateBuffer> _updates; + Raul::Forge& _forge; + SPtr<Machine> _machine; + SPtr<UpdateBuffer> _updates; }; } // namespace Machina diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index 7040082..1c8d6ae 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -23,8 +23,8 @@ #include <glibmm/ustring.h> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "machina/Driver.hpp" #include "machina/Loader.hpp" @@ -35,26 +35,26 @@ class Machine; class Engine { public: - Engine(Raul::Forge& forge, - SharedPtr<Driver> driver, - Sord::World& rdf_world); + Engine(Raul::Forge& forge, + SPtr<Driver> driver, + Sord::World& rdf_world); Sord::World& rdf_world() { return _rdf_world; } - static SharedPtr<Driver> new_driver(Raul::Forge& forge, - const std::string& name, - SharedPtr<Machine> machine); + static SPtr<Driver> new_driver(Raul::Forge& forge, + const std::string& name, + SPtr<Machine> machine); - SharedPtr<Driver> driver() { return _driver; } - SharedPtr<Machine> machine() { return _driver->machine(); } - Raul::Forge& forge() { return _forge; } + SPtr<Driver> driver() { return _driver; } + SPtr<Machine> machine() { return _driver->machine(); } + Raul::Forge& forge() { return _forge; } - SharedPtr<Machine> load_machine(const Glib::ustring& uri); - SharedPtr<Machine> load_machine_midi(const Glib::ustring& uri, - double q, - Raul::TimeDuration dur); + SPtr<Machine> load_machine(const Glib::ustring& uri); + SPtr<Machine> load_machine_midi(const Glib::ustring& uri, + double q, + Raul::TimeDuration dur); - void import_machine(SharedPtr<Machine> machine); + void import_machine(SPtr<Machine> machine); void export_midi(const Glib::ustring& filename, Raul::TimeDuration dur); @@ -63,10 +63,10 @@ public: void set_quantization(double beat_fraction); private: - SharedPtr<Driver> _driver; - Sord::World& _rdf_world; - Loader _loader; - Raul::Forge _forge; + SPtr<Driver> _driver; + Sord::World& _rdf_world; + Loader _loader; + Raul::Forge _forge; }; } // namespace Machina diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp index 1aec3e8..7c5504e 100644 --- a/src/engine/machina/Evolver.hpp +++ b/src/engine/machina/Evolver.hpp @@ -19,7 +19,7 @@ #define MACHINA_EVOLVER_HPP #include "eugene/GAImpl.hpp" -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "raul/Thread.hpp" #include "raul/TimeStamp.hpp" @@ -39,23 +39,24 @@ class Evolver : public Raul::Thread { public: - Evolver(Raul::TimeUnit unit, const string& target_midi, - SharedPtr<Machine> seed); + Evolver(Raul::TimeUnit unit, + const string& target_midi, + SPtr<Machine> seed); - void seed(SharedPtr<Machine> parent); + void seed(SPtr<Machine> parent); bool improvement() { return _improvement; } - SharedPtr<const Machine> best() { return _ga->best(); } + SPtr<const Machine> best() { return _ga->best(); } typedef Eugene::GAImpl<Machine> MachinaGA; private: void _run(); - SharedPtr<MachinaGA> _ga; - SharedPtr<Problem> _problem; - float _seed_fitness; - Schrodinbit _improvement; + SPtr<MachinaGA> _ga; + SPtr<Problem> _problem; + float _seed_fitness; + Schrodinbit _improvement; }; } // namespace Machina diff --git a/src/engine/machina/Loader.hpp b/src/engine/machina/Loader.hpp index f97618a..665f7b7 100644 --- a/src/engine/machina/Loader.hpp +++ b/src/engine/machina/Loader.hpp @@ -20,9 +20,8 @@ #include <glibmm/ustring.h> +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" - #include "sord/sordmm.hpp" using Sord::Namespaces; @@ -36,7 +35,7 @@ class Loader public: Loader(Raul::Forge& forge, Sord::World& rdf_world); - SharedPtr<Machine> load(const Glib::ustring& filename); + SPtr<Machine> load(const Glib::ustring& filename); private: Raul::Forge& _forge; diff --git a/src/engine/machina/Machine.hpp b/src/engine/machina/Machine.hpp index acfc599..1a8f267 100644 --- a/src/engine/machina/Machine.hpp +++ b/src/engine/machina/Machine.hpp @@ -19,15 +19,13 @@ #define MACHINA_MACHINE_HPP #include <vector> +#include <set> #include <boost/utility.hpp> +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/Maid.hpp" #include "raul/RingBuffer.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeSlice.hpp" -#include "raul/WeakPtr.hpp" - #include "sord/sordmm.hpp" #include "types.hpp" @@ -61,49 +59,50 @@ public: bool is_finished() { return _is_finished; } bool is_activated() { return _is_activated; } - void add_node(SharedPtr<Node> node); - void remove_node(SharedPtr<Node> node); - void learn(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node); + void add_node(SPtr<Node> node); + void remove_node(SPtr<Node> node); + void learn(SPtr<Raul::Maid> maid, SPtr<Node> node); void write_state(Sord::Model& model); // Audio context void reset(MIDISink* sink, Raul::TimeStamp time); - uint32_t run(Context& context, SharedPtr<UpdateBuffer> updates); + uint32_t run(Context& context, SPtr<UpdateBuffer> updates); // Any context inline Raul::TimeStamp time() const { return _time; } - SharedPtr<LearnRequest> pending_learn() { return _pending_learn; } - void clear_pending_learn() { _pending_learn.reset(); } + SPtr<LearnRequest> pending_learn() { return _pending_learn; } + void clear_pending_learn() { _pending_learn.reset(); } - typedef std::list< SharedPtr<Node> > Nodes; + typedef std::set< SPtr<Node> > Nodes; Nodes& nodes() { return _nodes; } const Nodes& nodes() const { return _nodes; } - SharedPtr<Node> random_node(); - SharedPtr<Edge> random_edge(); + SPtr<Node> random_node(); + SPtr<Edge> random_edge(); private: // Audio context - SharedPtr<Node> earliest_node() const; + SPtr<Node> earliest_node() const; + + bool enter_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates); - bool enter_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates); + void exit_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates); - void exit_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates); + static const size_t MAX_ACTIVE_NODES = 128; - static const size_t MAX_ACTIVE_NODES = 128; - std::vector< SharedPtr<Node> > _active_nodes; + std::vector< SPtr<Node> > _active_nodes; - SharedPtr<LearnRequest> _pending_learn; - Nodes _nodes; - Raul::TimeStamp _time; - bool _is_activated; - bool _is_finished; + SPtr<LearnRequest> _pending_learn; + Nodes _nodes; + Raul::TimeStamp _time; + bool _is_activated; + bool _is_finished; }; } // namespace Machina diff --git a/src/engine/machina/Updates.hpp b/src/engine/machina/Updates.hpp index d2947e7..cf29886 100644 --- a/src/engine/machina/Updates.hpp +++ b/src/engine/machina/Updates.hpp @@ -21,7 +21,6 @@ #include <stdint.h> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" @@ -32,16 +31,16 @@ enum UpdateType { }; void -write_set(SharedPtr<UpdateBuffer> buf, - uint64_t subject, - URIInt key, - const Raul::Atom& value); +write_set(SPtr<UpdateBuffer> buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value); uint32_t -read_set(SharedPtr<UpdateBuffer> buf, - uint64_t* subject, - URIInt* key, - Raul::Atom* value); +read_set(SPtr<UpdateBuffer> buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value); } // namespace Machina diff --git a/src/engine/machina/types.hpp b/src/engine/machina/types.hpp index 7603d3c..d71e144 100644 --- a/src/engine/machina/types.hpp +++ b/src/engine/machina/types.hpp @@ -18,6 +18,8 @@ #ifndef MACHINA_TYPES_HPP #define MACHINA_TYPES_HPP +#include <memory> + #include "raul/RingBuffer.hpp" namespace Machina { @@ -28,6 +30,30 @@ typedef Raul::RingBuffer UpdateBuffer; typedef uint32_t URIInt; +template <class T> +using SPtr = std::shared_ptr<T>; + +template <class T> +using WPtr = std::weak_ptr<T>; + +template <class T> +void NullDeleter(T* ptr) {} + +template<class T, class U> +SPtr<T> static_ptr_cast(const SPtr<U>& r) { + return std::static_pointer_cast<T>(r); +} + +template<class T, class U> +SPtr<T> dynamic_ptr_cast(const SPtr<U>& r) { + return std::dynamic_pointer_cast<T>(r); +} + +template<class T, class U> +SPtr<T> const_ptr_cast(const SPtr<U>& r) { + return std::const_pointer_cast<T>(r); +} + } // namespace Machina #endif // MACHINA_TYPES_HPP |