24 #include "lv_common.h"
25 #include "lv_util.hpp"
39 VisDestroyFunc destroy_func;
41 Closure (
void* func,
void* data, VisDestroyFunc destroy_func);
46 Closure (Closure
const&) =
delete;
47 Closure& operator= (Closure
const&) =
delete;
54 typedef std::list<std::unique_ptr<Closure> > HandlerList;
58 std::string description;
60 VisParamValue* default_value;
61 HandlerList changed_handlers;
63 std::unique_ptr<Closure> validator;
65 Param (std::string
const& name,
66 std::string
const& description,
74 Param (Param
const&) =
delete;
75 Param& operator= (Param
const&) =
delete;
77 Closure* add_callback (VisParamChangedFunc func,
79 VisDestroyFunc destroy_func);
81 bool remove_callback (Closure* to_remove);
85 void notify_callbacks ();
92 typedef std::map<std::string, std::unique_ptr<Param>> Entries;
95 EventQueue* event_queue;
104 int validate_param_value (VisParamValue *value, Closure *validator)
106 return (* (VisParamValidateFunc) validator->func) (value, validator->data);
113 Closure::Closure (
void *func_,
void *data_, VisDestroyFunc destroy_func_)
114 : func {
reinterpret_cast<void(*)()
> (func_)}
116 , destroy_func {destroy_func_}
123 if (data && destroy_func) {
130 ParamList::Impl::Impl ()
131 : event_queue {
nullptr}
136 ParamList::Impl::~Impl ()
155 for (
auto param : params) {
161 : m_impl {std::move (list.m_impl)}
168 m_impl->event_queue = &event_queue;
173 return m_impl->event_queue;
183 visual_return_if_fail (param !=
nullptr);
185 param->parent =
this;
188 m_impl->entries[param->name] = std::unique_ptr<Param> {param};
193 auto entry = m_impl->entries.find (name);
194 if (entry != m_impl->entries.end ()) {
195 m_impl->entries.erase (entry);
204 auto entry = m_impl->entries.find (name);
205 if (entry != m_impl->entries.end ()) {
206 return entry->second.get ();
214 Param::Param (std::string
const& name_,
215 std::string
const& description_,
217 void* default_value_,
221 , description (description_)
222 , validator (validator_)
225 VisParamValue test_value;
226 visual_param_value_init (&test_value, type_, default_value_);
228 if (!validate_param_value (&test_value, validator.get ())) {
229 throw std::runtime_error {
"Default value failed to validate"};
233 value = visual_param_value_new (type_, default_value_);
234 default_value = visual_param_value_new (type_, default_value_);
239 visual_param_value_free (value);
240 visual_param_value_free (default_value);
243 Closure* Param::add_callback (VisParamChangedFunc func,
245 VisDestroyFunc destroy_func)
247 visual_return_val_if_fail (func !=
nullptr,
nullptr);
249 auto closure =
new Closure {
reinterpret_cast<void*
> (func), priv, destroy_func};
250 changed_handlers.emplace_back (closure);
255 bool Param::remove_callback (Closure* to_remove)
257 visual_return_val_if_fail (to_remove !=
nullptr,
false);
259 auto entry = std::find_if (changed_handlers.begin (),
260 changed_handlers.end (),
261 [&] (std::unique_ptr<Closure>
const& handler) {
262 return handler.get () == to_remove;
265 if (entry != changed_handlers.end ()) {
266 changed_handlers.erase (entry);
273 void Param::changed ()
278 auto event_queue = parent->get_event_queue ();
286 void Param::notify_callbacks ()
288 for (
auto const& handler : changed_handlers) {
289 (*(VisParamChangedFunc) (handler->func)) (
this, handler->data);
300 VisClosure *visual_closure_new (
void *func,
void *data, VisDestroyFunc destroy_func)
302 return new LV::Closure {func, data, destroy_func};
305 void visual_closure_free (VisClosure *
self)
324 visual_return_if_fail (
self !=
nullptr);
325 visual_return_if_fail (event_queue !=
nullptr);
327 self->set_event_queue (*event_queue);
332 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
334 return self->get_event_queue ();
337 void visual_param_list_add (
VisParamList *
self, VisParam *param)
339 visual_return_if_fail (
self !=
nullptr);
340 visual_return_if_fail (param !=
nullptr);
346 void visual_param_list_add_array (
VisParamList *
self, VisParam **params,
unsigned int nparams)
348 visual_return_if_fail (
self !=
nullptr);
350 for (
unsigned int i = 0; i < nparams; i++) {
351 self->add (params[i]);
355 void visual_param_list_add_many (
VisParamList *
self, ...)
357 visual_return_if_fail (
self !=
nullptr);
361 va_start (args,
self);
363 auto param = va_arg (args, VisParam *);
367 param = va_arg (args, VisParam *);
373 VisParam **visual_param_list_get_entries (
VisParamList *
self)
375 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
382 int visual_param_list_remove (
VisParamList *
self,
const char *name)
384 visual_return_val_if_fail (
self !=
nullptr, FALSE);
385 visual_return_val_if_fail (name !=
nullptr, FALSE);
387 return self->remove (name);
390 VisParam *visual_param_list_get (
VisParamList *
self,
const char *name)
392 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
393 visual_return_val_if_fail (name !=
nullptr,
nullptr);
395 return self->get (name);
401 const char * description,
403 void * default_value,
404 VisClosure * validator)
407 return new LV::Param (name, description, type, default_value, validator);
409 catch (std::exception& error) {
421 visual_return_val_if_fail (
self !=
nullptr, FALSE);
423 return self->name == name;
426 VisParamType visual_param_get_type (VisParam *
self)
428 visual_return_val_if_fail (
self !=
nullptr, VISUAL_PARAM_TYPE_NONE);
430 return self->value->type;
433 const char *visual_param_get_name (VisParam *
self)
435 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
440 const char *visual_param_get_description (VisParam *
self)
442 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
447 void visual_param_set_value_bool (VisParam *
self,
int value)
449 visual_return_if_fail (
self !=
nullptr);
451 visual_param_value_set_bool (self->value, value);
454 void visual_param_set_value_integer (VisParam *
self,
int value)
456 visual_return_if_fail (
self !=
nullptr);
458 visual_param_value_set_integer (self->value, value);
461 void visual_param_set_value_float (VisParam *
self,
float value)
463 visual_return_if_fail (
self !=
nullptr);
465 visual_param_value_set_float (self->value, value);
468 void visual_param_set_value_double (VisParam *
self,
double value)
470 visual_return_if_fail (
self !=
nullptr);
472 visual_param_value_set_double (self->value, value);
475 void visual_param_set_value_string (VisParam *
self,
const char *
string)
477 visual_return_if_fail (
self !=
nullptr);
479 visual_param_value_set_string (self->value,
string);
482 void visual_param_set_value_color (VisParam *
self,
VisColor *color)
484 visual_return_if_fail (
self !=
nullptr);
486 visual_param_value_set_color (self->value, color);
489 void visual_param_set_value_palette (VisParam *
self,
VisPalette *palette)
491 visual_return_if_fail (
self !=
nullptr);
493 visual_param_value_set_palette (self->value, palette);
496 int visual_param_get_value_bool (VisParam *
self)
498 visual_return_val_if_fail (
self !=
nullptr, 0);
500 return visual_param_value_get_bool (self->value);
503 int visual_param_get_value_integer (VisParam *
self)
505 visual_return_val_if_fail (
self !=
nullptr, 0);
507 return visual_param_value_get_integer (self->value);
510 float visual_param_get_value_float (VisParam *
self)
512 visual_return_val_if_fail (
self !=
nullptr, 0.0f);
514 return visual_param_value_get_float (self->value);
517 double visual_param_get_value_double (VisParam *
self)
519 visual_return_val_if_fail (
self !=
nullptr, 0.0);
521 return visual_param_value_get_double (self->value);
524 const char *visual_param_get_value_string (VisParam *
self)
526 visual_return_val_if_fail (
self !=
nullptr, 0);
528 return visual_param_value_get_string (self->value);
531 VisColor *visual_param_get_value_color (VisParam *
self)
533 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
535 return visual_param_value_get_color (self->value);
538 VisPalette *visual_param_get_value_palette (VisParam *
self)
540 visual_return_val_if_fail (
self !=
nullptr,
nullptr);
542 return visual_param_value_get_palette (self->value);