OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
ossia_utils.hpp
1#pragma once
2#include <ossia/detail/config.hpp>
3
4#include <ossia/detail/flat_map.hpp>
5#include <ossia/detail/fmt.hpp>
6#include <ossia/network/base/node.hpp>
7#include <ossia/network/base/parameter.hpp>
8#include <ossia/network/base/protocol.hpp>
10#include <ossia/network/value/value.hpp>
11
12#include <ossia-c/log/ossia_log.h>
13#include <ossia-c/ossia-c.h>
14
15#include <algorithm>
16#include <cstdio>
17#include <cstring>
18#include <mutex>
19
20struct ossia_protocol
21{
22 ossia_protocol(ossia::net::protocol_base* p)
23 : protocol{p}
24 {
25 }
26 ossia::net::protocol_base* protocol{};
27};
28
29struct ossia_device
30{
31 std::shared_ptr<ossia::net::device_base> device;
32};
33
34struct ossia_domain
35{
36 ossia::domain domain;
37};
38
39struct ossia_value_callback_index
40{
41 ossia::net::parameter_base::iterator it;
42};
43
44struct ossia_value
45{
46 ossia::value value;
47};
48
49inline auto convert(ossia_type t)
50{
51 return static_cast<ossia::val_type>(t);
52}
53
54inline auto convert(ossia_access_mode t)
55{
56 return static_cast<ossia::access_mode>(t);
57}
58
59inline auto convert(ossia_bounding_mode t)
60{
61 return static_cast<ossia::bounding_mode>(t);
62}
63
64inline auto convert(ossia::val_type t)
65{
66 return static_cast<ossia_type>(t);
67}
68
69inline auto convert(ossia::access_mode t)
70{
71 return static_cast<ossia_access_mode>(t);
72}
73
74inline auto convert(ossia::bounding_mode t)
75{
76 return static_cast<ossia_bounding_mode>(t);
77}
78
79inline ossia::net::parameter_base* convert_parameter(ossia_parameter_t v)
80{
81 return static_cast<ossia::net::parameter_base*>(v);
82}
83
84inline void* convert(ossia::net::parameter_base* v)
85{
86 return static_cast<void*>(v);
87}
88inline void* convert(const ossia::net::parameter_base* v)
89{
90 return static_cast<void*>(const_cast<ossia::net::parameter_base*>(v));
91}
92
93inline ossia::net::node_base* convert_node(ossia_node_t v)
94{
95 return static_cast<ossia::net::node_base*>(v);
96}
97
98inline ossia::net::device_base* convert_device(ossia_device_t v)
99{
100 return v->device.get();
101}
102
103inline void* convert(const ossia::net::node_base* v)
104{
105 return static_cast<void*>(const_cast<ossia::net::node_base*>(v));
106}
107inline void* convert(ossia::net::node_base* v)
108{
109 return static_cast<void*>(v);
110}
111
112inline auto convert(ossia::value&& v)
113{
114 return new ossia_value{std::move(v)};
115}
116
117inline auto convert(const ossia::value& v)
118{
119 return new ossia_value{v};
120}
121
122inline auto convert(const ossia::domain& v)
123{
124 return new ossia_domain{v};
125}
126inline auto convert(ossia::domain&& v)
127{
128 return new ossia_domain{std::move(v)};
129}
130
131template <typename Fun>
132auto safe_function(const char name[], Fun f) -> decltype(f())
133try
134{
135 // DEBUG_LOG_FMT("Entering %s", name);
136 return f();
137}
138catch(const std::exception& e)
139{
140 auto str = fmt::format("{}: {}", name, e.what());
141 ossia_log_error(str.c_str());
142 return decltype(f())();
143}
144catch(...)
145{
146 auto str = fmt::format("{}: Exception caught", name);
147 ossia_log_error(str.c_str());
148 return decltype(f())();
149}
150
151inline char* copy_string(const std::string& str)
152{
153 const auto n = str.size();
154 auto mbuffer = new char[n + 1];
155 std::memcpy(mbuffer, str.data(), n);
156 mbuffer[n] = 0;
157 return mbuffer;
158}
159
160inline void copy_bytes(const std::string& str, char** ptr, size_t* sz)
161{
162 const auto n = str.size();
163 *sz = n;
164 *ptr = (char*)std::malloc(sizeof(char) * (n + 1));
165 std::memcpy(*ptr, str.data(), n);
166 (*ptr)[n] = 0;
167}
168
169struct node_cb
170{
171 ossia_node_callback_t m_cb{};
172 void* m_ctx{};
173 void operator()(const ossia::net::node_base& node) { m_cb(m_ctx, convert(&node)); }
174};
175struct address_cb
176{
177 ossia_parameter_callback_t m_cb{};
178 void* m_ctx{};
179 void operator()(const ossia::net::parameter_base& addr)
180 {
181 m_cb(m_ctx, convert(&addr));
182 }
183};
184
185struct global_devices
186{
187 ossia::flat_map<std::string, ossia_device_t> devices;
188
189 global_devices() = default;
190 global_devices(const global_devices&) = delete;
191 global_devices(global_devices&&) = delete;
192 global_devices& operator=(const global_devices&) = delete;
193 global_devices& operator=(global_devices&&) = delete;
194};
195
196global_devices& static_devices();
Root of a device tree.
Definition ossia/network/base/device.hpp:58
The node_base class.
Definition node.hpp:48
The parameter_base class.
Definition ossia/network/base/parameter.hpp:48
The protocol_base class.
Definition protocol.hpp:40
The value class.
Definition value.hpp:173
ossia_bounding_mode
Definition ossia-c.h:403
ossia_type
Definition ossia-c.h:376
ossia_access_mode
Definition ossia-c.h:393
val_type
Enum to represent the types that a value can take.
Definition parameter_properties.hpp:16
bounding_mode
Address behaviors at crossing domain boundaries.
Definition parameter_properties.hpp:56
access_mode
Address behaviors at crossing domain boundaries time.
Definition parameter_properties.hpp:46
domain A domain of values
Definition domain_base.hpp:23