libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
slot.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <algorithm>
11#include <functional>
14
16
28template <typename... Args>
29class Slot {
30 public:
32 using Function = std::function<void(Args...)>;
33
41 Slot(const Function &func, void *owner = nullptr, const char *prototype = nullptr, int id = -1) :
42 _owner(owner), _prototype(prototype), _id(id), _function(func) {}
43
45 void *owner() const { return _owner; }
46
48 const char *prototype() const { return _prototype; }
49
51 int id() const { return _id; }
52
54 void setID(int val) { _id = val; }
55
60 template <typename T> struct removeConstAndRef {
61 using type = std::remove_const_t<std::remove_reference_t<T>>;
62 };
63
65 template <typename T> using RemoveConstAndRef = typename removeConstAndRef<T>::type;
66
78 return { Variant(RemoveConstAndRef<Args>(args))... };
79 }
80
85 void exec(Args... args) {
86 _function(args...);
87 return;
88 }
89
99 execFromVariantList(std::make_index_sequence<sizeof...(Args)>(), variantList);
100 }
101
102 private:
103 void *_owner = nullptr;
104 const char *_prototype = nullptr;
105 int _id = -1;
106 Function _function;
107
108 template <std::size_t... Idx> void execFromVariantList(std::index_sequence<Idx...>, const VariantList &variantList) {
110 }
111
112 template <std::size_t Idx> decltype(auto) unpackVariant(const Variant &variant) {
113 using T = RemoveConstAndRef<std::tuple_element_t<Idx, std::tuple<Args...>>>;
114 return variant.get<T>();
115 }
116};
117
119
Dynamic array container wrapping std::vector.
Definition list.h:40
Type-safe callback slot that wraps a callable with optional ownership tracking.
Definition slot.h:29
void setID(int val)
Sets the numeric identifier for this slot.
Definition slot.h:54
static VariantList pack(Args... args)
Packs the given arguments into a VariantList.
Definition slot.h:77
const char * prototype() const
Returns the prototype string associated with this slot.
Definition slot.h:48
int id() const
Returns the numeric identifier for this slot.
Definition slot.h:51
std::function< void(Args...)> Function
Callable type that the slot wraps.
Definition slot.h:32
void * owner() const
Returns the owner pointer associated with this slot.
Definition slot.h:45
typename removeConstAndRef< T >::type RemoveConstAndRef
Convenience alias for removeConstAndRef.
Definition slot.h:65
void exec(const VariantList &variantList)
Executes the slot's callable by unpacking arguments from a VariantList.
Definition slot.h:98
Slot(const Function &func, void *owner=nullptr, const char *prototype=nullptr, int id=-1)
Constructs a Slot with the given callable and optional metadata.
Definition slot.h:41
void exec(Args... args)
Executes the slot's callable with the given typed arguments.
Definition slot.h:85
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
Helper trait that strips const qualification and references from a type.
Definition slot.h:60
VariantImpl< std::monostate, bool, uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t, float, double, String, DateTime, TimeStamp, Size2Du32, UUID, Timecode, Rational< int >, detail::VariantEnd > Variant
Concrete variant type instantiated with every type from PROMEKI_VARIANT_TYPES.
Definition variant.h:362