Jamba C++ API 8.0.0
Loading...
Searching...
No Matches
Metaprogramming.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2019 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <sstream>
22#include <ostream>
23#include <concepts>
24
25namespace pongasoft::Utils {
26
27template<typename To, typename From>
29 std::is_pointer_v<To> &&
30 requires(From* p)
31{
32 dynamic_cast<To>(p);
33};
34
39template<typename To, typename From>
40requires std::is_pointer_v<To>
41constexpr To safe_dynamic_cast(From* p) noexcept
42{
44 return dynamic_cast<To>(p);
45 else
46 return nullptr;
47}
48
61template<typename T>
62concept HasNotEqual =
63 requires(T const &a, T const &b)
64 {
65 { a != b } -> std::convertible_to<bool>;
66 };
67
83template<typename From, typename To>
85 requires(From const& f)
86 {
87 { static_cast<To>(f) } -> std::same_as<To>;
88 };
89
102template<typename T>
104 requires(std::ostream& os, T const& t)
105 {
106 { os << t } -> std::same_as<std::ostream&>;
107 };
108
112template<typename T>
113std::string typeString()
114{
115 std::stringstream s;
116 s << typeid(T).name();
117 if constexpr (std::is_const_v<typename std::remove_reference<T>::type>)
118 s << " const";
119 if constexpr (std::is_reference_v<T>)
120 s << " &";
121 return s.str();
122}
123
124}
Allows to detect (at compilation time) whether the call static_cast<To>(from) (where from is of type ...
Definition Metaprogramming.h:84
Definition Metaprogramming.h:28
Allows to detect whether a type defines operator!= at compile time.
Definition Metaprogramming.h:62
Allows to detect whether a type defines ostream << x at compile time.
Definition Metaprogramming.h:103
Definition CircularBuffer.h:27
std::string typeString()
typeid(T).name() does not account for const or reference.
Definition Metaprogramming.h:113
constexpr To safe_dynamic_cast(From *p) noexcept
dynamic_cast<U *>(x) does not compile if x is not polymorphic.
Definition Metaprogramming.h:41