Jamba C++ API  5.1.1
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 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <type_traits>
21 #include <sstream>
22 #include <ostream>
23 #include "Cpp17.h"
24 
25 namespace pongasoft::Utils {
26 
32 template<typename U>
33 class Cast
34 {
35 public:
36  template<typename T>
37  static inline U dynamic(T *iPtr)
38  {
39  if constexpr (std::is_polymorphic<T>::value)
40  {
41  return dynamic_cast<U>(iPtr);
42  }
43  else
44  {
45  return nullptr;
46  }
47  }
48 };
49 
53 template<typename T>
54 using operator_not_eq_t = decltype(std::declval<T const&>() != std::declval<T const&>());
55 
68 template<typename T>
69 constexpr auto is_operator_not_eq_defined = cpp17::experimental::is_detected_v<operator_not_eq_t, T>;
70 
74 template<typename From, typename To>
75 using static_cast_t = decltype(static_cast<To>(std::declval<From>()));
76 
92 template<typename From, typename To>
93 constexpr auto is_static_cast_defined = cpp17::experimental::is_detected_v<static_cast_t, From, To>;
94 
98 template<typename T>
99 using operator_write_to_ostream_t = decltype(std::declval<std::ostream &>() << std::declval<T const&>());
100 
113 template<typename T>
114 constexpr auto is_operator_write_to_ostream_defined = cpp17::experimental::is_detected_v<operator_write_to_ostream_t, T>;
115 
119 template<typename T>
120 std::string typeString()
121 {
122  std::stringstream s;
123  s << typeid(T).name();
124  if(std::is_const_v<typename std::remove_reference<T>::type>)
125  s << " const";
126  if(std::is_reference_v<T>)
127  s << " &";
128  return s.str();
129 }
130 
131 }
decltype(static_cast< To >(std::declval< From >())) static_cast_t
Defines the type for static_cast<To>(From)
Definition: Metaprogramming.h:75
Definition: CircularBuffer.h:25
static U dynamic(T *iPtr)
Definition: Metaprogramming.h:37
std::string typeString()
typeid(T).name() does not account for const or reference.
Definition: Metaprogramming.h:120
dynamic_cast<U *>(x) does not compile if x is not polymorphic.
Definition: Metaprogramming.h:33
constexpr auto is_operator_write_to_ostream_defined
Allows to detect whether a type defines ostream << x at compile time.
Definition: Metaprogramming.h:114
decltype(std::declval< std::ostream & >()<< std::declval< T const & >()) operator_write_to_ostream_t
Defines the type for operator<<
Definition: Metaprogramming.h:99
constexpr auto is_operator_not_eq_defined
Allows to detect whether a type defines operator!= at compile time.
Definition: Metaprogramming.h:69
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:93
decltype(std::declval< T const & >() !=std::declval< T const & >()) operator_not_eq_t
Defines the type for operator!=
Definition: Metaprogramming.h:54