open-cpp-utils 0.0.1
Loading...
Searching...
No Matches
any.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16#ifndef ANY_H
17#define ANY_H
18
19#include "template_utils.h"
20
21namespace open_cpp_utils
22{
23
28template<typename...Ts> class any;
29
30template<typename T, typename...Rest>
31class any<T, Rest...> : public any<Rest...>
32{
33// Assertions ==========================================================================================================
34
35 static_assert(is_unique<Rest...>);
36
37
38// Typedefs ============================================================================================================
39
40public:
41 using base_type = any<Rest...>;
42 using this_type = T;
43
44
45// Constructors ========================================================================================================
46
47public:
48 any() : base_type() , Value() { }
49 any(const this_type& value, const Rest&...other) : base_type(other...), Value(value) { }
50 any(this_type&& value, Rest&&...other) : base_type(other...), Value(value) { }
51 any(const any& other) = default;
52 any(any&& other) = default;
53 ~any() = default;
54
55
56// Operators ===========================================================================================================
57
58public:
59
60// Assignment operators ------------------------------------------------------------------------------------------------
61 any& operator=(const any&) = default;
62 any& operator=(any&&) = default;
63
64
65// Cast operators ------------------------------------------------------------------------------------------------------
66
67 operator this_type () const { return Value; }
68 operator this_type& () { return Value; }
69 operator const this_type& () const { return Value; }
70 operator this_type&&() { return Value; }
71 operator this_type* () { return &Value; }
72 operator const this_type* () const { return &Value; }
73
74
75// Variables ===========================================================================================================
76
77private:
78 static constexpr size_t Size = sizeof...(Rest);
79 this_type Value;
80};
81
82template<>
83class any<> { };
84
85}
86
87
88#endif //ANY_H
Wrapper for a value with multiple types.
Definition any.h:28
Provides compile time evaluation utilities for templates and template packs.