OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
wrapped_parameter.hpp
1#pragma once
2#include <ossia/network/base/osc_address.hpp>
3#include <ossia/network/base/protocol.hpp>
4#include <ossia/network/generic/generic_parameter.hpp>
5
6namespace ossia::net
7{
8
9template <typename T, typename Parameter_T>
10class wrapped_node : public ossia::net::node_base
11{
13 ossia::net::node_base* m_parent{};
14 std::unique_ptr<Parameter_T> m_parameter;
15
16public:
17 using data_type = T;
18 wrapped_node(
19 T&& data, ossia::net::device_base& aDevice, ossia::net::node_base& aParent)
20 : m_device{aDevice}
21 , m_parent{&aParent}
22 {
23 m_name = data.name;
24 m_extended = data.extended;
25 m_oscAddressCache = ossia::net::osc_parameter_string(*this);
26 if(data.valid())
27 m_parameter.reset(new Parameter_T(std::move(data), *this));
28 }
29
30 wrapped_node(T&& data, ossia::net::device_base& aDevice)
31 : m_device{aDevice}
32 {
33 m_name = data.name;
34 m_extended = data.extended;
35 m_oscAddressCache = ossia::net::osc_parameter_string(*this);
36 if(data.valid())
37 m_parameter.reset(new Parameter_T(std::move(data), *this));
38 }
39
40 ~wrapped_node() override { clear(); }
41
42 void clear()
43 {
45
46 write_lock_t lock{m_mutex};
47 m_children.clear();
48 m_parameter.reset();
49 }
50
51 device_base& get_device() const final override { return m_device; }
52
53 node_base* get_parent() const final override { return m_parent; }
54
55 node_base& set_name(std::string) final override { return *this; }
56
57 parameter_base* get_parameter() const final override { return m_parameter.get(); }
58
59 parameter_base* create_parameter(val_type = val_type::IMPULSE) final override
60 {
61 return nullptr;
62 }
63
64 bool remove_parameter() final override { return false; }
65
66 void add_child(std::unique_ptr<ossia::net::node_base> p)
67 {
68 if(p)
69 {
70 write_lock_t lock{m_mutex};
71 m_children.push_back(std::move(p));
72 }
73 }
74
75private:
76 std::unique_ptr<node_base> make_child(const std::string& name) final override
77 {
78 return std::make_unique<generic_node>(name, m_device, *this);
79 return nullptr;
80 }
81
82 void removing_child(node_base& node_base) final override { }
83};
84
85template <typename T>
86class wrapped_parameter : public ossia::net::generic_parameter
87{
88public:
89 using base_data_type = typename T::base_data_type;
90
91 wrapped_parameter(T&& data, ossia::net::node_base& node_base)
92 : generic_parameter{data, node_base}
93 , m_data(std::move(data))
94 {
95 }
96
97 wrapped_parameter() = delete;
98 wrapped_parameter(const wrapped_parameter& other) = delete;
99 wrapped_parameter(wrapped_parameter&& other) = delete;
100 wrapped_parameter& operator=(const wrapped_parameter& other) = delete;
101 wrapped_parameter& operator=(wrapped_parameter&& other) = delete;
103
104 const base_data_type& data() const { return m_data; }
105
106 base_data_type& data() { return m_data; }
107
108private:
109 base_data_type m_data;
110};
111
112template <typename Node_T, typename Protocol_T>
113class wrapped_device final
115 , public Node_T
116{
117public:
118 wrapped_device() = delete;
119 wrapped_device(const wrapped_device&) = delete;
120 wrapped_device(wrapped_device&&) = delete;
121 wrapped_device& operator=(const wrapped_device&) = delete;
122 wrapped_device& operator=(wrapped_device&&) = delete;
123
124 wrapped_device(std::unique_ptr<Protocol_T> protocol_base, std::string name)
125 : device_base(std::move(protocol_base))
126 , Node_T{typename Node_T::data_type{name}, *this}
127 {
128 m_capabilities.change_tree = true;
129 m_protocol->set_device(*this);
130 }
131
132 const ossia::net::node_base& get_root_node() const override { return *this; }
133 ossia::net::node_base& get_root_node() override { return *this; }
134
135 using Node_T::get_name;
136 using Node_T::set_name;
137
138 ~wrapped_device()
139 {
140 m_protocol->stop();
141
142 this->remove_parameter();
143
144 {
145 write_lock_t lock{this->m_mutex};
146 this->m_children.clear();
147 }
148
149 // Parameters, etc of the device's own node must also be cleared
150 // before removing the protocol:
151 Node_T::clear();
152
153 m_protocol.reset();
154 }
155};
156}
void callbacks_clear()
clear Clears callbacks.
Definition callback_container.hpp:203
Root of a device tree.
Definition ossia/network/base/device.hpp:58
Definition generic_parameter.hpp:24
generic_parameter(ossia::net::node_base &node_base)
Used for repetition filter.
Definition generic_parameter.cpp:28
The node_base class.
Definition node.hpp:48
Nano::Signal< void(const node_base &)> about_to_be_deleted
The node subclasses must call this in their destructor.
Definition node.hpp:204
node_base * add_child(std::unique_ptr< node_base >)
Adds a new child if it can be added.
Definition node.cpp:140
val_type
Enum to represent the types that a value can take.
Definition parameter_properties.hpp:16
@ IMPULSE
array<float, 4>
bool change_tree
change_tree : nodes can be added and removed externally.
Definition ossia/network/base/device.hpp:24