neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
i_any.hpp
Go to the documentation of this file.
1// i_any.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <typeinfo>
39#include <any>
40
41namespace neolib
42{
43 class i_any
44 {
45 public:
47 private:
48 template<class T>
49 friend T any_cast(const i_any& operand);
50 template<class T>
51 friend T any_cast(i_any& operand);
52 template<class T>
53 friend T any_cast(i_any&& operand);
54 template<class T>
55 friend const T* any_cast(const i_any* operand) noexcept;
56 template<class T>
57 friend T* any_cast(i_any* operand) noexcept;
58 template<class T>
59 friend T unsafe_any_cast(const i_any& operand) noexcept;
60 template<class T>
61 friend T unsafe_any_cast(i_any& operand) noexcept;
62 public:
63 virtual ~i_any() = default;
64 public:
65 virtual void reset() = 0;
66 public:
67 virtual bool has_value() const = 0;
68 virtual std::type_info const& type() const = 0;
69 public:
70 virtual bool operator==(const i_any& aOther) const = 0;
71 virtual bool operator!=(const i_any& aOther) const = 0;
72 virtual bool operator<(const i_any& aOther) const = 0;
73 private:
74 virtual const std::any& as_std_any() const = 0;
75 virtual std::any& as_std_any() = 0;
76 virtual const void* unsafe_ptr() const = 0;
77 virtual void* unsafe_ptr() = 0;
78 };
79
80 template<class T>
81 inline T any_cast(const i_any& operand)
82 {
83 return std::any_cast<T>(operand.as_std_any());
84 }
85 template<class T>
86 inline T any_cast(i_any& operand)
87 {
88 return std::any_cast<T>(operand.as_std_any());
89 }
90 template<class T>
91 inline T any_cast(i_any&& operand)
92 {
93 return std::any_cast<T>(std::move(operand.as_std_any()));
94 }
95 template<class T>
96 inline const T* any_cast(const i_any* operand) noexcept
97 {
98 return std::any_cast<const T*>(&operand->as_std_any());
99 }
100 template<class T>
101 inline T* any_cast(i_any* operand) noexcept
102 {
103 return std::any_cast<T*>(&operand->as_std_any());
104 }
105 template<class T>
106 inline T unsafe_any_cast(const i_any& operand) noexcept
107 {
108 return *static_cast<const std::decay_t<T>*>(operand.unsafe_ptr());
109 }
110 template<class T>
111 inline T unsafe_any_cast(i_any& operand) noexcept
112 {
113 return *static_cast<std::decay_t<T>*>(operand.unsafe_ptr());
114 }
115}
virtual std::type_info const & type() const =0
virtual void reset()=0
virtual bool operator==(const i_any &aOther) const =0
friend T any_cast(const i_any &operand)
Definition i_any.hpp:81
virtual bool has_value() const =0
virtual bool operator!=(const i_any &aOther) const =0
friend T unsafe_any_cast(const i_any &operand) noexcept
Definition i_any.hpp:106
virtual ~i_any()=default
virtual bool operator<(const i_any &aOther) const =0
i_any abstract_type
Definition i_any.hpp:46
T unsafe_any_cast(const any &operand) noexcept
Definition any.hpp:292
T any_cast(const any &operand)
Definition any.hpp:267