open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
any.h
1// =====================================================================================================================
2// open-cpp-utils, an open-source cpp library with data structures that extend the STL.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef OPEN_CPP_UTILS_ANY_H
20#define OPEN_CPP_UTILS_ANY_H
21
22#include "template_utils.h"
23
24namespace open_cpp_utils
25{
26
31template<typename...Ts> class any;
32
33template<> class any<> { };
34
35template<typename T, typename...Rest>
36class any<T, Rest...> : public any<Rest...>
37{
38// Assertions ==========================================================================================================
39
40 static_assert(is_unique<T, Rest...>);
41
42
43// Typedefs ============================================================================================================
44
45public:
46 using base_type = any<Rest...>;
47
48 using this_type = T;
49 using reference = T&;
50 using const_reference = const T&;
51 using pointer = T*;
52 using const_pointer = const T*;
53
54
55// Constructors ========================================================================================================
56
57public:
58 any() : base_type() , Value() { }
59 any(const this_type& value, const Rest&...other) : base_type(other...), Value(value) { }
60 any(this_type&& value, Rest&&...other) : base_type(other...), Value(value) { }
61 any(const any& other) = default;
62 any(any&& other) = default;
63 ~any() = default;
64
65
66// Operators ===========================================================================================================
67
68public:
69
70// Assignment operators ------------------------------------------------------------------------------------------------
71
72 any& operator=(const any&) = default;
73 any& operator=(any&&) = default;
74
75
76// Access --------------------------------------------------------------------------------------------------------------
77
78 template<typename V>
79 V& get()
80 {
81 static_assert(std::disjunction<std::is_same<V, T>, std::is_same<V, Rest>...>{});
82 return static_cast<V&>(*this);
83 }
84
85 template<typename V>
86 const V& get() const
87 {
88 static_assert(std::disjunction<std::is_same<V, T>, std::is_same<V, Rest>...>{});
89 return static_cast<const V&>(*this);
90 }
91
92// Cast operators ------------------------------------------------------------------------------------------------------
93
94 operator reference() { return Value; }
95 operator const_reference() const { return Value; }
96 operator pointer() { return &Value; }
97 operator const_pointer() const { return &Value; }
98
99
100// Variables ===========================================================================================================
101
102private:
103 static constexpr size_t Size = sizeof...(Rest);
104 this_type Value;
105};
106
107}
108
109
110#endif // OPEN_CPP_UTILS_ANY_H
Wrapper for a value with multiple types.
Definition any.h:31
Provides compile time evaluation utilities for templates and template packs.