OSSIA
Open Scenario System for Interactive Application
expression_fwd.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <ossia/detail/audio_spin_mutex.hpp>
6 #include <ossia/detail/nullable_variant.hpp>
7 #include <ossia/detail/small_vector.hpp>
8 
9 #include <smallfun.hpp>
10 
11 #include <memory>
12 
16 namespace ossia
17 {
18 namespace expressions
19 {
20 
25  = smallfun::function<void(bool), smallfun::DefaultSize * 2>;
26 
27 class expression_callback_container
28 {
29  using mutex = ossia::audio_spin_mutex;
30  using lock_guard = std::lock_guard<ossia::audio_spin_mutex>;
31 
32 public:
34  expression_callback_container() noexcept = default;
35  expression_callback_container(const expression_callback_container& other) = delete;
36  expression_callback_container(expression_callback_container&& other) noexcept = delete;
37  expression_callback_container& operator=(const expression_callback_container& other)
38  = delete;
39  expression_callback_container&
40  operator=(expression_callback_container&& other) noexcept
41  = delete;
42 
43  virtual ~expression_callback_container() = default;
44 
50  using impl = typename ossia::static_vector<T, 2>;
51  using iterator = typename impl::iterator;
52 
58  iterator add_callback(T&& callback)
59  {
60  if(callback.allocated())
61  {
62  lock_guard lck{m_mutx};
63  auto it = m_callbacks.insert(m_callbacks.begin(), std::move(callback));
64  if(m_callbacks.size() == 1)
65  on_first_callback_added();
66  return it;
67  }
68  else
69  {
70 #if defined(__cpp_exceptions)
71  throw invalid_callback_error{};
72 #else
73  return {};
74 #endif
75  }
76  }
77 
82  void remove_callback(iterator it)
83  {
84  lock_guard lck{m_mutx};
85  if(m_callbacks.size() == 1)
86  on_removing_last_callback();
87  m_callbacks.erase(it);
88  }
89 
94  std::size_t callback_count() const
95  {
96  lock_guard lck{m_mutx};
97  return m_callbacks.size();
98  }
99 
104  bool callbacks_empty() const
105  {
106  lock_guard lck{m_mutx};
107  return m_callbacks.empty();
108  }
109 
114  template <typename... Args>
115  void send(Args&&... args)
116  {
117  lock_guard lck{m_mutx};
118  for(auto& callback : m_callbacks)
119  {
120  if(callback)
121  callback(std::forward<Args>(args)...);
122  }
123  }
124 
128  void callbacks_clear()
129  {
130  lock_guard lck{m_mutx};
131  if(!m_callbacks.empty())
132  on_removing_last_callback();
133  m_callbacks.clear();
134  }
135 
136 protected:
147  virtual void on_first_callback_added() { }
148 
154  virtual void on_removing_last_callback() { }
155 
156 private:
157  impl m_callbacks TS_GUARDED_BY(m_mutx);
158  mutable ossia::audio_spin_mutex m_mutx;
159 };
160 
161 //using expression_callback_container = callback_container<expression_result_callback>;
162 using expression_callback_iterator = typename expression_callback_container::iterator;
163 class expression_atom;
164 class expression_bool;
165 class expression_composition;
166 class expression_not;
167 class expression_pulse;
168 class expression_generic;
169 using expression_base = ossia::nullable_variant<
170  expression_atom, expression_bool, expression_composition, expression_not,
171  expression_pulse, expression_generic>;
172 
173 // concept expression_base :
174 // public callback_container<expression_result_callback>
175 //{
176 // public:
177 
178 // /*! evaluate the expression
179 // \return bool result of the evaluation */
180 // bool evaluate() const;
181 
182 // /*! pull the value of any #Destination operand */
183 // void update() const;
184 
185 //};
186 }
187 using expression_ptr = std::unique_ptr<expressions::expression_base>;
188 
222 using expression = expressions::expression_base;
223 }
smallfun::function< void(bool), smallfun::DefaultSize *2 > expression_result_callback
Definition: expression_fwd.hpp:25
Definition: git_info.h:7
expressions::expression_base expression
Definition: expression_fwd.hpp:222