Jamba C++ API 7.5.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 <type_traits>
22#include <sstream>
23#include <ostream>
24#include "Cpp17.h"
25
26namespace pongasoft::Utils {
27
33template<typename U>
34class Cast
35{
36public:
37 template<typename T>
38 static inline U dynamic(T *iPtr)
39 {
40 if constexpr (std::is_polymorphic<T>::value)
41 {
42 return dynamic_cast<U>(iPtr);
43 }
44 else
45 {
46 return nullptr;
47 }
48 }
49};
50
54template<typename T>
55using operator_not_eq_t = decltype(std::declval<T const&>() != std::declval<T const&>());
56
69template<typename T>
71
75template<typename From, typename To>
76using static_cast_t = decltype(static_cast<To>(std::declval<From>()));
77
93template<typename From, typename To>
95
99template<typename T>
100using operator_write_to_ostream_t = decltype(std::declval<std::ostream &>() << std::declval<T const&>());
101
114template<typename T>
116
120template<typename T>
121std::string typeString()
122{
123 std::stringstream s;
124 s << typeid(T).name();
125 if(std::is_const_v<typename std::remove_reference<T>::type>)
126 s << " const";
127 if(std::is_reference_v<T>)
128 s << " &";
129 return s.str();
130}
131
132}
dynamic_cast<U *>(x) does not compile if x is not polymorphic.
Definition Metaprogramming.h:35
static U dynamic(T *iPtr)
Definition Metaprogramming.h:38
constexpr bool is_detected_v
Definition Cpp17.h:63
Definition CircularBuffer.h:26
decltype(std::declval< std::ostream & >()<< std::declval< T const & >()) operator_write_to_ostream_t
Defines the type for operator<<.
Definition Metaprogramming.h:100
constexpr auto is_operator_not_eq_defined
Allows to detect whether a type defines operator!= at compile time.
Definition Metaprogramming.h:70
std::string typeString()
typeid(T).name() does not account for const or reference.
Definition Metaprogramming.h:121
decltype(static_cast< To >(std::declval< From >())) static_cast_t
Defines the type for static_cast<To>(From).
Definition Metaprogramming.h:76
constexpr auto is_static_cast_defined
Allows to detect (at compilation time) whether the call static_cast<To>(from) (where from is of type ...
Definition Metaprogramming.h:94
decltype(std::declval< T const & >() !=std::declval< T const & >()) operator_not_eq_t
Defines the type for operator!=.
Definition Metaprogramming.h:55
constexpr auto is_operator_write_to_ostream_defined
Allows to detect whether a type defines ostream << x at compile time.
Definition Metaprogramming.h:115