From 1dad5b5aaa139993fe19e266d08dfc55844e6804 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Jan 2013 23:38:03 +0000 Subject: 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 --- src/client/ClientModel.cpp | 18 ++++----- src/client/ClientModel.hpp | 14 +++---- src/client/ClientObject.hpp | 9 ++--- src/engine/Action.hpp | 10 ++--- src/engine/ActionFactory.cpp | 18 ++++----- src/engine/ActionFactory.hpp | 8 ++-- src/engine/Controller.cpp | 51 ++++++++++++------------- src/engine/Edge.cpp | 4 +- src/engine/Edge.hpp | 16 ++++---- src/engine/Engine.cpp | 44 ++++++++++----------- src/engine/Evolver.cpp | 15 ++++---- src/engine/JackDriver.cpp | 33 ++++++++-------- src/engine/JackDriver.hpp | 24 ++++++------ src/engine/LearnRequest.cpp | 8 ++-- src/engine/LearnRequest.hpp | 23 +++++------ src/engine/Loader.cpp | 37 +++++++++--------- src/engine/Machine.cpp | 80 +++++++++++++++++++-------------------- src/engine/MachineBuilder.cpp | 64 +++++++++++++++---------------- src/engine/MachineBuilder.hpp | 38 +++++++++---------- src/engine/MidiAction.cpp | 1 - src/engine/Mutation.cpp | 46 +++++++++++----------- src/engine/Node.cpp | 66 ++++++++++++++++---------------- src/engine/Node.hpp | 56 +++++++++++++++------------ src/engine/Problem.cpp | 23 +++++------ src/engine/Problem.hpp | 10 +++-- src/engine/Recorder.cpp | 7 ++-- src/engine/Recorder.hpp | 12 +++--- src/engine/SMFDriver.cpp | 47 ++++++++++++----------- src/engine/SMFDriver.hpp | 32 ++++++++-------- src/engine/Updates.cpp | 17 ++++----- src/engine/machina/Context.hpp | 2 +- src/engine/machina/Controller.hpp | 22 +++++------ src/engine/machina/Driver.hpp | 16 ++++---- src/engine/machina/Engine.hpp | 38 +++++++++---------- src/engine/machina/Evolver.hpp | 19 +++++----- src/engine/machina/Loader.hpp | 5 +-- src/engine/machina/Machine.hpp | 53 +++++++++++++------------- src/engine/machina/Updates.hpp | 17 ++++----- src/engine/machina/types.hpp | 26 +++++++++++++ src/gui/EdgeView.cpp | 14 ++++--- src/gui/EdgeView.hpp | 16 +++++--- src/gui/MachinaCanvas.cpp | 56 ++++++++++++++------------- src/gui/MachinaCanvas.hpp | 41 ++++++++++---------- src/gui/MachinaGUI.cpp | 75 ++++++++++++++++++++---------------- src/gui/MachinaGUI.hpp | 46 ++++++++++++---------- src/gui/NodePropertiesWindow.cpp | 19 ++++++---- src/gui/NodePropertiesWindow.hpp | 21 +++++----- src/gui/NodeView.cpp | 17 ++++++--- src/gui/NodeView.hpp | 30 +++++++++------ src/gui/WidgetFactory.hpp | 6 +++ src/gui/main.cpp | 13 +++---- src/machina.cpp | 2 +- src/midi2machina.cpp | 4 +- 53 files changed, 728 insertions(+), 661 deletions(-) (limited to 'src') diff --git a/src/client/ClientModel.cpp b/src/client/ClientModel.cpp index c945de7..1cc4d3e 100644 --- a/src/client/ClientModel.cpp +++ b/src/client/ClientModel.cpp @@ -20,20 +20,20 @@ namespace Machina { namespace Client { -SharedPtr +SPtr ClientModel::find(uint64_t id) { - SharedPtr key(new ClientObjectKey(id)); - Objects::iterator i = _objects.find(key); + SPtr key(new ClientObjectKey(id)); + Objects::iterator i = _objects.find(key); if (i != _objects.end()) { return *i; } else { - return SharedPtr(); + return SPtr(); } } void -ClientModel::new_object(SharedPtr object) +ClientModel::new_object(SPtr object) { _objects.insert(object); signal_new_object.emit(object); @@ -42,21 +42,21 @@ ClientModel::new_object(SharedPtr object) void ClientModel::erase_object(uint64_t id) { - SharedPtr key(new ClientObjectKey(id)); - Objects::iterator i = _objects.find(key); + SPtr key(new ClientObjectKey(id)); + Objects::iterator i = _objects.find(key); if (i == _objects.end()) { return; } signal_erase_object.emit(*i); - (*i)->set_view(SharedPtr()); + (*i)->set_view(SPtr()); _objects.erase(i); } void ClientModel::property(uint64_t id, URIInt key, const Raul::Atom& value) { - SharedPtr object = find(id); + SPtr object = find(id); if (object) { object->set(key, value); } diff --git a/src/client/ClientModel.hpp b/src/client/ClientModel.hpp index 07500db..b629a37 100644 --- a/src/client/ClientModel.hpp +++ b/src/client/ClientModel.hpp @@ -34,25 +34,25 @@ namespace Client { class ClientModel { public: - void new_object(SharedPtr object); + void new_object(SPtr object); void erase_object(uint64_t id); void property(uint64_t id, URIInt key, const Raul::Atom& value); - SharedPtr find(uint64_t id); + SPtr find(uint64_t id); - sigc::signal< void, SharedPtr > signal_new_object; - sigc::signal< void, SharedPtr > signal_erase_object; + sigc::signal< void, SPtr > signal_new_object; + sigc::signal< void, SPtr > signal_erase_object; private: struct ClientObjectComparator { - inline bool operator()(SharedPtr lhs, - SharedPtr rhs) const { + inline bool operator()(SPtr lhs, + SPtr rhs) const { return lhs->id() < rhs->id(); } }; - typedef std::set, ClientObjectComparator> Objects; + typedef std::set, ClientObjectComparator> Objects; Objects _objects; }; diff --git a/src/client/ClientObject.hpp b/src/client/ClientObject.hpp index 994a5ba..df8a1dd 100644 --- a/src/client/ClientObject.hpp +++ b/src/client/ClientObject.hpp @@ -23,7 +23,6 @@ #include #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" @@ -52,12 +51,12 @@ public: typedef std::map Properties; const Properties& properties() { return _properties; } - SharedPtr view() const { return _view; } - void set_view(SharedPtr view) { _view = view; } + SPtr view() const { return _view; } + void set_view(SPtr view) { _view = view; } private: - uint64_t _id; - SharedPtr _view; + uint64_t _id; + SPtr _view; Properties _properties; }; diff --git a/src/engine/Action.hpp b/src/engine/Action.hpp index f954a39..7397eaa 100644 --- a/src/engine/Action.hpp +++ b/src/engine/Action.hpp @@ -21,9 +21,8 @@ #include #include -#include "raul/SharedPtr.hpp" +#include "raul/Maid.hpp" #include "raul/TimeSlice.hpp" -#include "raul/Manageable.hpp" #include "machina/types.hpp" @@ -35,15 +34,14 @@ namespace Machina { /** An Action, executed on entering or exiting of a state. */ struct Action - : public Raul::Manageable - , public Stateful { + : public Raul::Maid::Manageable + , public Stateful { virtual void execute(MIDISink* sink, Raul::TimeStamp time) = 0; virtual void write_state(Sord::Model& model); }; -class PrintAction - : public Action +class PrintAction : public Action { public: PrintAction(const std::string& msg) diff --git a/src/engine/ActionFactory.cpp b/src/engine/ActionFactory.cpp index 92bc114..afcc178 100644 --- a/src/engine/ActionFactory.cpp +++ b/src/engine/ActionFactory.cpp @@ -20,18 +20,18 @@ namespace Machina { -SharedPtr -ActionFactory::copy(SharedPtr copy) +SPtr +ActionFactory::copy(SPtr copy) { - SharedPtr ma = PtrCast(copy); + SPtr ma = dynamic_ptr_cast(copy); if (ma) { - return SharedPtr(new MidiAction(ma->event_size(), ma->event())); + return SPtr(new MidiAction(ma->event_size(), ma->event())); } else { - return SharedPtr(); + return SPtr(); } } -SharedPtr +SPtr ActionFactory::note_on(unsigned char note) { unsigned char buf[3]; @@ -39,10 +39,10 @@ ActionFactory::note_on(unsigned char note) buf[1] = note; buf[2] = 0x40; - return SharedPtr(new MidiAction(3, buf)); + return SPtr(new MidiAction(3, buf)); } -SharedPtr +SPtr ActionFactory::note_off(unsigned char note) { unsigned char buf[3]; @@ -50,7 +50,7 @@ ActionFactory::note_off(unsigned char note) buf[1] = note; buf[2] = 0x40; - return SharedPtr(new MidiAction(3, buf)); + return SPtr(new MidiAction(3, buf)); } } // namespace Machine diff --git a/src/engine/ActionFactory.hpp b/src/engine/ActionFactory.hpp index 1ae47c4..d273274 100644 --- a/src/engine/ActionFactory.hpp +++ b/src/engine/ActionFactory.hpp @@ -18,16 +18,16 @@ #ifndef MACHINA_ACTIONFACTORY_HPP #define MACHINA_ACTIONFACTORY_HPP -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" namespace Machina { struct Action; namespace ActionFactory { -SharedPtr copy(SharedPtr copy); -SharedPtr note_on(unsigned char note); -SharedPtr note_off(unsigned char note); +SPtr copy(SPtr copy); +SPtr note_on(unsigned char note); +SPtr note_off(unsigned char note); } } // namespace Machina diff --git a/src/engine/Controller.cpp b/src/engine/Controller.cpp index f29d10e..df60c97 100644 --- a/src/engine/Controller.cpp +++ b/src/engine/Controller.cpp @@ -27,7 +27,7 @@ namespace Machina { -Controller::Controller(SharedPtr engine, +Controller::Controller(SPtr engine, Client::ClientModel& client_model) : _engine(engine) , _client_model(client_model) @@ -41,8 +41,8 @@ Controller::create(const Client::ClientObject& properties) { TimeDuration dur(_engine->machine()->time().unit(), properties.get(URIs::instance().machina_duration).get_float()); - SharedPtr node(new Machina::Node(dur)); - SharedPtr obj( + SPtr node(new Machina::Node(dur)); + SPtr obj( new Client::ClientObject(properties, node->id())); _objects.insert(node); _client_model.new_object(obj); @@ -51,13 +51,13 @@ Controller::create(const Client::ClientObject& properties) } void -Controller::announce(SharedPtr machine) +Controller::announce(SPtr machine) { Raul::Forge& forge = _engine->forge(); for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); n != machine->nodes().end(); ++n) { - SharedPtr obj( + SPtr obj( new Machina::Client::ClientObject((*n)->id())); obj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Node)); @@ -75,7 +75,7 @@ Controller::announce(SharedPtr machine) for (Machina::Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { _objects.insert(*e); - SharedPtr eobj( + SPtr eobj( new Client::ClientObject((*e)->id())); eobj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Edge)); @@ -91,21 +91,21 @@ Controller::announce(SharedPtr machine) } } -SharedPtr +SPtr Controller::find(uint64_t id) { - SharedPtr key(new StatefulKey(id)); - Objects::iterator i = _objects.find(key); + SPtr key(new StatefulKey(id)); + Objects::iterator i = _objects.find(key); if (i != _objects.end()) { return *i; } - return SharedPtr(); + return SPtr(); } void -Controller::learn(SharedPtr maid, uint64_t node_id) +Controller::learn(SPtr maid, uint64_t node_id) { - SharedPtr node = PtrCast(find(node_id)); + SPtr node = dynamic_ptr_cast(find(node_id)); if (node) { _engine->machine()->learn(maid, node); } else { @@ -119,7 +119,7 @@ Controller::set_property(uint64_t object_id, URIInt key, const Raul::Atom& value) { - SharedPtr object = find(object_id); + SPtr object = find(object_id); if (object) { object->set(key, value); _client_model.property(object_id, key, value); @@ -129,17 +129,16 @@ Controller::set_property(uint64_t object_id, uint64_t Controller::connect(uint64_t tail_id, uint64_t head_id) { - SharedPtr tail = PtrCast(find(tail_id)); - SharedPtr head = PtrCast(find(head_id)); + SPtr tail = dynamic_ptr_cast(find(tail_id)); + SPtr head = dynamic_ptr_cast(find(head_id)); - SharedPtr edge(new Machina::Edge(tail, head)); + SPtr edge(new Machina::Edge(tail, head)); tail->add_edge(edge); _objects.insert(edge); Raul::Forge& forge = _engine->forge(); - SharedPtr obj( - new Client::ClientObject(edge->id())); + SPtr obj(new Client::ClientObject(edge->id())); obj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Edge)); obj->set(URIs::instance().machina_probability, forge.make(1.0f)); @@ -154,10 +153,10 @@ Controller::connect(uint64_t tail_id, uint64_t head_id) void Controller::disconnect(uint64_t tail_id, uint64_t head_id) { - SharedPtr tail = PtrCast(find(tail_id)); - SharedPtr head = PtrCast(find(head_id)); + SPtr tail = dynamic_ptr_cast(find(tail_id)); + SPtr head = dynamic_ptr_cast(find(head_id)); - SharedPtr edge = tail->remove_edge_to(head); + SPtr edge = tail->remove_edge_to(head); if (edge) { _client_model.erase_object(edge->id()); } else { @@ -168,13 +167,13 @@ Controller::disconnect(uint64_t tail_id, uint64_t head_id) void Controller::erase(uint64_t id) { - SharedPtr key(new StatefulKey(id)); + SPtr key(new StatefulKey(id)); Objects::iterator i = _objects.find(key); if (i == _objects.end()) { return; } - SharedPtr node = PtrCast(*i); + SPtr node = dynamic_ptr_cast(*i); if (node) { _engine->machine()->remove_node(node); } @@ -193,13 +192,11 @@ Controller::process_updates() Raul::Atom value; for (uint32_t i = 0; i < read_space; ) { i += read_set(_updates, &subject, &key, &value); - SharedPtr obj = _client_model.find( - subject); + SPtr obj = _client_model.find(subject); if (obj) { obj->set(key, value); } else { - SharedPtr obj( - new Client::ClientObject(subject)); + SPtr obj(new Client::ClientObject(subject)); obj->set(key, value); _client_model.new_object(obj); } diff --git a/src/engine/Edge.cpp b/src/engine/Edge.cpp index 9aee198..e43efa1 100644 --- a/src/engine/Edge.cpp +++ b/src/engine/Edge.cpp @@ -42,8 +42,8 @@ Edge::write_state(Sord::Model& model) Sord::Curie(model.world(), "rdf:type"), Sord::Curie(model.world(), "machina:Edge")); - SharedPtr tail = _tail.lock(); - SharedPtr head = _head; + SPtr tail = _tail.lock(); + SPtr head = _head; if (!tail || !head) return; diff --git a/src/engine/Edge.hpp b/src/engine/Edge.hpp index 10c7ab8..6d916c1 100644 --- a/src/engine/Edge.hpp +++ b/src/engine/Edge.hpp @@ -22,8 +22,6 @@ #include #include "raul/DoubleBuffer.hpp" -#include "raul/SharedPtr.hpp" -#include "raul/WeakPtr.hpp" #include "machina/types.hpp" @@ -38,7 +36,7 @@ class Edge : public Stateful { public: - Edge(WeakPtr tail, SharedPtr head) + Edge(WPtr tail, SPtr head) : _probability(1.0f) , _tail(tail) , _head(head) @@ -47,11 +45,11 @@ public: void set(URIInt key, const Raul::Atom& value); void write_state(Sord::Model& model); - WeakPtr tail() { return _tail; } - SharedPtr head() { return _head; } + WPtr tail() { return _tail; } + SPtr head() { return _head; } - void set_tail(WeakPtr tail) { _tail = tail; } - void set_head(SharedPtr head) { _head = head; } + void set_tail(WPtr tail) { _tail = tail; } + void set_head(SPtr head) { _head = head; } inline float probability() { return _probability.get(); } inline void set_probability(float p) { _probability.set(p); } @@ -59,8 +57,8 @@ public: private: Raul::DoubleBuffer _probability; - WeakPtr _tail; - SharedPtr _head; + WPtr _tail; + SPtr _head; }; } // namespace Machina diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index b103310..e151cae 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -28,43 +28,43 @@ namespace Machina { -Engine::Engine(Raul::Forge& forge, - SharedPtr driver, - Sord::World& rdf_world) +Engine::Engine(Raul::Forge& forge, + SPtr driver, + Sord::World& rdf_world) : _driver(driver) , _rdf_world(rdf_world) , _loader(_forge, _rdf_world) , _forge(forge) {} -SharedPtr +SPtr Engine::new_driver(Raul::Forge& forge, const std::string& name, - SharedPtr machine) + SPtr machine) { #ifdef HAVE_JACK if (name == "jack") { JackDriver* driver = new JackDriver(forge, machine); driver->attach("machina"); - return SharedPtr(driver); + return SPtr(driver); } #endif if (name == "smf") { - return SharedPtr(new SMFDriver(forge, machine->time().unit())); + return SPtr(new SMFDriver(forge, machine->time().unit())); } std::cerr << "Error: Unknown driver type `" << name << "'" << std::endl; - return SharedPtr(); + return SPtr(); } /** Load the machine at @a uri, and run it (replacing current machine). * Safe to call while engine is processing. */ -SharedPtr +SPtr Engine::load_machine(const Glib::ustring& uri) { - SharedPtr machine = _loader.load(uri); - SharedPtr old_machine; + SPtr machine = _loader.load(uri); + SPtr old_machine; if (machine) { old_machine = _driver->machine(); // Keep a reference to old machine... machine->activate(); @@ -79,13 +79,14 @@ Engine::load_machine(const Glib::ustring& uri) /** Build a machine from the MIDI at @a uri, and run it (replacing current machine). * Safe to call while engine is processing. */ -SharedPtr -Engine::load_machine_midi(const Glib::ustring& uri, double q, - Raul::TimeDuration dur) +SPtr +Engine::load_machine_midi(const Glib::ustring& uri, + double q, + Raul::TimeDuration dur) { - SharedPtr file_driver(new SMFDriver(_forge, dur.unit())); - SharedPtr machine = file_driver->learn(uri, q, dur); - SharedPtr old_machine; + SPtr file_driver(new SMFDriver(_forge, dur.unit())); + SPtr machine = file_driver->learn(uri, q, dur); + SPtr old_machine; if (machine) { old_machine = _driver->machine(); // Keep a reference to old machine... machine->activate(); @@ -98,11 +99,10 @@ Engine::load_machine_midi(const Glib::ustring& uri, double q, } void -Engine::import_machine(SharedPtr machine) +Engine::import_machine(SPtr machine) { machine->activate(); - _driver->machine()->nodes().insert(_driver->machine()->nodes().end(), - machine->nodes().begin(), + _driver->machine()->nodes().insert(machine->nodes().begin(), machine->nodes().end()); // FIXME: thread safe? // FIXME: announce @@ -111,8 +111,8 @@ Engine::import_machine(SharedPtr machine) void Engine::export_midi(const Glib::ustring& filename, Raul::TimeDuration dur) { - SharedPtr machine = _driver->machine(); - SharedPtr file_driver( + SPtr machine = _driver->machine(); + SPtr file_driver( new Machina::SMFDriver(_forge, dur.unit())); const bool activated = _driver->is_activated(); diff --git a/src/engine/Evolver.cpp b/src/engine/Evolver.cpp index a71d64b..56b3b45 100644 --- a/src/engine/Evolver.cpp +++ b/src/engine/Evolver.cpp @@ -32,12 +32,13 @@ using namespace boost; namespace Machina { -Evolver::Evolver(TimeUnit unit, const string& target_midi, - SharedPtr seed) +Evolver::Evolver(TimeUnit unit, + const string& target_midi, + SPtr seed) : _problem(new Problem(unit, target_midi, seed)) , _seed_fitness(-FLT_MAX) { - SharedPtr > m(new HybridMutation()); + SPtr > m(new HybridMutation()); m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation >( new Mutation::Compress())); @@ -59,14 +60,14 @@ Evolver::Evolver(TimeUnit unit, const string& target_midi, boost::shared_ptr< Selection > s(new TournamentSelection( _problem, 3, 0.8)); boost::shared_ptr< Crossover > crossover; - _ga = SharedPtr(new MachinaGA(_problem, s, crossover, m, - 20, 20, 2, 1.0, 0.0)); + _ga = SPtr( + new MachinaGA(_problem, s, crossover, m, 20, 20, 2, 1.0, 0.0)); } void -Evolver::seed(SharedPtr parent) +Evolver::seed(SPtr parent) { - /*_best = SharedPtr(new Machine(*parent.get())); + /*_best = SPtr(new Machine(*parent.get())); _best_fitness = _problem->fitness(*_best.get());*/ _problem->seed(parent); _seed_fitness = _problem->fitness(*parent.get()); diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index d13bbdd..d8442b1 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -32,7 +32,7 @@ using namespace std; namespace Machina { -JackDriver::JackDriver(Raul::Forge& forge, SharedPtr machine) +JackDriver::JackDriver(Raul::Forge& forge, SPtr machine) : Driver(forge, machine) , _client(NULL) , _machine_changed(0) @@ -107,9 +107,9 @@ JackDriver::attach(const std::string& client_name) } if (!_machine) { - _machine = SharedPtr( - new Machine(TimeUnit::frames(jack_get_sample_rate( - jack_client())))); + _machine = SPtr( + new Machine(TimeUnit::frames( + jack_get_sample_rate(jack_client())))); } _machine->activate(); @@ -160,13 +160,13 @@ JackDriver::deactivate() } void -JackDriver::set_machine(SharedPtr machine) +JackDriver::set_machine(SPtr machine) { if (machine == _machine) { return; } - SharedPtr last_machine = _last_machine; // Keep a reference + SPtr last_machine = _last_machine; // Keep a reference _machine_changed.reset(0); assert(!last_machine.unique()); _machine = machine; @@ -178,7 +178,7 @@ JackDriver::set_machine(SharedPtr machine) } void -JackDriver::read_input_recording(SharedPtr machine, +JackDriver::read_input_recording(SPtr machine, const Raul::TimeSlice& time) { const jack_nframes_t nframes = time.length_ticks().ticks(); @@ -203,7 +203,7 @@ JackDriver::read_input_recording(SharedPtr machine, } void -JackDriver::read_input_playing(SharedPtr machine, +JackDriver::read_input_playing(SPtr machine, const Raul::TimeSlice& time) { const jack_nframes_t nframes = time.length_ticks().ticks(); @@ -215,7 +215,7 @@ JackDriver::read_input_playing(SharedPtr machine, jack_midi_event_get(&ev, buf, i); if (ev.buffer[0] == 0x90) { - const SharedPtr learn = machine->pending_learn(); + const SPtr learn = machine->pending_learn(); if (learn) { learn->enter_action()->set_event(ev.size, ev.buffer); learn->start(_quantization.get(), @@ -225,7 +225,7 @@ JackDriver::read_input_playing(SharedPtr machine, } } else if (ev.buffer[0] == 0x80) { - const SharedPtr learn = machine->pending_learn(); + const SPtr learn = machine->pending_learn(); if (learn && learn->started()) { learn->exit_action()->set_event(ev.size, ev.buffer); learn->finish( @@ -323,7 +323,7 @@ JackDriver::on_process(jack_nframes_t nframes) /* Take a reference to machine here and use only it during the process * cycle so _machine can be switched with set_machine during a cycle. */ - SharedPtr machine = _machine; + SPtr machine = _machine; // Machine was switched since last cycle, finalize old machine. if (machine != _last_machine) { @@ -411,9 +411,8 @@ void JackDriver::start_record(bool step) { // FIXME: Choose an appropriate maximum ringbuffer size - _recorder - = SharedPtr(new Recorder(_forge, 1024, _beats_unit, - _quantization.get(), step)); + _recorder = SPtr( + new Recorder(_forge, 1024, _beats_unit, _quantization.get(), step)); _recorder->start(); _record_dur = 0; _recording = true; @@ -423,12 +422,10 @@ void JackDriver::finish_record() { _recording = false; - SharedPtr machine = _recorder->finish(); + SPtr machine = _recorder->finish(); _recorder.reset(); machine->activate(); - _machine->nodes().insert(_machine->nodes().end(), - machine->nodes().begin(), - machine->nodes().end()); + _machine->nodes().insert(machine->nodes().begin(), machine->nodes().end()); } int diff --git a/src/engine/JackDriver.hpp b/src/engine/JackDriver.hpp index c0f94ff..06ffabe 100644 --- a/src/engine/JackDriver.hpp +++ b/src/engine/JackDriver.hpp @@ -25,11 +25,11 @@ #include "raul/DoubleBuffer.hpp" #include "raul/Semaphore.hpp" -#include "raul/SharedPtr.hpp" #include "machina/Context.hpp" #include "machina/Driver.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Recorder.hpp" @@ -47,8 +47,8 @@ class JackDriver : public Machina::Driver { public: - JackDriver(Raul::Forge& forge, - SharedPtr machine = SharedPtr()); + JackDriver(Raul::Forge& forge, + SPtr machine = SPtr()); ~JackDriver(); void attach(const std::string& client_name); @@ -57,7 +57,7 @@ public: void activate(); void deactivate(); - void set_machine(SharedPtr machine); + void set_machine(SPtr machine); void write_event(Raul::TimeStamp time, size_t size, @@ -90,10 +90,10 @@ public: jack_client_t* jack_client() const { return _client; } private: - void read_input_recording(SharedPtr machine, + void read_input_recording(SPtr machine, const Raul::TimeSlice& time); - void read_input_playing(SharedPtr machine, + void read_input_playing(SPtr machine, const Raul::TimeSlice& time); static void jack_error_cb(const char* msg); @@ -104,8 +104,8 @@ private: jack_client_t* _client; - Raul::Semaphore _machine_changed; - SharedPtr _last_machine; + Raul::Semaphore _machine_changed; + SPtr _last_machine; jack_port_t* _input_port; jack_port_t* _output_port; @@ -121,10 +121,10 @@ private: Raul::Semaphore _stop; bool _stop_flag; - Raul::TimeDuration _record_dur; - std::atomic _recording; - SharedPtr _recorder; - bool _is_activated; + Raul::TimeDuration _record_dur; + std::atomic _recording; + SPtr _recorder; + bool _is_activated; }; } // namespace Machina diff --git a/src/engine/LearnRequest.cpp b/src/engine/LearnRequest.cpp index 81ef0e3..acf5bf0 100644 --- a/src/engine/LearnRequest.cpp +++ b/src/engine/LearnRequest.cpp @@ -20,7 +20,7 @@ namespace Machina { -LearnRequest::LearnRequest(SharedPtr maid, SharedPtr node) +LearnRequest::LearnRequest(SPtr maid, SPtr node) : _started(false) , _start_time(TimeUnit(TimeUnit::BEATS, 19200), 0, 0) // irrelevant , _quantization(0) // irrelevant @@ -32,10 +32,10 @@ LearnRequest::LearnRequest(SharedPtr maid, SharedPtr node) maid->manage(_exit_action); } -SharedPtr -LearnRequest::create(SharedPtr maid, SharedPtr node) +SPtr +LearnRequest::create(SPtr maid, SPtr node) { - SharedPtr ret(new LearnRequest(maid, node)); + SPtr ret(new LearnRequest(maid, node)); maid->manage(ret); return ret; } diff --git a/src/engine/LearnRequest.hpp b/src/engine/LearnRequest.hpp index af28017..4093d24 100644 --- a/src/engine/LearnRequest.hpp +++ b/src/engine/LearnRequest.hpp @@ -19,8 +19,6 @@ #define MACHINA_LEARNREQUEST_HPP #include "raul/Maid.hpp" -#include "raul/SharedPtr.hpp" -#include "raul/Manageable.hpp" #include "machina/types.hpp" @@ -34,31 +32,30 @@ class MidiAction; /** A request to MIDI learn a certain node. */ -class LearnRequest - : public Raul::Manageable +class LearnRequest : public Raul::Maid::Manageable { public: - static SharedPtrcreate(SharedPtr maid, - SharedPtr node); + static SPtrcreate(SPtr maid, + SPtr node); void start(double q, Raul::TimeStamp time); void finish(TimeStamp time); inline bool started() const { return _started; } - const SharedPtr& node() { return _node; } - const SharedPtr& enter_action() { return _enter_action; } - const SharedPtr& exit_action() { return _exit_action; } + const SPtr& node() { return _node; } + const SPtr& enter_action() { return _enter_action; } + const SPtr& exit_action() { return _exit_action; } private: - LearnRequest(SharedPtr maid, SharedPtr node); + LearnRequest(SPtr maid, SPtr node); bool _started; TimeStamp _start_time; double _quantization; - SharedPtr _node; - SharedPtr _enter_action; - SharedPtr _exit_action; + SPtr _node; + SPtr _enter_action; + SPtr _exit_action; }; } // namespace Machina diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp index 32e8d19..c206c4c 100644 --- a/src/engine/Loader.cpp +++ b/src/engine/Loader.cpp @@ -50,12 +50,12 @@ Loader::Loader(Raul::Forge& forge, Sord::World& rdf_world) * @param uri URI of machine (resolvable URI to an RDF document). * @return Loaded Machine. */ -SharedPtr +SPtr Loader::load(const Glib::ustring& uri) { using Glib::ustring; - SharedPtr machine; + SPtr machine; ustring document_uri = uri; @@ -68,10 +68,10 @@ Loader::load(const Glib::ustring& uri) cout << "[Loader] Loading " << document_uri << endl; - machine = SharedPtr( + machine = SPtr( new Machine(TimeUnit::beats(MACHINA_PPQN))); - typedef std::map > Created; + typedef std::map > Created; Created created; Sord::Model model(_rdf_world, document_uri); @@ -98,11 +98,10 @@ Loader::load(const Glib::ustring& uri) !i.end(); ++i) { const Sord::Node& node_id = i.get_object(); Sord::Iter d = model.find(node_id, machina_duration, nil); - SharedPtr node( - new Node( - TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), - d.get_object().to_float()), - true)); + SPtr node( + new Node(TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), + d.get_object().to_float()), + true)); Sord::Iter s = model.find(node_id, rdf_type, machina_SelectorNode); if (!s.end()) { @@ -119,10 +118,10 @@ Loader::load(const Glib::ustring& uri) const Sord::Node& node_id = i.get_object(); Sord::Iter d = model.find(node_id, machina_duration, nil); if (created.find(node_id) == created.end()) { - SharedPtr node(new Node( - TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), - d.get_object().to_float()), - false)); + SPtr node( + new Node(TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), + d.get_object().to_float()), + false)); machine->add_node(node); created[node_id] = node; } @@ -143,8 +142,8 @@ Loader::load(const Glib::ustring& uri) Created::iterator node_i = created.find((const char*)results->get("node")); if (node_i != created.end()) { - SharedPtr node = node_i->second; - const int note_num = results->get("note").to_int(); + SPtr node = node_i->second; + const int note_num = results->get("note").to_int(); if (note_num >= 0 && note_num <= 127) { node->set_enter_action( ActionFactory::note_on((uint8_t)note_num)); @@ -179,10 +178,10 @@ Loader::load(const Glib::ustring& uri) Created::iterator head_i = created.find(head); if (tail_i != created.end() && head_i != created.end()) { - const SharedPtr tail = tail_i->second; - const SharedPtr head = head_i->second; + const SPtr tail = tail_i->second; + const SPtr head = head_i->second; - SharedPtr edge(new Edge(tail, head)); + SPtr edge(new Edge(tail, head)); edge->set_probability(prob); tail->add_edge(edge); @@ -197,7 +196,7 @@ Loader::load(const Glib::ustring& uri) machine->reset(NULL, machine->time()); return machine; } else { - return SharedPtr(); + return SPtr(); } } diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index 4b548a7..73eba15 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -19,14 +19,14 @@ #include #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "sord/sordmm.hpp" +#include "machina/Context.hpp" #include "machina/Machine.hpp" -#include "machina/Updates.hpp" #include "machina/URIs.hpp" -#include "machina/Context.hpp" +#include "machina/Updates.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "Node.hpp" @@ -39,7 +39,7 @@ using namespace Raul; namespace Machina { Machine::Machine(TimeUnit unit) - : _active_nodes(MAX_ACTIVE_NODES, SharedPtr()) + : _active_nodes(MAX_ACTIVE_NODES, SPtr()) , _time(unit, 0, 0) , _is_activated(false) , _is_finished(false) @@ -52,17 +52,17 @@ Machine::Machine(TimeUnit unit) */ Machine::Machine(const Machine& copy) : Stateful() // don't copy RDF ID - , _active_nodes(MAX_ACTIVE_NODES, SharedPtr()) + , _active_nodes(MAX_ACTIVE_NODES, SPtr()) , _time(copy.time()) , _is_activated(false) , _is_finished(false) { - std::map< SharedPtr, SharedPtr > replacements; + std::map< SPtr, SPtr > replacements; for (Nodes::const_iterator n = copy._nodes.begin(); n != copy._nodes.end(); ++n) { - SharedPtr node(new Machina::Node(*n->get())); - _nodes.push_back(node); + SPtr node(new Machina::Node(*n->get())); + _nodes.insert(node); replacements[*n] = node; } @@ -79,20 +79,20 @@ Machine::Machine(const Machine& copy) Machine& Machine::operator=(const Machine& other) { - _active_nodes = std::vector< SharedPtr >(MAX_ACTIVE_NODES, - SharedPtr()); + _active_nodes = std::vector< SPtr >(MAX_ACTIVE_NODES, + SPtr()); _is_activated = false; _is_finished = false; _time = other._time; - _pending_learn = SharedPtr(); + _pending_learn = SPtr(); _nodes.clear(); - map< SharedPtr, SharedPtr > replacements; + map< SPtr, SPtr > replacements; for (Nodes::const_iterator n = other._nodes.begin(); n != other._nodes.end(); ++n) { - SharedPtr node(new Machina::Node(*n->get())); - _nodes.push_back(node); + SPtr node(new Machina::Node(*n->get())); + _nodes.insert(node); replacements[*n] = node; } @@ -109,11 +109,11 @@ Machine::operator=(const Machine& other) } /** Always returns a node, unless there are none */ -SharedPtr +SPtr Machine::random_node() { if (_nodes.empty()) { - return SharedPtr(); + return SPtr(); } size_t i = rand() % _nodes.size(); @@ -126,33 +126,32 @@ Machine::random_node() } } - return SharedPtr(); + return SPtr(); } /** May return NULL even if edges exist (with low probability) */ -SharedPtr +SPtr Machine::random_edge() { - SharedPtr tail = random_node(); + SPtr tail = random_node(); for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) { tail = random_node(); } - return tail ? tail->random_edge() : SharedPtr(); + return tail ? tail->random_edge() : SPtr(); } void -Machine::add_node(SharedPtr node) +Machine::add_node(SPtr node) { - assert(std::find(_nodes.begin(), _nodes.end(), node) == _nodes.end()); - _nodes.push_back(node); + _nodes.insert(node); } void -Machine::remove_node(SharedPtr node) +Machine::remove_node(SPtr node) { - _nodes.erase(std::find(_nodes.begin(), _nodes.end(), node)); + _nodes.erase(node); for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { (*n)->remove_edge_to(node); @@ -165,9 +164,8 @@ void Machine::reset(MIDISink* sink, Raul::TimeStamp time) { if (!_is_finished) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); - ++n) { - SharedPtr node = (*n); + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + SPtr node = (*n); if (node->is_active()) { node->exit(sink, time); @@ -187,13 +185,13 @@ Machine::reset(MIDISink* sink, Raul::TimeStamp time) /** Return the active Node with the earliest exit time. */ -SharedPtr +SPtr Machine::earliest_node() const { - SharedPtr earliest; + SPtr earliest; for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { - SharedPtr node = _active_nodes.at(i); + SPtr node = _active_nodes.at(i); if (node) { if (!node->is_active()) { @@ -214,9 +212,9 @@ Machine::earliest_node() const * Returns true if node was entered, or false if the maximum active nodes has been reached. */ bool -Machine::enter_node(Context& context, - SharedPtr node, - SharedPtr updates) +Machine::enter_node(Context& context, + SPtr node, + SPtr updates) { assert(!node->is_active()); @@ -245,9 +243,9 @@ Machine::enter_node(Context& context, /** Exit an active node at the current _time. */ void -Machine::exit_node(Context& context, - SharedPtr node, - SharedPtr updates) +Machine::exit_node(Context& context, + SPtr node, + SPtr updates) { node->exit(context.sink(), _time); write_set(updates, @@ -294,7 +292,7 @@ Machine::exit_node(Context& context, const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] if (rand_normal <= (*e)->probability()) { - SharedPtr head = (*e)->head(); + SPtr head = (*e)->head(); if (!head->is_active()) { enter_node(context, head, updates); @@ -315,7 +313,7 @@ Machine::exit_node(Context& context, * with sample accuracy if necessary). */ uint32_t -Machine::run(Context& context, SharedPtr updates) +Machine::run(Context& context, SPtr updates) { if (_is_finished) { return 0; @@ -357,7 +355,7 @@ Machine::run(Context& context, SharedPtr updates) while (true) { - SharedPtr earliest = earliest_node(); + SPtr earliest = earliest_node(); if (!earliest) { // No more active states, machine is finished @@ -393,7 +391,7 @@ Machine::run(Context& context, SharedPtr updates) * NOT realtime (actions are allocated here). */ void -Machine::learn(SharedPtr maid, SharedPtr node) +Machine::learn(SPtr maid, SPtr node) { _pending_learn = LearnRequest::create(maid, node); } diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index 0a6dbfc..57d6c2f 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -19,9 +19,8 @@ #include "lv2/lv2plug.in/ns/ext/midi/midi.h" -#include "raul/SharedPtr.hpp" - #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "MachineBuilder.hpp" @@ -34,7 +33,7 @@ using namespace Raul; namespace Machina { -MachineBuilder::MachineBuilder(SharedPtr machine, double q, bool step) +MachineBuilder::MachineBuilder(SPtr machine, double q, bool step) : _quantization(q) , _time(machine->time().unit()) // = 0 , _machine(machine) @@ -48,13 +47,13 @@ void MachineBuilder::reset() { _time = TimeStamp(_machine->time().unit()); // = 0 - _initial_node = SharedPtr(new Node(_time, true)); // duration 0 + _initial_node = SPtr(new Node(_time, true)); // duration 0 _connect_node = _initial_node; _connect_node_end_time = _time; // = 0 } bool -MachineBuilder::is_delay_node(SharedPtr node) const +MachineBuilder::is_delay_node(SPtr node) const { return !node->enter_action() && !node->exit_action(); } @@ -62,7 +61,7 @@ MachineBuilder::is_delay_node(SharedPtr node) const /** Set the duration of a node, with quantization. */ void -MachineBuilder::set_node_duration(SharedPtr node, +MachineBuilder::set_node_duration(SPtr node, Raul::TimeDuration d) const { if (_step) { @@ -83,31 +82,31 @@ MachineBuilder::set_node_duration(SharedPtr node, * * If a delay node is added to the machine, it is returned. */ -SharedPtr -MachineBuilder::connect_nodes(SharedPtr m, - SharedPtr tail, - Raul::TimeStamp tail_end_time, - SharedPtr head, - Raul::TimeStamp head_start_time) +SPtr +MachineBuilder::connect_nodes(SPtr m, + SPtr tail, + Raul::TimeStamp tail_end_time, + SPtr head, + Raul::TimeStamp head_start_time) { assert(tail != head); assert(head_start_time >= tail_end_time); - SharedPtr delay_node; + SPtr delay_node; if (is_delay_node(tail) && tail->edges().size() == 0) { // Tail is a delay node, just accumulate the time difference into it set_node_duration(tail, tail->duration() + head_start_time - tail_end_time); - tail->add_edge(SharedPtr(new Edge(tail, head))); + tail->add_edge(SPtr(new Edge(tail, head))); } else if (_step || (head_start_time == tail_end_time)) { // Connect directly - tail->add_edge(SharedPtr(new Edge(tail, head))); + tail->add_edge(SPtr(new Edge(tail, head))); } else { // Need to actually create a delay node - delay_node = SharedPtr(new Node(head_start_time - tail_end_time)); - tail->add_edge(SharedPtr(new Edge(tail, delay_node))); - delay_node->add_edge(SharedPtr(new Edge(delay_node, head))); + delay_node = SPtr(new Node(head_start_time - tail_end_time)); + tail->add_edge(SPtr(new Edge(tail, delay_node))); + delay_node->add_edge(SPtr(new Edge(delay_node, head))); m->add_node(delay_node); } @@ -117,10 +116,10 @@ MachineBuilder::connect_nodes(SharedPtr m, void MachineBuilder::note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf) { - SharedPtr node(new Node(TimeStamp(t.unit()))); - node->set_enter_action(SharedPtr(new MidiAction(ev_size, buf))); + SPtr node(new Node(TimeStamp(t.unit()))); + node->set_enter_action(SPtr(new MidiAction(ev_size, buf))); - SharedPtr this_connect_node; + SPtr this_connect_node; Raul::TimeStamp this_connect_node_end_time(t.unit()); /* If currently polyphonic, use a poly node with no successors as connect @@ -143,7 +142,7 @@ MachineBuilder::note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf) this_connect_node_end_time = _connect_node_end_time; } - SharedPtr delay_node = connect_nodes( + SPtr delay_node = connect_nodes( _machine, this_connect_node, this_connect_node_end_time, node, t); if (delay_node) { @@ -159,10 +158,10 @@ void MachineBuilder::resolve_note(Raul::TimeStamp t, size_t ev_size, uint8_t* buf, - SharedPtr resolved) + SPtr resolved) { resolved->set_exit_action( - SharedPtr(new MidiAction(ev_size, buf))); + SPtr(new MidiAction(ev_size, buf))); set_node_duration(resolved, t - resolved->enter_time()); if (_active_nodes.size() == 1) { @@ -171,7 +170,7 @@ MachineBuilder::resolve_note(Raul::TimeStamp t, if (!_poly_nodes.empty()) { // Finish a polyphonic section - _connect_node = SharedPtr(new Node(TimeStamp(t.unit()))); + _connect_node = SPtr(new Node(TimeStamp(t.unit()))); _machine->add_node(_connect_node); connect_nodes(_machine, resolved, t, _connect_node, t); @@ -201,8 +200,8 @@ MachineBuilder::resolve_note(Raul::TimeStamp t, assert(_connect_node->edges().empty()); _connect_node->set_enter_action(resolved->enter_action()); _connect_node->set_exit_action(resolved->exit_action()); - resolved->set_enter_action(SharedPtr()); - resolved->set_exit_action(SharedPtr()); + resolved->set_enter_action(SPtr()); + resolved->set_exit_action(SPtr()); set_node_duration(_connect_node, resolved->duration()); resolved = _connect_node; if (std::find(_machine->nodes().begin(), @@ -246,8 +245,7 @@ MachineBuilder::event(Raul::TimeStamp time_offset, } else if ((buf[0] & 0xF0) == LV2_MIDI_MSG_NOTE_OFF) { for (ActiveList::iterator i = _active_nodes.begin(); i != _active_nodes.end(); ++i) { - SharedPtr action = PtrCast( - (*i)->enter_action()); + SPtr action = dynamic_ptr_cast((*i)->enter_action()); if (!action) { continue; } @@ -275,10 +273,10 @@ MachineBuilder::resolve() { // Resolve stuck notes if (!_active_nodes.empty()) { - for (list >::iterator i = _active_nodes.begin(); + for (list >::iterator i = _active_nodes.begin(); i != _active_nodes.end(); ++i) { cerr << "WARNING: Resolving stuck note." << endl; - SharedPtr action = PtrCast( + SPtr action = dynamic_ptr_cast( (*i)->enter_action()); if (!action) { continue; @@ -293,7 +291,7 @@ MachineBuilder::resolve() 0x40 }; (*i)->set_exit_action( - SharedPtr(new MidiAction(3, note_off))); + SPtr(new MidiAction(3, note_off))); set_node_duration((*i), _time - (*i)->enter_time()); (*i)->exit(NULL, _time); _machine->add_node((*i)); @@ -312,7 +310,7 @@ MachineBuilder::resolve() } -SharedPtr +SPtr MachineBuilder::finish() { resolve(); diff --git a/src/engine/MachineBuilder.hpp b/src/engine/MachineBuilder.hpp index cb3ccea..4a915aa 100644 --- a/src/engine/MachineBuilder.hpp +++ b/src/engine/MachineBuilder.hpp @@ -22,7 +22,7 @@ #include -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "raul/TimeStamp.hpp" namespace Machina { @@ -33,9 +33,9 @@ class Node; class MachineBuilder { public: - MachineBuilder(SharedPtr machine, - double quantization, - bool step); + MachineBuilder(SPtr machine, + double quantization, + bool step); void set_time(Raul::TimeStamp time) { _time = time; } @@ -44,38 +44,38 @@ public: void reset(); void resolve(); - SharedPtr finish(); + SPtr finish(); private: - bool is_delay_node(SharedPtr node) const; - void set_node_duration(SharedPtr node, Raul::TimeDuration d) const; + bool is_delay_node(SPtr node) const; + void set_node_duration(SPtr node, Raul::TimeDuration d) const; void note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf); void resolve_note(Raul::TimeStamp t, size_t ev_size, uint8_t* buf, - SharedPtr resolved); + SPtr resolved); - SharedPtrconnect_nodes(SharedPtr m, - SharedPtr tail, - Raul::TimeStamp tail_end_time, - SharedPtr head, - Raul::TimeStamp head_start_time); + SPtrconnect_nodes(SPtr m, + SPtr tail, + Raul::TimeStamp tail_end_time, + SPtr head, + Raul::TimeStamp head_start_time); - typedef std::list > ActiveList; + typedef std::list > ActiveList; ActiveList _active_nodes; - typedef std::list > > PolyList; + typedef std::list > > PolyList; PolyList _poly_nodes; double _quantization; Raul::TimeStamp _time; - SharedPtr _machine; - SharedPtr _initial_node; - SharedPtr _connect_node; - Raul::TimeStamp _connect_node_end_time; + SPtr _machine; + SPtr _initial_node; + SPtr _connect_node; + Raul::TimeStamp _connect_node_end_time; bool _step; }; diff --git a/src/engine/MidiAction.cpp b/src/engine/MidiAction.cpp index 268279f..cac01a3 100644 --- a/src/engine/MidiAction.cpp +++ b/src/engine/MidiAction.cpp @@ -17,7 +17,6 @@ #include -#include "raul/SharedPtr.hpp" #include "raul/Atom.hpp" #include "machina/types.hpp" diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp index e25cdbb..fde1ba6 100644 --- a/src/engine/Mutation.cpp +++ b/src/engine/Mutation.cpp @@ -55,17 +55,17 @@ AddNode::mutate(Machine& machine) //cout << "ADD NODE" << endl; // Create random node - SharedPtr node(new Node(machine.time().unit())); + SPtr node(new Node(machine.time().unit())); node->set_selector(true); - SharedPtr note_node = machine.random_node(); + SPtr note_node = machine.random_node(); if (!note_node) { return; } uint8_t note = rand() % 128; - SharedPtr enter_action = PtrCast( + SPtr enter_action = dynamic_ptr_cast( note_node->enter_action()); if (enter_action) { note = enter_action->event()[1]; @@ -76,15 +76,15 @@ AddNode::mutate(Machine& machine) machine.add_node(node); // Insert after some node - SharedPtr tail = machine.random_node(); + SPtr tail = machine.random_node(); if (tail && (tail != node) /* && !node->connected_to(tail)*/) { - tail->add_edge(boost::shared_ptr(new Edge(tail, node))); + tail->add_edge(SPtr(new Edge(tail, node))); } // Insert before some other node - SharedPtr head = machine.random_node(); + SPtr head = machine.random_node(); if (head && (head != node) /* && !head->connected_to(node)*/) { - node->add_edge(boost::shared_ptr(new Edge(node, head))); + node->add_edge(SPtr(new Edge(node, head))); } } @@ -93,7 +93,7 @@ RemoveNode::mutate(Machine& machine) { //cout << "REMOVE NODE" << endl; - SharedPtr node = machine.random_node(); + SPtr node = machine.random_node(); if (node && !node->is_initial()) { machine.remove_node(node); } @@ -104,11 +104,11 @@ AdjustNode::mutate(Machine& machine) { //cout << "ADJUST NODE" << endl; - SharedPtr node = machine.random_node(); + SPtr node = machine.random_node(); if (node) { - SharedPtr enter_action = PtrCast( + SPtr enter_action = dynamic_ptr_cast( node->enter_action()); - SharedPtr exit_action = PtrCast( + SPtr exit_action = dynamic_ptr_cast( node->exit_action()); if (enter_action && exit_action) { const uint8_t note = rand() % 128; @@ -128,16 +128,16 @@ SwapNodes::mutate(Machine& machine) return; } - SharedPtr a = machine.random_node(); - SharedPtr b = machine.random_node(); + SPtr a = machine.random_node(); + SPtr b = machine.random_node(); while (b == a) { b = machine.random_node(); } - SharedPtr a_enter = PtrCast(a->enter_action()); - SharedPtr a_exit = PtrCast(a->exit_action()); - SharedPtr b_enter = PtrCast(b->enter_action()); - SharedPtr b_exit = PtrCast(b->exit_action()); + SPtr a_enter = dynamic_ptr_cast(a->enter_action()); + SPtr a_exit = dynamic_ptr_cast(a->exit_action()); + SPtr b_enter = dynamic_ptr_cast(b->enter_action()); + SPtr b_exit = dynamic_ptr_cast(b->exit_action()); uint8_t note_a = a_enter->event()[1]; uint8_t note_b = b_enter->event()[1]; @@ -153,14 +153,14 @@ AddEdge::mutate(Machine& machine) { //cout << "ADJUST EDGE" << endl; - SharedPtr tail = machine.random_node(); - SharedPtr head = machine.random_node(); + SPtr tail = machine.random_node(); + SPtr head = machine.random_node(); if (tail && head && tail != head) { // && !tail->connected_to(head) && !head->connected_to(tail) - SharedPtr edge(new Edge(tail, head)); + SPtr edge(new Edge(tail, head)); edge->set_probability(rand() / (float)RAND_MAX); - tail->add_edge(boost::shared_ptr(new Edge(tail, head))); + tail->add_edge(SPtr(new Edge(tail, head))); } } @@ -169,7 +169,7 @@ RemoveEdge::mutate(Machine& machine) { //cout << "REMOVE EDGE" << endl; - SharedPtr tail = machine.random_node(); + SPtr tail = machine.random_node(); if (tail && !(tail->is_initial() && tail->edges().size() == 1)) { tail->remove_edge(tail->random_edge()); } @@ -180,7 +180,7 @@ AdjustEdge::mutate(Machine& machine) { //cout << "ADJUST EDGE" << endl; - SharedPtr edge = machine.random_edge(); + SPtr edge = machine.random_edge(); if (edge) { edge->set_probability(rand() / (float)RAND_MAX); edge->tail().lock()->edges_changed(); diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp index 4de00d2..d8d89b9 100644 --- a/src/engine/Node.cpp +++ b/src/engine/Node.cpp @@ -53,16 +53,16 @@ Node::Node(const Node& copy) { for (Edges::const_iterator i = copy._edges.begin(); i != copy._edges.end(); ++i) { - SharedPtr edge(new Edge(*i->get())); - _edges.push_back(edge); + SPtr edge(new Edge(*i->get())); + _edges.insert(edge); } } /** Always returns an edge, unless there are none */ -SharedPtr +SPtr Node::random_edge() { - SharedPtr ret; + SPtr ret; if (_edges.empty()) { return ret; } @@ -115,14 +115,14 @@ Node::set_selector(bool yn) } void -Node::set_enter_action(SharedPtr action) +Node::set_enter_action(SPtr action) { _enter_action = action; _changed = true; } void -Node::set_exit_action(SharedPtr action) +Node::set_exit_action(SPtr action) { _exit_action = action; _changed = true; @@ -156,52 +156,52 @@ Node::exit(MIDISink* sink, TimeStamp time) _enter_time = 0; } -void -Node::add_edge(SharedPtr edge) +SPtr +Node::edge_to(SPtr head) const { - assert(edge->tail().lock().get() == this); + // TODO: Make logarithmic for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == edge->head()) { - return; + if ((*i)->head() == head) { + return *i; } } + return SPtr(); +} + +void +Node::add_edge(SPtr edge) +{ + assert(edge->tail().lock().get() == this); + if (edge_to(edge->head())) { + return; + } - _edges.push_back(edge); + _edges.insert(edge); edges_changed(); } void -Node::remove_edge(SharedPtr edge) +Node::remove_edge(SPtr edge) { - _edges.erase(std::find(_edges.begin(), _edges.end(), edge)); + _edges.erase(edge); edges_changed(); } bool -Node::connected_to(SharedPtr node) +Node::connected_to(SPtr node) { - for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == node) { - return true; - } - } - - return false; + return bool(edge_to(node)); } -SharedPtr -Node::remove_edge_to(SharedPtr node) +SPtr +Node::remove_edge_to(SPtr node) { - for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == node) { - SharedPtr edge(*i); - _edges.erase(i); - edges_changed(); - return edge; - } + SPtr edge = edge_to(node); + if (edge) { + _edges.erase(edge); // TODO: avoid double search + edges_changed(); } - - return SharedPtr(); + return edge; } void diff --git a/src/engine/Node.hpp b/src/engine/Node.hpp index cf41b26..953f652 100644 --- a/src/engine/Node.hpp +++ b/src/engine/Node.hpp @@ -18,9 +18,9 @@ #ifndef MACHINA_NODE_HPP #define MACHINA_NODE_HPP -#include +#include -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "Action.hpp" #include "Schrodinbit.hpp" @@ -36,34 +36,33 @@ using Raul::TimeUnit; /** A node is a state (as in a FSM diagram), or "note". * - * It contains a action, as well as a duration and pointers to it's + * It contains a action, as well as a duration and pointers to its * successors (states/nodes that (may) follow it). * * Initial nodes do not have enter actions (since they are entered at * an undefined point in time <= 0). */ -class Node - : public Stateful +class Node : public Stateful { public: Node(TimeDuration duration, bool initial = false); Node(const Node& copy); - void set_enter_action(SharedPtr action); - void set_exit_action(SharedPtr action); + void set_enter_action(SPtr action); + void set_exit_action(SPtr action); - SharedPtr enter_action() { return _enter_action; } - SharedPtr exit_action() { return _exit_action; } + SPtr enter_action() { return _enter_action; } + SPtr exit_action() { return _exit_action; } void enter(MIDISink* sink, TimeStamp time); void exit(MIDISink* sink, TimeStamp time); void edges_changed(); - void add_edge(SharedPtr edge); - void remove_edge(SharedPtr edge); - SharedPtr remove_edge_to(SharedPtr node); - bool connected_to(SharedPtr node); + void add_edge(SPtr edge); + void remove_edge(SPtr edge); + SPtr remove_edge_to(SPtr node); + bool connected_to(SPtr node); void set(URIInt key, const Raul::Atom& value); void write_state(Sord::Model& model); @@ -81,23 +80,32 @@ public: inline bool changed() { return _changed; } inline void set_changed() { _changed = true; } - typedef std::list< SharedPtr > Edges; + struct EdgeHeadOrder { + inline bool operator()(const SPtr& a, const SPtr& b) { + return a < b; + } + }; + + SPtr edge_to(SPtr head) const; + + typedef std::set, EdgeHeadOrder> Edges; + Edges& edges() { return _edges; } - SharedPtr random_edge(); + SPtr random_edge(); private: Node& operator=(const Node& other); // undefined - TimeStamp _enter_time; ///< valid iff _is_active - TimeDuration _duration; - SharedPtr _enter_action; - SharedPtr _exit_action; - Edges _edges; - Schrodinbit _changed; - bool _is_initial; - bool _is_selector; - bool _is_active; + TimeStamp _enter_time; ///< valid iff _is_active + TimeDuration _duration; + SPtr _enter_action; + SPtr _exit_action; + Edges _edges; + Schrodinbit _changed; + bool _is_initial; + bool _is_selector; + bool _is_active; }; } // namespace Machina diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index 2db4f45..3540b2b 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -38,8 +38,9 @@ using namespace std; namespace Machina { -Problem::Problem(TimeUnit unit, const std::string& target_midi, - SharedPtr seed) +Problem::Problem(TimeUnit unit, + const std::string& target_midi, + SPtr seed) : _unit(unit) , _target(*this) , _seed(new Machine(*seed.get())) @@ -86,7 +87,7 @@ Problem::fitness(const Machine& const_machine) const return cached->second; } - SharedPtr eval(new Evaluator(*this)); + SPtr eval(new Evaluator(*this)); //machine.reset(); machine.deactivate(); @@ -230,11 +231,11 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const // FIXME: ignores _seed and builds based on MIDI // evolution of the visible machine would be nice.. - SharedPtr base = SharedPtr(new Machine(_unit)); + SPtr base = SPtr(new Machine(_unit)); for (uint8_t i = 0; i < 128; ++i) { if (_target._counts[i] > 0) { //cout << "Initial note: " << (int)i << endl; - SharedPtr node(new Node(TimeDuration(_unit, 1 / 2.0))); + SPtr node(new Node(TimeDuration(_unit, 1 / 2.0))); node->set_enter_action(ActionFactory::note_on(i)); node->set_exit_action(ActionFactory::note_off(i)); node->set_selector(true); @@ -246,12 +247,12 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const // FIXME: double copy Machine m(*base.get()); - set< SharedPtr > unreachable; - SharedPtr initial; + set< SPtr > unreachable; + SPtr initial; for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); ++i) { - if (PtrCast((*i)->enter_action())->event()[1] == + if (dynamic_ptr_cast((*i)->enter_action())->event()[1] == _target.first_note()) { (*i)->set_initial(true); initial = *i; @@ -260,9 +261,9 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const } } - SharedPtr cur = initial; + SPtr cur = initial; unreachable.erase(cur); - SharedPtr head; + SPtr head; while (!unreachable.empty()) { if (rand() % 2) { @@ -272,7 +273,7 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const } if (!head->connected_to(head) ) { - cur->add_edge(SharedPtr(new Edge(cur, head))); + cur->add_edge(SPtr(new Edge(cur, head))); unreachable.erase(head); cur = head; } diff --git a/src/engine/Problem.hpp b/src/engine/Problem.hpp index a5fe853..7d96250 100644 --- a/src/engine/Problem.hpp +++ b/src/engine/Problem.hpp @@ -29,10 +29,12 @@ namespace Machina { class Problem : public Eugene::Problem { public: - Problem(TimeUnit unit, const std::string& target_midi, SharedPtr seed = SharedPtr()); + Problem(TimeUnit unit, + const std::string& target_midi, + SPtr seed = SPtr()); virtual ~Problem() {} - void seed(SharedPtr parent) { _seed = parent; } + void seed(SPtr parent) { _seed = parent; } float fitness(const Machine& machine) const; @@ -109,8 +111,8 @@ private: TimeUnit _unit; - Evaluator _target; - SharedPtr _seed; + Evaluator _target; + SPtr _seed; /// for levenshtein distance mutable std::vector< std::vector > _matrix; diff --git a/src/engine/Recorder.cpp b/src/engine/Recorder.cpp index 8bc9a4a..df9a200 100644 --- a/src/engine/Recorder.cpp +++ b/src/engine/Recorder.cpp @@ -34,8 +34,7 @@ Recorder::Recorder(Raul::Forge& forge, : _forge(forge) , _unit(unit) , _record_buffer(buffer_size) - , _builder(new MachineBuilder( - SharedPtr(new Machine(unit)), q, step)) + , _builder(new MachineBuilder(SPtr(new Machine(unit)), q, step)) {} void @@ -62,10 +61,10 @@ Recorder::_whipped() } } -SharedPtr +SPtr Recorder::finish() { - SharedPtr machine = _builder->finish(); + SPtr machine = _builder->finish(); _builder.reset(); return machine; } diff --git a/src/engine/Recorder.hpp b/src/engine/Recorder.hpp index ce9cad8..62295cf 100644 --- a/src/engine/Recorder.hpp +++ b/src/engine/Recorder.hpp @@ -18,10 +18,10 @@ #ifndef MACHINA_RECORDER_HPP #define MACHINA_RECORDER_HPP -#include "raul/SharedPtr.hpp" #include "raul/RingBuffer.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Slave.hpp" @@ -52,15 +52,15 @@ public: } } - SharedPtr finish(); + SPtr finish(); private: virtual void _whipped(); - Raul::Forge& _forge; - TimeUnit _unit; - Raul::RingBuffer _record_buffer; - SharedPtr _builder; + Raul::Forge& _forge; + TimeUnit _unit; + Raul::RingBuffer _record_buffer; + SPtr _builder; }; } // namespace Machina diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp index da7fecb..7401755 100644 --- a/src/engine/SMFDriver.cpp +++ b/src/engine/SMFDriver.cpp @@ -20,10 +20,9 @@ #include -#include "raul/SharedPtr.hpp" - #include "machina/Context.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "SMFDriver.hpp" @@ -36,9 +35,9 @@ using namespace std; namespace Machina { SMFDriver::SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit) - : Driver(forge, SharedPtr()) + : Driver(forge, SPtr()) { - _writer = SharedPtr(new SMFWriter(unit)); + _writer = SPtr(new SMFWriter(unit)); } /** Learn a single track from the MIDI file at @a uri @@ -48,23 +47,25 @@ SMFDriver::SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit) * Currently only file:// URIs are supported. * @return the resulting machine. */ -SharedPtr -SMFDriver::learn(const string& filename, unsigned track, double q, +SPtr +SMFDriver::learn(const string& filename, + unsigned track, + double q, Raul::TimeDuration max_duration) { //assert(q.unit() == max_duration.unit()); - SharedPtr m(new Machine(max_duration.unit())); - SharedPtr builder = SharedPtr( + SPtr m(new Machine(max_duration.unit())); + SPtr builder = SPtr( new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { cerr << "Unable to open MIDI file " << filename << endl; - return SharedPtr(); + return SPtr(); } if (track > reader.num_tracks()) { - return SharedPtr(); + return SPtr(); } else { learn_track(builder, reader, track, q, max_duration); } @@ -74,7 +75,7 @@ SMFDriver::learn(const string& filename, unsigned track, double q, if (m->nodes().size() > 1) { return m; } else { - return SharedPtr(); + return SPtr(); } } @@ -82,17 +83,17 @@ SMFDriver::learn(const string& filename, unsigned track, double q, * * This will result in one disjoint subgraph in the machine for each track. */ -SharedPtr +SPtr SMFDriver::learn(const string& filename, double q, Raul::TimeStamp max_duration) { - SharedPtr m(new Machine(max_duration.unit())); - SharedPtr builder = SharedPtr( + SPtr m(new Machine(max_duration.unit())); + SPtr builder = SPtr( new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { cerr << "Unable to open MIDI file " << filename << endl; - return SharedPtr(); + return SPtr(); } for (unsigned t = 1; t <= reader.num_tracks(); ++t) { @@ -105,16 +106,16 @@ SMFDriver::learn(const string& filename, double q, Raul::TimeStamp max_duration) if (m->nodes().size() > 1) { return m; } else { - return SharedPtr(); + return SPtr(); } } void -SMFDriver::learn_track(SharedPtr builder, - SMFReader& reader, - unsigned track, - double q, - Raul::TimeDuration max_duration) +SMFDriver::learn_track(SPtr builder, + SMFReader& reader, + unsigned track, + double q, + Raul::TimeDuration max_duration) { const bool found_track = reader.seek_to_track(track); if (!found_track) { @@ -153,7 +154,7 @@ SMFDriver::learn_track(SharedPtr builder, } void -SMFDriver::run(SharedPtr machine, Raul::TimeStamp max_time) +SMFDriver::run(SPtr machine, Raul::TimeStamp max_time) { // FIXME: unit kludge (tempo only) Context context(_forge, machine->time().unit().ppt(), @@ -161,7 +162,7 @@ SMFDriver::run(SharedPtr machine, Raul::TimeStamp max_time) context.set_sink(this); context.time().set_slice(TimeStamp(max_time.unit(), 0, 0), context.time().beats_to_ticks(max_time)); - machine->run(context, SharedPtr()); + machine->run(context, SPtr()); } } // namespace Machina diff --git a/src/engine/SMFDriver.hpp b/src/engine/SMFDriver.hpp index abed40d..ee3c220 100644 --- a/src/engine/SMFDriver.hpp +++ b/src/engine/SMFDriver.hpp @@ -20,8 +20,6 @@ #include -#include "raul/SharedPtr.hpp" - #include "machina/Driver.hpp" #include "machina/types.hpp" @@ -40,16 +38,16 @@ class SMFDriver public: SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit); - SharedPtr learn(const std::string& filename, - double q, - Raul::TimeDuration max_duration); + SPtr learn(const std::string& filename, + double q, + Raul::TimeDuration max_duration); - SharedPtr learn(const std::string& filename, - unsigned track, - double q, - Raul::TimeDuration max_duration); + SPtr learn(const std::string& filename, + unsigned track, + double q, + Raul::TimeDuration max_duration); - void run(SharedPtr machine, Raul::TimeStamp max_time); + void run(SPtr machine, Raul::TimeStamp max_time); void write_event(Raul::TimeStamp time, size_t ev_size, @@ -59,16 +57,16 @@ public: void set_bpm(double /*bpm*/) {} void set_quantization(double /*quantization*/) {} - SharedPtr writer() { return _writer; } + SPtr writer() { return _writer; } private: - SharedPtr _writer; + SPtr _writer; - void learn_track(SharedPtr builder, - SMFReader& reader, - unsigned track, - double q, - Raul::TimeDuration max_duration); + void learn_track(SPtr builder, + SMFReader& reader, + unsigned track, + double q, + Raul::TimeDuration max_duration); }; } // namespace Machina diff --git a/src/engine/Updates.cpp b/src/engine/Updates.cpp index 16717e4..275ca2b 100644 --- a/src/engine/Updates.cpp +++ b/src/engine/Updates.cpp @@ -18,7 +18,6 @@ #include "lv2/lv2plug.in/ns/ext/atom/atom.h" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" #include "machina/Updates.hpp" @@ -26,10 +25,10 @@ namespace Machina { void -write_set(SharedPtr buf, - uint64_t subject, - URIInt key, - const Raul::Atom& value) +write_set(SPtr buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value) { const uint32_t update_type = UPDATE_SET; buf->write(sizeof(update_type), &update_type); @@ -42,10 +41,10 @@ write_set(SharedPtr buf, } uint32_t -read_set(SharedPtr buf, - uint64_t* subject, - URIInt* key, - Raul::Atom* value) +read_set(SPtr buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value) { uint32_t update_type; buf->read(sizeof(update_type), &update_type); 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 -#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, Client::ClientModel& client_model); + Controller(SPtr 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 maid, uint64_t node_id); + void learn(SPtr maid, uint64_t node_id); void disconnect(uint64_t tail_id, uint64_t head_id); void erase(uint64_t id); - void announce(SharedPtr machine); + void announce(SPtr machine); void process_updates(); private: - SharedPtr find(uint64_t id); + SPtr find(uint64_t id); struct StatefulComparator { - inline bool operator()(SharedPtr lhs, - SharedPtr rhs) const { + inline bool operator()(SPtr lhs, + SPtr rhs) const { return lhs->id() < rhs->id(); } }; - typedef std::set, StatefulComparator> Objects; + typedef std::set, StatefulComparator> Objects; Objects _objects; - SharedPtr _engine; + SPtr _engine; Client::ClientModel& _client_model; - SharedPtr _updates; + SPtr _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) + Driver(Raul::Forge& forge, SPtr machine) : _forge(forge) , _machine(machine) {} virtual ~Driver() {} - SharedPtr machine() { return _machine; } + SPtr machine() { return _machine; } - virtual void set_machine(SharedPtr machine) { + virtual void set_machine(SPtr machine) { _machine = machine; } - SharedPtr update_sink() { return _updates; } + SPtr update_sink() { return _updates; } - void set_update_sink(SharedPtr b) { + void set_update_sink(SPtr b) { _updates = b; } @@ -65,9 +65,9 @@ public: virtual void finish_record() {} protected: - Raul::Forge& _forge; - SharedPtr _machine; - SharedPtr _updates; + Raul::Forge& _forge; + SPtr _machine; + SPtr _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 #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, - Sord::World& rdf_world); + Engine(Raul::Forge& forge, + SPtr driver, + Sord::World& rdf_world); Sord::World& rdf_world() { return _rdf_world; } - static SharedPtr new_driver(Raul::Forge& forge, - const std::string& name, - SharedPtr machine); + static SPtr new_driver(Raul::Forge& forge, + const std::string& name, + SPtr machine); - SharedPtr driver() { return _driver; } - SharedPtr machine() { return _driver->machine(); } - Raul::Forge& forge() { return _forge; } + SPtr driver() { return _driver; } + SPtr machine() { return _driver->machine(); } + Raul::Forge& forge() { return _forge; } - SharedPtr load_machine(const Glib::ustring& uri); - SharedPtr load_machine_midi(const Glib::ustring& uri, - double q, - Raul::TimeDuration dur); + SPtr load_machine(const Glib::ustring& uri); + SPtr load_machine_midi(const Glib::ustring& uri, + double q, + Raul::TimeDuration dur); - void import_machine(SharedPtr machine); + void import_machine(SPtr 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; - Sord::World& _rdf_world; - Loader _loader; - Raul::Forge _forge; + SPtr _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 seed); + Evolver(Raul::TimeUnit unit, + const string& target_midi, + SPtr seed); - void seed(SharedPtr parent); + void seed(SPtr parent); bool improvement() { return _improvement; } - SharedPtr best() { return _ga->best(); } + SPtr best() { return _ga->best(); } typedef Eugene::GAImpl MachinaGA; private: void _run(); - SharedPtr _ga; - SharedPtr _problem; - float _seed_fitness; - Schrodinbit _improvement; + SPtr _ga; + SPtr _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 +#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 load(const Glib::ustring& filename); + SPtr 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 +#include #include +#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); - void remove_node(SharedPtr node); - void learn(SharedPtr maid, SharedPtr node); + void add_node(SPtr node); + void remove_node(SPtr node); + void learn(SPtr maid, SPtr node); void write_state(Sord::Model& model); // Audio context void reset(MIDISink* sink, Raul::TimeStamp time); - uint32_t run(Context& context, SharedPtr updates); + uint32_t run(Context& context, SPtr updates); // Any context inline Raul::TimeStamp time() const { return _time; } - SharedPtr pending_learn() { return _pending_learn; } - void clear_pending_learn() { _pending_learn.reset(); } + SPtr pending_learn() { return _pending_learn; } + void clear_pending_learn() { _pending_learn.reset(); } - typedef std::list< SharedPtr > Nodes; + typedef std::set< SPtr > Nodes; Nodes& nodes() { return _nodes; } const Nodes& nodes() const { return _nodes; } - SharedPtr random_node(); - SharedPtr random_edge(); + SPtr random_node(); + SPtr random_edge(); private: // Audio context - SharedPtr earliest_node() const; + SPtr earliest_node() const; + + bool enter_node(Context& context, + SPtr node, + SPtr updates); - bool enter_node(Context& context, - SharedPtr node, - SharedPtr updates); + void exit_node(Context& context, + SPtr node, + SPtr updates); - void exit_node(Context& context, - SharedPtr node, - SharedPtr updates); + static const size_t MAX_ACTIVE_NODES = 128; - static const size_t MAX_ACTIVE_NODES = 128; - std::vector< SharedPtr > _active_nodes; + std::vector< SPtr > _active_nodes; - SharedPtr _pending_learn; - Nodes _nodes; - Raul::TimeStamp _time; - bool _is_activated; - bool _is_finished; + SPtr _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 #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" @@ -32,16 +31,16 @@ enum UpdateType { }; void -write_set(SharedPtr buf, - uint64_t subject, - URIInt key, - const Raul::Atom& value); +write_set(SPtr buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value); uint32_t -read_set(SharedPtr buf, - uint64_t* subject, - URIInt* key, - Raul::Atom* value); +read_set(SPtr 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 + #include "raul/RingBuffer.hpp" namespace Machina { @@ -28,6 +30,30 @@ typedef Raul::RingBuffer UpdateBuffer; typedef uint32_t URIInt; +template +using SPtr = std::shared_ptr; + +template +using WPtr = std::weak_ptr; + +template +void NullDeleter(T* ptr) {} + +template +SPtr static_ptr_cast(const SPtr& r) { + return std::static_pointer_cast(r); +} + +template +SPtr dynamic_ptr_cast(const SPtr& r) { + return std::dynamic_pointer_cast(r); +} + +template +SPtr const_ptr_cast(const SPtr& r) { + return std::const_pointer_cast(r); +} + } // namespace Machina #endif // MACHINA_TYPES_HPP diff --git a/src/gui/EdgeView.cpp b/src/gui/EdgeView.cpp index afa8cfd..dd28a41 100644 --- a/src/gui/EdgeView.cpp +++ b/src/gui/EdgeView.cpp @@ -25,7 +25,8 @@ #include "MachinaGUI.hpp" #include "NodeView.hpp" -using Machina::URIs; +namespace Machina { +namespace GUI { /* probability colour stuff */ @@ -63,10 +64,10 @@ inline static uint32_t edge_color(float prob) using namespace Ganv; -EdgeView::EdgeView(Canvas& canvas, - SharedPtr src, - SharedPtr dst, - SharedPtr edge) +EdgeView::EdgeView(Canvas& canvas, + SPtr src, + SPtr dst, + SPtr edge) : Ganv::Edge(canvas, src.get(), dst.get(), 0x9FA0A0F4, true, false) , _edge(edge) { @@ -132,3 +133,6 @@ EdgeView::on_property(Machina::URIInt key, const Raul::Atom& value) set_color(edge_color(value.get_float())); } } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/EdgeView.hpp b/src/gui/EdgeView.hpp index b532071..bafcb6b 100644 --- a/src/gui/EdgeView.hpp +++ b/src/gui/EdgeView.hpp @@ -24,6 +24,9 @@ #include "machina/types.hpp" +namespace Machina { +namespace GUI { + class NodeView; class EdgeView @@ -31,10 +34,10 @@ class EdgeView , public Machina::Client::ClientObject::View { public: - EdgeView(Ganv::Canvas& canvas, - SharedPtr src, - SharedPtr dst, - SharedPtr edge); + EdgeView(Ganv::Canvas& canvas, + SPtr src, + SPtr dst, + SPtr edge); void show_label(bool show); @@ -46,7 +49,10 @@ private: float probability() const; - SharedPtr _edge; + SPtr _edge; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_EDGEVIEW_HPP diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp index 2501cec..95a3f14 100644 --- a/src/gui/MachinaCanvas.cpp +++ b/src/gui/MachinaCanvas.cpp @@ -17,13 +17,13 @@ #include -#include "raul/SharedPtr.hpp" #include "raul/TimeStamp.hpp" -#include "machina/Engine.hpp" -#include "machina/Controller.hpp" -#include "client/ClientObject.hpp" #include "client/ClientModel.hpp" +#include "client/ClientObject.hpp" +#include "machina/Controller.hpp" +#include "machina/Engine.hpp" +#include "machina/types.hpp" #include "EdgeView.hpp" #include "MachinaCanvas.hpp" @@ -32,7 +32,9 @@ using namespace Raul; using namespace Ganv; -using namespace Machina; + +namespace Machina { +namespace GUI { MachinaCanvas::MachinaCanvas(MachinaGUI* app, int width, int height) : Canvas(width, height) @@ -45,9 +47,9 @@ MachinaCanvas::MachinaCanvas(MachinaGUI* app, int width, int height) } bool -MachinaCanvas::node_clicked(WeakPtr item, GdkEventButton* event) +MachinaCanvas::node_clicked(WPtr item, GdkEventButton* event) { - SharedPtr node = PtrCast(item.lock()); + SPtr node = dynamic_ptr_cast(item.lock()); if (!node) { return false; } @@ -63,7 +65,7 @@ MachinaCanvas::node_clicked(WeakPtr item, GdkEventButton* event) } else if (event->button == 1) { // Left click: connect/disconnect - SharedPtr last = _last_clicked.lock(); + SPtr last = _last_clicked.lock(); if (last) { if (node != last) { @@ -101,7 +103,7 @@ MachinaCanvas::on_event(GdkEvent* event) } void -MachinaCanvas::on_new_object(SharedPtr object) +MachinaCanvas::on_new_object(SPtr object) { const Machina::URIs& uris = URIs::instance(); const Raul::Atom& type = object->get(uris.rdf_type); @@ -110,7 +112,7 @@ MachinaCanvas::on_new_object(SharedPtr object) } if (type.get() == uris.machina_Node) { - SharedPtr view( + SPtr view( new NodeView(_app->window(), *this, object, object->get(uris.machina_canvas_x).get_float(), object->get(uris.machina_canvas_y).get_float())); @@ -120,28 +122,25 @@ MachinaCanvas::on_new_object(SharedPtr object) view->signal_clicked().connect( sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked), - WeakPtr(view))); + WPtr(view))); object->set_view(view); } else if (type.get() == uris.machina_Edge) { - SharedPtr tail - = _app->client_model()->find( - object->get(uris.machina_tail_id).get_int32()); - SharedPtr head - = _app->client_model()->find( - object->get(uris.machina_head_id).get_int32()); + SPtr tail = _app->client_model()->find( + object->get(uris.machina_tail_id).get_int32()); + SPtr head = _app->client_model()->find( + object->get(uris.machina_head_id).get_int32()); if (!tail || !head) { std::cerr << "Invalid arc" << std::endl; return; } - SharedPtr tail_view = PtrCast(tail->view()); - SharedPtr head_view = PtrCast(head->view()); + SPtr tail_view = dynamic_ptr_cast(tail->view()); + SPtr head_view = dynamic_ptr_cast(head->view()); - SharedPtr view( - new EdgeView(*this, tail_view, head_view, object)); + SPtr view(new EdgeView(*this, tail_view, head_view, object)); object->set_view(view); @@ -151,13 +150,13 @@ MachinaCanvas::on_new_object(SharedPtr object) } void -MachinaCanvas::on_erase_object(SharedPtr object) +MachinaCanvas::on_erase_object(SPtr object) { const Raul::Atom& type = object->get(URIs::instance().rdf_type); if (type.get() == URIs::instance().machina_Node) { // Destruction of the view will remove from the canvas } else if (type.get() == URIs::instance().machina_Edge) { - object->set_view(SharedPtr()); + object->set_view(SPtr()); } else { std::cerr << "Unknown object type" << std::endl; } @@ -176,15 +175,18 @@ MachinaCanvas::action_create_node(double x, double y) } void -MachinaCanvas::action_connect(boost::shared_ptr src, - boost::shared_ptr head) +MachinaCanvas::action_connect(SPtr src, + SPtr head) { _app->controller()->connect(src->node()->id(), head->node()->id()); } void -MachinaCanvas::action_disconnect(boost::shared_ptr src, - boost::shared_ptr head) +MachinaCanvas::action_disconnect(SPtr src, + SPtr head) { _app->controller()->disconnect(src->node()->id(), head->node()->id()); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/MachinaCanvas.hpp b/src/gui/MachinaCanvas.hpp index 4f190fd..a9ca5b9 100644 --- a/src/gui/MachinaCanvas.hpp +++ b/src/gui/MachinaCanvas.hpp @@ -19,53 +19,56 @@ #define MACHINA_CANVAS_HPP_HPP #include -#include "raul/SharedPtr.hpp" -#include "raul/WeakPtr.hpp" + #include "ganv/Canvas.hpp" +#include "machina/types.hpp" using namespace Ganv; +namespace Machina { + +namespace Client { class ClientObject; } + +namespace GUI { + class MachinaGUI; class NodeView; -namespace Machina { -namespace Client { -class ClientObject; -} -} - class MachinaCanvas : public Canvas { public: MachinaCanvas(MachinaGUI* app, int width, int height); - //void build(SharedPtr machine, bool show_labels); + //void build(SPtr machine, bool show_labels); //void update_edges(); - void on_new_object(SharedPtr object); - void on_erase_object(SharedPtr object); + void on_new_object(SPtr object); + void on_erase_object(SPtr object); MachinaGUI* app() { return _app; } protected: bool on_event(GdkEvent* event); - bool node_clicked(WeakPtr item, GdkEventButton* ev); + bool node_clicked(WPtr item, GdkEventButton* ev); private: - //SharedPtr create_node_view(SharedPtr node); + //SPtr create_node_view(SPtr node); void action_create_node(double x, double y); - void action_connect(SharedPtr port1, - SharedPtr port2); + void action_connect(SPtr port1, + SPtr port2); - void action_disconnect(SharedPtr port1, - SharedPtr port2); + void action_disconnect(SPtr port1, + SPtr port2); - MachinaGUI* _app; - WeakPtr _last_clicked; + MachinaGUI* _app; + WPtr _last_clicked; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_CANVAS_HPP_HPP diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp index 6e3b188..f162ebd 100644 --- a/src/gui/MachinaGUI.cpp +++ b/src/gui/MachinaGUI.cpp @@ -38,9 +38,10 @@ #include "machina/Evolver.hpp" #endif -using namespace Machina; +namespace Machina { +namespace GUI { -MachinaGUI::MachinaGUI(SharedPtr engine) +MachinaGUI::MachinaGUI(SPtr engine) : _refresh(false) , _evolve(false) , _unit(TimeUnit::BEATS, 19200) @@ -49,8 +50,7 @@ MachinaGUI::MachinaGUI(SharedPtr engine) , _controller(new Machina::Controller(_engine, *_client_model.get())) , _maid(new Raul::Maid()) { - _canvas = boost::shared_ptr( - new MachinaCanvas(this, 1600*2, 1200*2)); + _canvas = SPtr(new MachinaCanvas(this, 1600*2, 1200*2)); Glib::RefPtr xml = WidgetFactory::create(); @@ -146,23 +146,30 @@ MachinaGUI::MachinaGUI(SharedPtr engine) _quantize_spinbutton->signal_changed().connect( sigc::mem_fun(this, &MachinaGUI::quantize_changed)); - _mutate_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::random_mutation), - SharedPtr())); - _compress_button->signal_clicked().connect(sigc::hide_return(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 0))); - _add_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 1)); - _remove_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 2)); - _adjust_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 3)); - _add_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 4)); - _remove_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 5)); - _adjust_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr(), 6)); + _mutate_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::random_mutation), + SPtr())); + _compress_button->signal_clicked().connect( + sigc::hide_return(sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 0))); + _add_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 1)); + _remove_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 2)); + _adjust_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 3)); + _add_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 4)); + _remove_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 5)); + _adjust_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr(), 6)); connect_widgets(); @@ -177,9 +184,8 @@ MachinaGUI::MachinaGUI(SharedPtr engine) // Idle callback to drive the maid (collect garbage) Glib::signal_timeout().connect( - sigc::bind_return( - sigc::mem_fun(_maid.get(), &Raul::Maid::cleanup), - true), + sigc::bind_return(sigc::mem_fun(_maid.get(), &Raul::Maid::cleanup), + true), 1000); // Idle callback to update node states @@ -289,16 +295,16 @@ void MachinaGUI::evolve_toggled() { if (_evolve_button->get_active()) { - _evolver = SharedPtr( + _evolver = SPtr( new Evolver(_unit, _target_filename, _engine->machine())); _evolve = true; stop_clicked(); - _engine->driver()->set_machine(SharedPtr()); + _engine->driver()->set_machine(SPtr()); _evolver->start(); } else { _evolver->stop(); _evolve = false; - SharedPtr new_machine = SharedPtr( + SPtr new_machine = SPtr( new Machine(*_evolver->best())); _engine->driver()->set_machine(new_machine); _canvas->build(new_machine, _menu_view_labels->get_active()); @@ -309,7 +315,7 @@ MachinaGUI::evolve_toggled() #endif void -MachinaGUI::random_mutation(SharedPtr machine) +MachinaGUI::random_mutation(SPtr machine) { if (!machine) machine = _engine->machine(); @@ -318,7 +324,7 @@ MachinaGUI::random_mutation(SharedPtr machine) } void -MachinaGUI::mutate(SharedPtr machine, unsigned mutation) +MachinaGUI::mutate(SPtr machine, unsigned mutation) { #if 0 if (!machine) @@ -423,7 +429,7 @@ MachinaGUI::menu_file_open() const int result = dialog.run(); if (result == Gtk::RESPONSE_OK) { - SharedPtr new_machine = _engine->load_machine(dialog.get_uri()); + SPtr new_machine = _engine->load_machine(dialog.get_uri()); if (new_machine) { _canvas->destroy(); _controller->announce(new_machine); @@ -530,7 +536,7 @@ MachinaGUI::menu_import_midi() const double length_dbl = length_sb->get_value_as_int(); const Raul::TimeStamp length(_unit, length_dbl); - SharedPtr machine = _engine->load_machine_midi( + SPtr machine = _engine->load_machine_midi( dialog.get_filename(), 0.0, length); if (machine) { @@ -689,13 +695,16 @@ MachinaGUI::play_toggled() } void -MachinaGUI::on_new_object(SharedPtr object) +MachinaGUI::on_new_object(SPtr object) { _canvas->on_new_object(object); } void -MachinaGUI::on_erase_object(SharedPtr object) +MachinaGUI::on_erase_object(SPtr object) { _canvas->on_erase_object(object); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp index c48ac93..f9b0857 100644 --- a/src/gui/MachinaGUI.hpp +++ b/src/gui/MachinaGUI.hpp @@ -22,52 +22,55 @@ #include -#include "raul/Maid.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeStamp.hpp" +#include "machina/types.hpp" #include "machina_config.h" using namespace std; namespace Machina { + class Machine; class Engine; class Evolver; class Controller; + namespace Client { -class ClientModel; class ClientObject; -} +class ClientModel; +class ClientObject; } +namespace GUI { + class MachinaCanvas; class MachinaGUI { public: - MachinaGUI(SharedPtr engine); + MachinaGUI(SPtr engine); ~MachinaGUI(); - boost::shared_ptr canvas() { return _canvas; } - boost::shared_ptr engine() { return _engine; } + SPtr canvas() { return _canvas; } + SPtr engine() { return _engine; } Raul::Forge& forge() { return _forge; } - SharedPtr maid() { return _maid; } + SPtr maid() { return _maid; } Gtk::Window* window() { return _main_window; } void attach(); void quit() { _main_window->hide(); } - SharedPtr controller() { return _controller; } + SPtr controller() { return _controller; } inline void queue_refresh() { _refresh = true; } - void on_new_object(SharedPtr object); - void on_erase_object(SharedPtr object); + void on_new_object(SPtr object); + void on_erase_object(SPtr object); - SharedPtr client_model() { + SPtr client_model() { return _client_model; } @@ -88,8 +91,8 @@ protected: void arrange(); void load_target_clicked(); - void random_mutation(SharedPtr machine); - void mutate(SharedPtr machine, unsigned mutation); + void random_mutation(SPtr machine); + void mutate(SPtr machine, unsigned mutation); void zoom(double z); void update_toolbar(); @@ -116,13 +119,13 @@ protected: Raul::TimeUnit _unit; - SharedPtr _canvas; - SharedPtr _engine; - SharedPtr _client_model; - SharedPtr _controller; + SPtr _canvas; + SPtr _engine; + SPtr _client_model; + SPtr _controller; - SharedPtr _maid; - SharedPtr _evolver; + SPtr _maid; + SPtr _evolver; Raul::Forge _forge; @@ -171,4 +174,7 @@ protected: Gtk::ToolButton* _adjust_edge_button; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_GUI_HPP diff --git a/src/gui/NodePropertiesWindow.cpp b/src/gui/NodePropertiesWindow.cpp index 9a76420..a58d649 100644 --- a/src/gui/NodePropertiesWindow.cpp +++ b/src/gui/NodePropertiesWindow.cpp @@ -20,7 +20,9 @@ #include "WidgetFactory.hpp" using namespace std; -using namespace Machina; + +namespace Machina { +namespace GUI { NodePropertiesWindow* NodePropertiesWindow::_instance = NULL; @@ -57,9 +59,9 @@ NodePropertiesWindow::apply_clicked() _node->set_enter_action(ActionFactory::note_on(note)); _node->set_exit_action(ActionFactory::note_off(note)); } else { - SharedPtr action = PtrCast(_node->enter_action()); + SPtr action = dynamic_ptr_cast(_node->enter_action()); action->event()[1] = note; - action = PtrCast(_node->exit_action()); + action = dynamic_ptr_cast(_node->exit_action()); action->event()[1] = note; } @@ -86,11 +88,11 @@ NodePropertiesWindow::ok_clicked() } void -NodePropertiesWindow::set_node(SharedPtr node) +NodePropertiesWindow::set_node(SPtr node) { _node = node; #if 0 - SharedPtr enter_action = PtrCast(node->enter_action()); + SPtr enter_action = dynamic_ptr_cast(node->enter_action()); if (enter_action && ( enter_action->event_size() > 1) && ( (enter_action->event()[0] & 0xF0) == 0x90) ) { _note_spinbutton->set_value(enter_action->event()[1]); @@ -106,8 +108,8 @@ NodePropertiesWindow::set_node(SharedPtr node) } void -NodePropertiesWindow::present(Gtk::Window* parent, - SharedPtr node) +NodePropertiesWindow::present(Gtk::Window* parent, + SPtr node) { if (!_instance) { Glib::RefPtr xml = WidgetFactory::create(); @@ -122,3 +124,6 @@ NodePropertiesWindow::present(Gtk::Window* parent, _instance->set_node(node); _instance->show(); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/NodePropertiesWindow.hpp b/src/gui/NodePropertiesWindow.hpp index 7d395c9..78a5285 100644 --- a/src/gui/NodePropertiesWindow.hpp +++ b/src/gui/NodePropertiesWindow.hpp @@ -20,13 +20,13 @@ #include -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" namespace Machina { -namespace Client { -class ClientObject; -} -} + +namespace Client { class ClientObject; } + +namespace GUI { class NodePropertiesWindow : public Gtk::Dialog @@ -37,11 +37,11 @@ public: ~NodePropertiesWindow(); - static void present(Gtk::Window* parent, - SharedPtr node); + static void present(Gtk::Window* parent, + SPtr node); private: - void set_node(SharedPtr node); + void set_node(SPtr node); void apply_clicked(); void cancel_clicked(); @@ -49,7 +49,7 @@ private: static NodePropertiesWindow* _instance; - SharedPtr _node; + SPtr _node; Gtk::SpinButton* _note_spinbutton; Gtk::SpinButton* _duration_spinbutton; @@ -58,4 +58,7 @@ private: Gtk::Button* _ok_button; }; +} // namespace Machina +} // namespace GUI + #endif // NODEPROPERTIESWINDOW_HPP diff --git a/src/gui/NodeView.cpp b/src/gui/NodeView.cpp index fc5377e..79258cd 100644 --- a/src/gui/NodeView.cpp +++ b/src/gui/NodeView.cpp @@ -27,13 +27,15 @@ #include "NodeView.hpp" using namespace std; -using Machina::URIs; -NodeView::NodeView(Gtk::Window* window, - Ganv::Canvas& canvas, - SharedPtr node, - double x, - double y) +namespace Machina { +namespace GUI { + +NodeView::NodeView(Gtk::Window* window, + Ganv::Canvas& canvas, + SPtr node, + double x, + double y) : Ganv::Circle(canvas, "", x, y, 20, false) , _window(window) , _node(node) @@ -172,3 +174,6 @@ NodeView::set_default_colors() set_fill_color(_default_fill_color); set_border_color(_default_border_color); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/NodeView.hpp b/src/gui/NodeView.hpp index b0c0da4..5507a75 100644 --- a/src/gui/NodeView.hpp +++ b/src/gui/NodeView.hpp @@ -24,18 +24,21 @@ #include "machina/types.hpp" +namespace Machina { +namespace GUI { + class NodeView : public Ganv::Circle , public Machina::Client::ClientObject::View { public: - NodeView(Gtk::Window* window, - Canvas& canvas, - SharedPtr node, - double x, - double y); + NodeView(Gtk::Window* window, + Canvas& canvas, + SPtr node, + double x, + double y); - SharedPtr node() { return _node; } + SPtr node() { return _node; } void show_label(bool show); @@ -55,15 +58,18 @@ private: bool node_is(Raul::Forge& forge, Machina::URIInt key); - Gtk::Window* _window; - SharedPtr _node; - uint32_t _default_border_color; - uint32_t _default_fill_color; + Gtk::Window* _window; + SPtr _node; + uint32_t _default_border_color; + uint32_t _default_fill_color; - SharedPtr _enter_action; - sigc::connection _enter_action_connection; + SPtr _enter_action; + sigc::connection _enter_action_connection; sigc::signal _signal_clicked; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_NODEVIEW_HPP diff --git a/src/gui/WidgetFactory.hpp b/src/gui/WidgetFactory.hpp index 41e8200..4f54998 100644 --- a/src/gui/WidgetFactory.hpp +++ b/src/gui/WidgetFactory.hpp @@ -23,6 +23,9 @@ #include "machina_config.h" +namespace Machina { +namespace GUI { + class WidgetFactory { public: @@ -58,3 +61,6 @@ public: } }; + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/main.cpp b/src/gui/main.cpp index e87e29d..25b1254 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -40,14 +40,14 @@ main(int argc, char** argv) Machina::URIs::init(); - SharedPtr machine; + SPtr machine; // Load machine, if given #if 0 if (argc >= 2) { const string filename = argv[1]; cout << "Building machine from MIDI file " << filename << endl; - SharedPtr file_driver(new Machina::SMFDriver()); + SPtr file_driver(new Machina::SMFDriver()); if (argc >= 3) { float q = strtof(argv[2], NULL); @@ -68,8 +68,7 @@ main(int argc, char** argv) Raul::Forge forge; if (!machine) { - machine = SharedPtr( - new Machine(TimeUnit(TimeUnit::BEATS, 19200))); + machine = SPtr(new Machine(TimeUnit(TimeUnit::BEATS, 19200))); } std::string driver_name = "smf"; @@ -78,13 +77,13 @@ main(int argc, char** argv) #endif // Build engine - SharedPtr driver(Engine::new_driver(forge, driver_name, machine)); - SharedPtr engine(new Engine(forge, driver, rdf_world)); + SPtr driver(Engine::new_driver(forge, driver_name, machine)); + SPtr engine(new Engine(forge, driver, rdf_world)); Gtk::Main app(argc, argv); driver->activate(); - MachinaGUI gui(engine); + GUI::MachinaGUI gui(engine); app.run(*gui.window()); diff --git a/src/machina.cpp b/src/machina.cpp index 4dc9ada..7772da5 100644 --- a/src/machina.cpp +++ b/src/machina.cpp @@ -58,7 +58,7 @@ main(int argc, char** argv) Glib::thread_init(); } - SharedPtr driver(new JackDriver()); + SPtr driver(new JackDriver()); Sord::World rdf_world; Engine engine(driver, rdf_world); diff --git a/src/midi2machina.cpp b/src/midi2machina.cpp index 3a7f055..7cd6242 100644 --- a/src/midi2machina.cpp +++ b/src/midi2machina.cpp @@ -51,9 +51,9 @@ main(int argc, char** argv) return -1; } - SharedPtr driver(new SMFDriver()); + SPtr driver(new SMFDriver()); - SharedPtr machine = driver->learn(argv[2], strtof(argv[1], NULL)); + SPtr machine = driver->learn(argv[2], strtof(argv[1], NULL)); if (!machine) { cout << "Failed to load MIDI file." << endl; -- cgit v1.2.1