Audacity 3.2.0
TypedAny.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file TypedAny.h
6 @brief Wrapper class for std::any, making type distinctions
7
8 Paul Licameli
9
10**********************************************************************/
11#ifndef __AUDACITY_TYPED_ANY__
12#define __AUDACITY_TYPED_ANY__
13
14#include <any>
15#include <type_traits>
16#include <utility>
17
18namespace audacity {
19
21
24template<typename Tag> class TypedAny
25{
26 // Define a trait of argument packs
27 // Primary template:
28 template<typename... Args> struct Good_args : std::true_type{};
29 // Partial specialization:
30 template<typename Arg> struct Good_args<Arg>
31 : std::bool_constant< !std::is_base_of_v<TypedAny,
32 std::remove_const_t< std::remove_reference_t<Arg> >
33 > >{};
34public:
36 template<typename... Args,
37 typename restriction = std::enable_if_t< Good_args<Args...>::value >
38 >
39 explicit TypedAny(Args &&... args)
40 : mAny(std::forward<Args>(args)...)
41 {}
42
43 TypedAny(const TypedAny&) = default;
44 TypedAny &operator=(const TypedAny&) = default;
45 TypedAny(TypedAny&&) = default;
47
56 template<typename ValueType, typename... Args>
57 std::decay_t<ValueType> &emplace(Args &&... args)
58 {
59 return mAny.emplace<ValueType>(std::forward<Args>(args)...);
60 }
61 void reset() noexcept { mAny.reset(); }
62 void swap(TypedAny &other) noexcept { mAny.swap(other.mAny); }
63 bool has_value() const noexcept { return mAny.has_value(); }
64 const std::type_info& type() const noexcept { return mAny.type(); }
65
67 template<typename T>
68 const T* cast() const noexcept { return std::any_cast<T>(&mAny); }
69
71 template<typename T>
72 T* cast() noexcept { return std::any_cast<T>(&mAny); }
73
75 template<typename T, typename... Args>
76 static TypedAny make(Args &&... args)
77 {
78 return TypedAny(std::in_place_type<T>, std::forward<Args>(args)...);
79 }
80
82
83private:
84 std::any mAny;
85};
86
88template<typename Tag>
89inline void swap(TypedAny<Tag> &x, TypedAny<Tag> &y) { x.swap(y); }
90
91}
92
93#endif
Generates distinct, non-interconvertible types wrapping std::any.
Definition: TypedAny.h:25
TypedAny(TypedAny &&)=default
void reset() noexcept
Definition: TypedAny.h:61
TypedAny & operator=(TypedAny &&)=default
T * cast() noexcept
Like pointer-valued any_cast but a non-static member function.
Definition: TypedAny.h:72
TypedAny(Args &&... args)
Constructor with arguments just as for std::any, but it is explicit.
Definition: TypedAny.h:39
std::any mAny
Definition: TypedAny.h:84
std::decay_t< ValueType > & emplace(Args &&... args)
Definition: TypedAny.h:57
bool has_value() const noexcept
Definition: TypedAny.h:63
void swap(TypedAny &other) noexcept
Definition: TypedAny.h:62
const T * cast() const noexcept
Like pointer-valued any_cast but a non-static member function.
Definition: TypedAny.h:68
const std::type_info & type() const noexcept
Definition: TypedAny.h:64
TypedAny(const TypedAny &)=default
TypedAny & operator=(const TypedAny &)=default
static TypedAny make(Args &&... args)
Like make_any but a static member function.
Definition: TypedAny.h:76
void swap(TypedAny< Tag > &x, TypedAny< Tag > &y)
Non-member swap.
Definition: TypedAny.h:89
STL namespace.