Veethree D7 SDK
Loading...
Searching...
No Matches
io_event_source.h
Go to the documentation of this file.
1
5#ifndef IO_EVENT_SOURCE_H
6#define IO_EVENT_SOURCE_H
7
8#include <cstdint>
9#include <string>
10#include <type_traits>
11
12namespace vsdk::io {
13
18
26public:
30 enum class source {
31 analog = 0x2f5f3,
32 digital_out = 0x30f15,
33 digital_in = 0x4e2f1,
34 pwm_out = 0x50bff,
35 pwm_in = 0x5f4f3,
36 i420 = 0x5f9ff,
37 };
38
49 template<typename T>
50 event_source(source src, T value, std::string label = "")
51 : _hash{ hash(src, value) }
52 , _label{ std::move(label) }
53 {}
54
61 : _hash{}
62 {}
63
68 uint32_t hash() const { return _hash; }
73 std::string label() const { return _label; }
77 explicit operator uint32_t() const { return _hash; }
78
84 bool operator==(const event_source& other) const { return _hash == other._hash; }
90 bool operator!=(const event_source& other) const { return _hash != other._hash; }
96 bool operator<(const event_source& other) const { return _hash < other._hash; }
97
98private:
105 template<typename T>
106 static uint32_t hash(source src, T value)
107 {
108 if constexpr (!(std::is_integral_v<T> || std::is_enum_v<T>) ) {
109 static_assert(false, "type T is not convertible to uint32_t");
110 }
111 // +1 to avoid multiplication by zero
112 uint32_t result{ static_cast<uint32_t>(value) + 1 };
113 result = (result ^ (result >> 8U)) * static_cast<uint32_t>(src);
114 result = (result ^ (result >> 8U)) * static_cast<uint32_t>(src);
115 return result ^ (result >> 8U);
116 }
117
118 uint32_t _hash;
119 std::string _label;
120};
121
123
124} // namespace vsdk::io
125
126#endif // IO_EVENT_SOURCE_H
event_source()
The class default constructor.
Definition io_event_source.h:60
std::string label() const
Return the (optinal) label assigned to this event source at time of object construction.
Definition io_event_source.h:73
bool operator<(const event_source &other) const
operator < to be used when sorting event_source objects
Definition io_event_source.h:96
bool operator!=(const event_source &other) const
Inequality operator, verifies if two event_source objects have different values.
Definition io_event_source.h:90
uint32_t hash() const
Return the cached hash value calculated at construction time.
Definition io_event_source.h:68
bool operator==(const event_source &other) const
Equality operator, verifies if two event_source objects have the same value.
Definition io_event_source.h:84
event_source(source src, T value, std::string label="")
The class constructor.
Definition io_event_source.h:50
source
The source enum contains unique constants to be used in the hash calculation.
Definition io_event_source.h:30
Any entities that belong to the IO library.