OSSIA
Open Scenario System for Interactive Application
network/base/node.hpp
1 #pragma once
5 #include <ossia/detail/mutex.hpp>
7 #include <ossia/detail/string_view.hpp>
8 #include <ossia/network/base/name_validation.hpp>
9 #include <ossia/network/common/parameter_properties.hpp>
10 
11 #include <nano_signal_slot.hpp>
12 
13 #include <functional>
14 #include <memory>
15 #include <string>
16 #if defined(OSSIA_QT)
17 class QString;
18 #endif
19 namespace ossia
20 {
21 namespace net
22 {
23 class device_base;
24 class parameter_base;
25 class node_base;
47 class OSSIA_EXPORT node_base
48 {
49 public:
50  using children_t = std::vector<std::unique_ptr<node_base>>;
51  node_base() = default;
52  node_base(const node_base&) = delete;
53  node_base(node_base&&) = delete;
54  node_base& operator=(const node_base&) = delete;
55  node_base& operator=(node_base&&) = delete;
56 
57  virtual ~node_base();
58 
60  virtual device_base& get_device() const = 0;
61 
63  virtual node_base* get_parent() const = 0;
64 
70  const std::string& get_name() const { return m_name; }
71  virtual node_base& set_name(std::string) = 0;
72 
76  virtual void set_parameter(std::unique_ptr<ossia::net::parameter_base>);
77  virtual bool remove_parameter() = 0;
78  virtual parameter_base* get_parameter() const = 0;
79 
84  const extended_attributes& get_extended_attributes() const;
85  void set_extended_attributes(const extended_attributes&);
86 
99  ossia::any get_attribute(std::string_view str) const;
100 
101  template <typename T>
102  void set(std::string_view str, const T& val);
103  template <typename T>
104  void set(std::string_view str, T&& val);
105 
106  template <typename T>
107  void set(std::string_view str, const std::optional<T>& val);
108  template <typename T>
109  void set(std::string_view str, std::optional<T>&& val);
110 
111  void set(std::string_view str, bool value);
112 
113  template <typename Attribute, typename T>
114  void set(Attribute a, const T& value);
115  template <typename Attribute, typename T>
116  void set(Attribute a, T& value);
117  template <typename Attribute, typename T>
118  void set(Attribute a, T&& value);
119 
132  node_base* create_child(std::string name);
133 
140  node_base* add_child(std::unique_ptr<node_base>);
141 
150  node_base* find_child(std::string_view name);
151 #if defined(OSSIA_QT)
152  node_base* find_child(const QString& name);
153 #endif
154 
156  bool has_child(ossia::net::node_base&);
157 
158  bool remove_child(const std::string& name);
159  bool remove_child(const node_base& name);
160 
162  void clear_children();
163 
164  operator const extended_attributes&() const
165  {
166  return m_extended;
167  }
168  operator extended_attributes&()
169  {
170  return m_extended;
171  }
172 
173  locked_container<const children_t> children() const
174  {
175  return {m_children, m_mutex};
176  }
177 
179  const auto& unsafe_children() const TS_REQUIRES(m_mutex)
180  {
181  return m_children;
182  }
183  mutable shared_mutex_t m_mutex;
184 
186  std::vector<node_base*> children_copy() const;
187 
189  std::vector<std::string> children_names() const;
190 
192  bool is_root_instance(const ossia::net::node_base& child) const;
193 
194  const std::string& osc_address() const
195  {
196  return m_oscAddressCache;
197  }
198  virtual void on_address_change();
199 
201  mutable Nano::Signal<void(const node_base&)> about_to_be_deleted;
202 
203 protected:
205  virtual std::unique_ptr<node_base> make_child(const std::string& name) = 0;
206 
208  virtual void removing_child(node_base& node_base) = 0;
209 
210  std::string m_name;
211  children_t m_children TS_GUARDED_BY(m_mutex);
212  extended_attributes m_extended{0};
213  std::string m_oscAddressCache;
214 };
215 }
216 }
Thread-safe read-only reference to a container.
Definition: locked_container.hpp:15
Root of a device tree.
Definition: ossia/network/base/device.hpp:58
The node_base class.
Definition: network/base/node.hpp:48
virtual std::unique_ptr< node_base > make_child(const std::string &name)=0
Should return nullptr if no child is to be added.
virtual node_base * get_parent() const =0
Parent of this node. May be null if it is the device (i.e. root).
virtual void removing_child(node_base &node_base)=0
Reimplement for a specific removal action.
const std::string & get_name() const
The name of this node, e.g. "foo".
Definition: network/base/node.hpp:70
const auto & unsafe_children() const TS_REQUIRES(m_mutex)
Non mutex-protected version. With great powers, yada yada etc etc.
Definition: network/base/node.hpp:179
Nano::Signal< void(const node_base &)> about_to_be_deleted
The node subclasses must call this in their destructor.
Definition: network/base/node.hpp:201
virtual device_base & get_device() const =0
The device in which this node is.
virtual parameter_base * create_parameter(val_type=val_type::IMPULSE)=0
Allows a node to carry a value.
The parameter_base class.
Definition: ossia/network/base/parameter.hpp:48
The value class.
Definition: value.hpp:173
Definition: git_info.h:7
val_type
Enum to represent the types that a value can take.
Definition: parameter_properties.hpp:16
@ IMPULSE
array<float, 4>
auto get_attribute(const any_map &e, std::string_view name)
get_attribute Get an attribute of an any_map.
Definition: any_map.hpp:29