FastLED 3.9.15
Loading...
Searching...
No Matches
fl::Variant< Types > Class Template Reference

Detailed Description

template<typename... Types>
class fl::Variant< Types >

Definition at line 9 of file variant.h.

#include <variant.h>

+ Inheritance diagram for fl::Variant< Types >:

Classes

struct  type_to_tag_impl
 
struct  type_to_tag_impl< T >
 
struct  type_to_tag_impl< T, U, Rest... >
 

Public Types

using Tag = uint8_t
 

Public Member Functions

 Variant () noexcept
 
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
 Variant (const T &value)
 
 Variant (const Variant &other)
 
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
 Variant (T &&value)
 
 Variant (Variant &&other) noexcept
 
 ~Variant ()
 
template<typename T, typename... Args>
fl::enable_if< contains_type< T, Types... >::value, T & >::type emplace (Args &&...args)
 
bool empty () const noexcept
 
template<typename T>
bool equals (const T &other) const
 
template<typename T>
bool is () const noexcept
 
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
Variantoperator= (const T &value)
 
Variantoperator= (const Variant &other)
 
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
Variantoperator= (T &&value)
 
Variantoperator= (Variant &&other) noexcept
 
template<typename T>
T * ptr ()
 
template<typename T>
const T * ptr () const
 
void reset () noexcept
 
Tag tag () const noexcept
 
template<typename Visitor>
void visit (Visitor &visitor)
 
template<typename Visitor>
void visit (Visitor &visitor) const
 

Static Public Attributes

static constexpr Tag Empty = 0
 

Private Member Functions

template<typename T, typename... Args>
void construct (Args &&...args)
 
void copy_construct_from (const Variant &other)
 
void destroy_current () noexcept
 
void move_construct_from (Variant &other) noexcept
 

Static Private Member Functions

template<typename T>
static void copy_fn (void *storage, const Variant &other)
 
template<typename T>
static void destroy_fn (void *storage)
 
template<typename T>
static void move_fn (void *storage, Variant &other)
 
template<typename T>
static constexpr Tag type_to_tag ()
 
template<typename T, typename Visitor>
static void visit_fn (void *storage, Visitor &v)
 
template<typename T, typename Visitor>
static void visit_fn_const (const void *storage, Visitor &v)
 

Private Attributes

char _storage [max_size< Types... >::value]
 
Tag _tag
 

Member Typedef Documentation

◆ Tag

template<typename... Types>
using fl::Variant< Types >::Tag = uint8_t

Definition at line 11 of file variant.h.

Constructor & Destructor Documentation

◆ Variant() [1/5]

template<typename... Types>
fl::Variant< Types >::Variant ( )
inlinenoexcept

Definition at line 16 of file variant.h.

16: _tag(Empty) {}
static constexpr Tag Empty
Definition variant.h:12

◆ Variant() [2/5]

template<typename... Types>
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
fl::Variant< Types >::Variant ( const T & value)
inline

Definition at line 20 of file variant.h.

20 : _tag(Empty) {
22 }
void construct(Args &&...args)
Definition variant.h:249

◆ Variant() [3/5]

template<typename... Types>
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
fl::Variant< Types >::Variant ( T && value)
inline

Definition at line 26 of file variant.h.

26 : _tag(Empty) {
28 }

◆ Variant() [4/5]

template<typename... Types>
fl::Variant< Types >::Variant ( const Variant< Types > & other)
inline

Definition at line 30 of file variant.h.

30 : _tag(Empty) {
31 if (!other.empty()) {
33 }
34 }
bool empty() const noexcept
Definition variant.h:100
void copy_construct_from(const Variant &other)
Definition variant.h:196

◆ Variant() [5/5]

template<typename... Types>
fl::Variant< Types >::Variant ( Variant< Types > && other)
inlinenoexcept

Definition at line 36 of file variant.h.

36 : _tag(Empty) {
37 if (!other.empty()) {
39 other.reset();
40 }
41 }
void reset() noexcept
Definition variant.h:92
void move_construct_from(Variant &other) noexcept
Definition variant.h:209

◆ ~Variant()

template<typename... Types>
fl::Variant< Types >::~Variant ( )
inline

Definition at line 43 of file variant.h.

43{ reset(); }

Member Function Documentation

◆ construct()

template<typename... Types>
template<typename T, typename... Args>
void fl::Variant< Types >::construct ( Args &&... args)
inlineprivate

Definition at line 249 of file variant.h.

249 {
250 new (&_storage) T(fl::forward<Args>(args)...);
252 }
char _storage[max_size< Types... >::value]
Definition variant.h:255
static constexpr Tag type_to_tag()
Definition variant.h:228

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::Variant(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::Variant(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::emplace(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=().

+ Here is the caller graph for this function:

◆ copy_construct_from()

template<typename... Types>
void fl::Variant< Types >::copy_construct_from ( const Variant< Types > & other)
inlineprivate

Definition at line 196 of file variant.h.

196 {
197 using Fn = void (*)(void *, const Variant &);
198 static constexpr Fn table[] = {&Variant::template copy_fn<Types>...};
199 table[other._tag - 1](&_storage, other);
200 _tag = other._tag;
201 }
Variant() noexcept
Definition variant.h:16
static void copy_fn(void *storage, const Variant &other)
Definition variant.h:204

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=().

+ Here is the caller graph for this function:

◆ copy_fn()

template<typename... Types>
template<typename T>
static void fl::Variant< Types >::copy_fn ( void * storage,
const Variant< Types > & other )
inlinestaticprivate

Definition at line 204 of file variant.h.

204 {
205 new (storage) T(*reinterpret_cast<const T *>(&other._storage));
206 }

◆ destroy_current()

template<typename... Types>
void fl::Variant< Types >::destroy_current ( )
inlineprivatenoexcept

Definition at line 183 of file variant.h.

183 {
184 using Fn = void (*)(void *);
185 static constexpr Fn table[] = {&Variant::template destroy_fn<Types>...};
186 if (_tag != Empty) {
187 table[_tag - 1](&_storage);
188 }
189 }
static void destroy_fn(void *storage)
Definition variant.h:191

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::reset().

+ Here is the caller graph for this function:

◆ destroy_fn()

template<typename... Types>
template<typename T>
static void fl::Variant< Types >::destroy_fn ( void * storage)
inlinestaticprivate

Definition at line 191 of file variant.h.

191 {
192 reinterpret_cast<T *>(storage)->~T();
193 }

◆ emplace()

template<typename... Types>
template<typename T, typename... Args>
fl::enable_if< contains_type< T, Types... >::value, T & >::type fl::Variant< Types >::emplace ( Args &&... args)
inline

Definition at line 86 of file variant.h.

86 {
87 reset();
89 return ptr<T>();
90 }
T * ptr()
Definition variant.h:106

◆ empty()

template<typename... Types>
bool fl::Variant< Types >::empty ( ) const
inlinenoexcept

Definition at line 100 of file variant.h.

100{ return _tag == Empty; }

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::Variant(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=(), fl::Optional< T >::operator==(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::reset().

+ Here is the caller graph for this function:

◆ equals()

template<typename... Types>
template<typename T>
bool fl::Variant< Types >::equals ( const T & other) const
inline

Definition at line 128 of file variant.h.

128 {
129 if (auto p = ptr<T>()) {
130 return *p == other;
131 }
132 return false;
133 }

◆ is()

template<typename... Types>
template<typename T>
bool fl::Variant< Types >::is ( ) const
inlinenoexcept

Definition at line 102 of file variant.h.

102 {
103 return _tag == type_to_tag<T>();
104 }

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::ptr(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::ptr().

+ Here is the caller graph for this function:

◆ move_construct_from()

template<typename... Types>
void fl::Variant< Types >::move_construct_from ( Variant< Types > & other)
inlineprivatenoexcept

Definition at line 209 of file variant.h.

209 {
210 using Fn = void (*)(void *, Variant &);
211 static constexpr Fn table[] = {&Variant::template move_fn<Types>...};
212 table[other._tag - 1](&_storage, other);
213 _tag = other._tag;
214 other.reset();
215 }
static void move_fn(void *storage, Variant &other)
Definition variant.h:217

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::Variant(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=().

+ Here is the caller graph for this function:

◆ move_fn()

template<typename... Types>
template<typename T>
static void fl::Variant< Types >::move_fn ( void * storage,
Variant< Types > & other )
inlinestaticprivate

Definition at line 217 of file variant.h.

217 {
218 new (storage) T(fl::move(*reinterpret_cast<T *>(&other._storage)));
219 }

◆ operator=() [1/4]

template<typename... Types>
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
Variant & fl::Variant< Types >::operator= ( const T & value)
inline

Definition at line 68 of file variant.h.

68 {
69 reset();
71 return *this;
72 }

◆ operator=() [2/4]

template<typename... Types>
Variant & fl::Variant< Types >::operator= ( const Variant< Types > & other)
inline

Definition at line 45 of file variant.h.

45 {
46 if (this != &other) {
47 reset();
48 if (!other.empty()) {
50 }
51 }
52 return *this;
53 }

◆ operator=() [3/4]

template<typename... Types>
template<typename T, typename = typename fl::enable_if< contains_type<T, Types...>::value>::type>
Variant & fl::Variant< Types >::operator= ( T && value)
inline

Definition at line 76 of file variant.h.

76 {
77 reset();
79 return *this;
80 }

◆ operator=() [4/4]

template<typename... Types>
Variant & fl::Variant< Types >::operator= ( Variant< Types > && other)
inlinenoexcept

Definition at line 55 of file variant.h.

55 {
56 if (this != &other) {
57 reset();
58 if (!other.empty()) {
60 other.reset();
61 }
62 }
63 return *this;
64 }

◆ ptr() [1/2]

template<typename... Types>
template<typename T>
T * fl::Variant< Types >::ptr ( )
inline

Definition at line 106 of file variant.h.

106 {
107 return is<T>() ? reinterpret_cast<T *>(&_storage) : nullptr;
108 }
bool is() const noexcept
Definition variant.h:102

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::emplace().

+ Here is the caller graph for this function:

◆ ptr() [2/2]

template<typename... Types>
template<typename T>
const T * fl::Variant< Types >::ptr ( ) const
inline

Definition at line 110 of file variant.h.

110 {
111 return is<T>() ? reinterpret_cast<const T *>(&_storage) : nullptr;
112 }

◆ reset()

template<typename... Types>
void fl::Variant< Types >::reset ( )
inlinenoexcept

Definition at line 92 of file variant.h.

92 {
93 if (!empty()) {
95 _tag = Empty;
96 }
97 }
void destroy_current() noexcept
Definition variant.h:183

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::~Variant(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::emplace(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=(), fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::operator=().

+ Here is the caller graph for this function:

◆ tag()

template<typename... Types>
Tag fl::Variant< Types >::tag ( ) const
inlinenoexcept

Definition at line 99 of file variant.h.

99{ return _tag; }

◆ type_to_tag()

template<typename... Types>
template<typename T>
static constexpr Tag fl::Variant< Types >::type_to_tag ( )
inlinestaticconstexprprivate

Definition at line 228 of file variant.h.

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::construct(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::is().

+ Here is the caller graph for this function:

◆ visit() [1/2]

template<typename... Types>
template<typename Visitor>
void fl::Variant< Types >::visit ( Visitor & visitor)
inline

Definition at line 136 of file variant.h.

136 {
137 if (_tag == Empty)
138 return;
139
140 // Fn is "a pointer to function taking (void* storage, Visitor&)"
141 using Fn = void (*)(void *, Visitor &);
142
143 // Build a constexpr array of one thunk per type in Types...
144 // Each thunk casts the storage back to the right T* and calls
145 // visitor.accept
146 static constexpr Fn table[] = {
148
149 // _tag is 1-based, so dispatch in O(1) via one indirect call:
150 table[_tag - 1](&_storage, visitor);
151 }
static void visit_fn(void *storage, Visitor &v)
Definition variant.h:171

Referenced by fl::Gradient::Gradient().

+ Here is the caller graph for this function:

◆ visit() [2/2]

template<typename... Types>
template<typename Visitor>
void fl::Variant< Types >::visit ( Visitor & visitor) const
inline

Definition at line 153 of file variant.h.

153 {
154 if (_tag == Empty)
155 return;
156
157 // Fn is "a pointer to function taking (const void* storage, Visitor&)"
158 using Fn = void (*)(const void *, Visitor &);
159
160 // Build a constexpr array of one thunk per type in Types...
161 static constexpr Fn table[] = {
163
164 // _tag is 1-based, so dispatch in O(1) via one indirect call:
165 table[_tag - 1](&_storage, visitor);
166 }
static void visit_fn_const(const void *storage, Visitor &v)
Definition variant.h:177

◆ visit_fn()

template<typename... Types>
template<typename T, typename Visitor>
static void fl::Variant< Types >::visit_fn ( void * storage,
Visitor & v )
inlinestaticprivate

Definition at line 171 of file variant.h.

171 {
172 // unsafe_cast is OK here because we know _tag matched T
173 v.accept(*reinterpret_cast<T *>(storage));
174 }

◆ visit_fn_const()

template<typename... Types>
template<typename T, typename Visitor>
static void fl::Variant< Types >::visit_fn_const ( const void * storage,
Visitor & v )
inlinestaticprivate

Definition at line 177 of file variant.h.

177 {
178 // unsafe_cast is OK here because we know _tag matched T
179 v.accept(*reinterpret_cast<const T *>(storage));
180 }

Member Data Documentation

◆ _storage

template<typename... Types>
char fl::Variant< Types >::_storage[max_size< Types... >::value]
private

Definition at line 255 of file variant.h.

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::copy_fn(), and fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::move_fn().

◆ _tag

template<typename... Types>
Tag fl::Variant< Types >::_tag
private

Definition at line 257 of file variant.h.

Referenced by fl::Variant< const CRGBPalette16 *, const CRGBPalette32 *, const CRGBPalette256 *, GradientFunction >::copy_construct_from().

◆ Empty

template<typename... Types>
Tag fl::Variant< Types >::Empty = 0
staticconstexpr

Definition at line 12 of file variant.h.


The documentation for this class was generated from the following file: