diff options
Diffstat (limited to 'src/engine/machina')
-rw-r--r-- | src/engine/machina/Atom.hpp | 217 | ||||
-rw-r--r-- | src/engine/machina/Context.hpp | 8 | ||||
-rw-r--r-- | src/engine/machina/Controller.hpp | 2 | ||||
-rw-r--r-- | src/engine/machina/Driver.hpp | 4 | ||||
-rw-r--r-- | src/engine/machina/Engine.hpp | 13 | ||||
-rw-r--r-- | src/engine/machina/Loader.hpp | 6 | ||||
-rw-r--r-- | src/engine/machina/Machine.hpp | 2 | ||||
-rw-r--r-- | src/engine/machina/URIs.hpp | 3 | ||||
-rw-r--r-- | src/engine/machina/Updates.hpp | 7 |
9 files changed, 238 insertions, 24 deletions
diff --git a/src/engine/machina/Atom.hpp b/src/engine/machina/Atom.hpp new file mode 100644 index 0000000..180fe1c --- /dev/null +++ b/src/engine/machina/Atom.hpp @@ -0,0 +1,217 @@ +/* + This file is part of Machina. + Copyright 2007-2013 David Robillard <http://drobilla.net> + + Raul is free software: you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation, either version 3 of the License, or any later version. + + Raul is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Raul. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef MACHINA_ATOM_HPP +#define MACHINA_ATOM_HPP + +#include <stdint.h> + +#include <cassert> +#include <cstdlib> +#include <cstring> +#include <string> + +namespace machina { + +class Forge; + +/** + A generic typed data container. + + An Atom holds a value with some type and size, both specified by a uint32_t. + Values with size less than sizeof(void*) are stored inline: no dynamic + allocation occurs so Atoms may be created in hard real-time threads. + Otherwise, if the size is larger than sizeof(void*), the value will be + dynamically allocated in a separate chunk of memory. +*/ +class Atom { +public: + Atom() : _size(0), _type(0) { _val._blob = NULL; } + ~Atom() { dealloc(); } + + typedef uint32_t TypeID; + + /** Contruct a raw atom. + * + * Typically this is not used directly, use Forge methods to make atoms. + */ + Atom(uint32_t size, TypeID type, const void* body) + : _size(size) + , _type(type) + { + if (is_reference()) { + _val._blob = malloc(size); + } + if (body) { + memcpy(get_body(), body, size); + } + } + + Atom(const Atom& copy) + : _size(copy._size) + , _type(copy._type) + { + if (is_reference()) { + _val._blob = malloc(_size); + memcpy(_val._blob, copy._val._blob, _size); + } else { + memcpy(&_val, ©._val, _size); + } + } + + Atom& operator=(const Atom& other) { + if (&other == this) { + return *this; + } + dealloc(); + _size = other._size; + _type = other._type; + if (is_reference()) { + _val._blob = malloc(_size); + memcpy(_val._blob, other._val._blob, _size); + } else { + memcpy(&_val, &other._val, _size); + } + return *this; + } + + inline bool operator==(const Atom& other) const { + if (_type == other.type() && _size == other.size()) { + if (is_reference()) { + return !memcmp(_val._blob, other._val._blob, _size); + } else { + return !memcmp(&_val, &other._val, _size); + } + } + return false; + } + + inline bool operator!=(const Atom& other) const { + return !operator==(other); + } + + inline bool operator<(const Atom& other) const { + if (_type == other.type()) { + if (is_reference()) { + return memcmp(_val._blob, other._val._blob, _size) < 0; + } else { + return memcmp(&_val, &other._val, _size) < 0; + } + } + return _type < other.type(); + } + + inline uint32_t size() const { return _size; } + inline bool is_valid() const { return _type; } + inline TypeID type() const { return _type; } + + inline const void* get_body() const { + return is_reference() ? _val._blob : &_val; + } + + inline void* get_body() { + return is_reference() ? _val._blob : &_val; + } + + template <typename T> const T& get() const { + assert(size() == sizeof(T)); + return *static_cast<const T*>(get_body()); + } + + template <typename T> const T* ptr() const { + return static_cast<const T*>(get_body()); + } + +private: + friend class Forge; + + /** Free dynamically allocated value, if applicable. */ + inline void dealloc() { + if (is_reference()) { + free(_val._blob); + } + } + + /** Return true iff this value is dynamically allocated. */ + inline bool is_reference() const { + return _size > sizeof(_val); + } + + uint32_t _size; + TypeID _type; + + union { + intptr_t _val; + void* _blob; + } _val; +}; + +class Forge { +public: + Forge() + : Int(1) + , Float(2) + , Bool(3) + , URI(4) + , URID(5) + , String(6) + {} + + virtual ~Forge() {} + + Atom make() { return Atom(); } + Atom make(int32_t v) { return Atom(sizeof(v), Int, &v); } + Atom make(float v) { return Atom(sizeof(v), Float, &v); } + Atom make(bool v) { + const int32_t iv = v ? 1 : 0; + return Atom(sizeof(int32_t), Bool, &iv); + } + + Atom make_urid(int32_t v) { return Atom(sizeof(int32_t), URID, &v); } + + Atom alloc(uint32_t size, uint32_t type, const void* val) { + return Atom(size, type, val); + } + + Atom alloc(const char* v) { + const size_t len = strlen(v); + return Atom(len + 1, String, v); + } + + Atom alloc(const std::string& v) { + return Atom(v.length() + 1, String, v.c_str()); + } + + Atom alloc_uri(const char* v) { + const size_t len = strlen(v); + return Atom(len + 1, URI, v); + } + + Atom alloc_uri(const std::string& v) { + return Atom(v.length() + 1, URI, v.c_str()); + } + + Atom::TypeID Int; + Atom::TypeID Float; + Atom::TypeID Bool; + Atom::TypeID URI; + Atom::TypeID URID; + Atom::TypeID String; +}; + +} // namespace machina + +#endif // MACHINA_ATOM_HPP diff --git a/src/engine/machina/Context.hpp b/src/engine/machina/Context.hpp index e0ca423..766975b 100644 --- a/src/engine/machina/Context.hpp +++ b/src/engine/machina/Context.hpp @@ -17,8 +17,8 @@ #ifndef MACHINA_CONTEXT_HPP #define MACHINA_CONTEXT_HPP +#include "machina/Atom.hpp" #include "machina/types.hpp" -#include "raul/Atom.hpp" #include "raul/TimeSlice.hpp" namespace machina { @@ -28,20 +28,20 @@ class MIDISink; class Context { public: - Context(Raul::Forge& forge, uint32_t rate, uint32_t ppqn, double bpm) + Context(Forge& forge, uint32_t rate, uint32_t ppqn, double bpm) : _forge(forge) , _time(rate, ppqn, bpm) {} void set_sink(MIDISink* sink) { _sink = sink; } - Raul::Forge& forge() { return _forge; } + Forge& forge() { return _forge; } const Raul::TimeSlice& time() const { return _time; } Raul::TimeSlice& time() { return _time; } MIDISink* sink() { return _sink; } private: - Raul::Forge& _forge; + Forge& _forge; Raul::TimeSlice _time; MIDISink* _sink; }; diff --git a/src/engine/machina/Controller.hpp b/src/engine/machina/Controller.hpp index 7738f22..e745c9c 100644 --- a/src/engine/machina/Controller.hpp +++ b/src/engine/machina/Controller.hpp @@ -52,7 +52,7 @@ public: uint64_t create(const client::ClientObject& obj); uint64_t connect(uint64_t tail_id, uint64_t head_id); - void set_property(uint64_t object_id, URIInt key, const Raul::Atom& value); + void set_property(uint64_t object_id, URIInt key, const Atom& value); void learn(SPtr<Raul::Maid> maid, uint64_t node_id); void disconnect(uint64_t tail_id, uint64_t head_id); diff --git a/src/engine/machina/Driver.hpp b/src/engine/machina/Driver.hpp index d913741..546c4d6 100644 --- a/src/engine/machina/Driver.hpp +++ b/src/engine/machina/Driver.hpp @@ -30,7 +30,7 @@ class Machine; class Driver : public MIDISink { public: - Driver(Raul::Forge& forge, SPtr<Machine> machine) + Driver(Forge& forge, SPtr<Machine> machine) : _forge(forge) , _machine(machine) , _play_state(PlayState::STOPPED) @@ -72,7 +72,7 @@ public: PlayState play_state() const { return _play_state; } protected: - Raul::Forge& _forge; + Forge& _forge; SPtr<Machine> _machine; SPtr<Raul::RingBuffer> _updates; PlayState _play_state; diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index b3c6010..4bd907b 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -21,11 +21,10 @@ #include <glibmm/ustring.h> -#include "raul/Atom.hpp" - -#include "machina/types.hpp" +#include "machina/Atom.hpp" #include "machina/Driver.hpp" #include "machina/Loader.hpp" +#include "machina/types.hpp" namespace machina { @@ -34,19 +33,19 @@ class Machine; class Engine { public: - Engine(Raul::Forge& forge, + Engine(Forge& forge, SPtr<Driver> driver, Sord::World& rdf_world); Sord::World& rdf_world() { return _rdf_world; } - static SPtr<Driver> new_driver(Raul::Forge& forge, + static SPtr<Driver> new_driver(Forge& forge, const std::string& name, SPtr<Machine> machine); SPtr<Driver> driver() { return _driver; } SPtr<Machine> machine() { return _driver->machine(); } - Raul::Forge& forge() { return _forge; } + Forge& forge() { return _forge; } SPtr<Machine> load_machine(const Glib::ustring& uri); SPtr<Machine> load_machine_midi(const Glib::ustring& uri, @@ -63,7 +62,7 @@ private: SPtr<Driver> _driver; Sord::World& _rdf_world; Loader _loader; - Raul::Forge _forge; + Forge _forge; }; } // namespace machina diff --git a/src/engine/machina/Loader.hpp b/src/engine/machina/Loader.hpp index 548e23a..0b1e9ec 100644 --- a/src/engine/machina/Loader.hpp +++ b/src/engine/machina/Loader.hpp @@ -19,8 +19,8 @@ #include <glibmm/ustring.h> +#include "machina/Atom.hpp" #include "machina/types.hpp" -#include "raul/Atom.hpp" #include "raul/TimeStamp.hpp" #include "sord/sordmm.hpp" @@ -33,7 +33,7 @@ class Machine; class Loader { public: - Loader(Raul::Forge& forge, Sord::World& rdf_world); + Loader(Forge& forge, Sord::World& rdf_world); SPtr<Machine> load(const Glib::ustring& filename); @@ -42,7 +42,7 @@ public: Raul::TimeDuration dur); private: - Raul::Forge& _forge; + Forge& _forge; Sord::World& _rdf_world; }; diff --git a/src/engine/machina/Machine.hpp b/src/engine/machina/Machine.hpp index d712c33..7c9855d 100644 --- a/src/engine/machina/Machine.hpp +++ b/src/engine/machina/Machine.hpp @@ -21,7 +21,7 @@ #include <set> #include "machina/types.hpp" -#include "raul/Atom.hpp" +#include "machina/Atom.hpp" #include "raul/RingBuffer.hpp" #include "raul/TimeSlice.hpp" #include "sord/sordmm.hpp" diff --git a/src/engine/machina/URIs.hpp b/src/engine/machina/URIs.hpp index eba1692..f770723 100644 --- a/src/engine/machina/URIs.hpp +++ b/src/engine/machina/URIs.hpp @@ -19,8 +19,7 @@ #include <stdint.h> -#include "raul/Atom.hpp" - +#include "machina/Atom.hpp" #include "machina/types.hpp" #define MACHINA_URI_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" diff --git a/src/engine/machina/Updates.hpp b/src/engine/machina/Updates.hpp index fe68855..7c0391b 100644 --- a/src/engine/machina/Updates.hpp +++ b/src/engine/machina/Updates.hpp @@ -19,8 +19,7 @@ #include <stdint.h> -#include "raul/Atom.hpp" - +#include "machina/Atom.hpp" #include "machina/types.hpp" namespace machina { @@ -33,13 +32,13 @@ void write_set(SPtr<Raul::RingBuffer> buf, uint64_t subject, URIInt key, - const Raul::Atom& value); + const Atom& value); uint32_t read_set(SPtr<Raul::RingBuffer> buf, uint64_t* subject, URIInt* key, - Raul::Atom* value); + Atom* value); } // namespace machina |