CellModules
introspect.hpp
Go to the documentation of this file.
1#ifndef MECACELL_INTROSPECT_HPP
2#define MECACELL_INTROSPECT_HPP
3#include <cxxabi.h>
4#include <functional>
5#include <iostream>
6#include <string>
7#include <typeinfo>
8#include <utility>
9
10namespace MecaCell {
11template <typename... Ts> struct make_void { typedef void type; };
12template <typename... Ts> using void_t = typename make_void<Ts...>::type;
13}
14
15// displays type name, for debugging purpose
16#define DEBUG_TYPE(x) \
17 do { \
18 typedef void(*T) x; \
19 debug_type<T>(T(), #x); \
20 } while (0)
21
22template <typename T> struct debug_type {
23 template <typename U> debug_type(void (*)(U), const std::string &p_str) {
24 std::string str(p_str.begin() + 1, p_str.end() - 1);
25 std::cout << str << " => ";
26 char *name = nullptr;
27 int status;
28 name = abi::__cxa_demangle(typeid(U).name(), 0, 0, &status);
29 if (name) {
30 std::cout << name << std::endl;
31 } else {
32 std::cout << typeid(U).name() << std::endl;
33 }
34 free(name);
35 }
36};
37
38#define CREATE_METHOD_CHECKS(method) \
39 /* checks if Class C has a method callable using the given signature */ \
40 template <typename C, typename F> struct is_##method##_callable {}; \
41 template <typename C, typename Ret, typename... Args> \
42 struct is_##method##_callable<C, Ret(Args...)> { \
43 template <typename T> static constexpr bool is(...) { return false; } \
44 template <typename T> \
45 static constexpr bool is( \
46 typename std::is_same< \
47 Ret, decltype(std::declval<T>().method(std::declval<Args...>()))>::type *) { \
48 return true; \
49 } \
50 static constexpr bool value = is<C>(nullptr); \
51 }; \
52 /* checks if Class C has a method with the exact given signature */ \
53 template <typename C, typename F> struct has_##method##_signature {}; \
54 template <typename C, typename Ret, typename... Args> \
55 struct has_##method##_signature<C, Ret(Args...)> { \
56 using SIG = Ret (C::*)(Args...); \
57 template <typename T, T> struct same; \
58 template <typename T> \
59 static constexpr auto has(same<SIG, &T::method> *) -> typename std::true_type::type; \
60 template <typename T> \
61 static constexpr auto has(...) -> typename std::false_type::type; \
62 static constexpr bool value = decltype(has<C>(0))::value; \
63 }
64
65// helper that explodes a tuple and forward its content to a func
66// (+ other args at the begining) ... C++14 only :'(
67// template <typename F, typename... OtherArgs, typename... TupleTypes, std::size_t...
68// Ind>
69// auto callExpand(F &&f, OtherArgs &&... otherArgs, const std::tuple<TupleTypes...>
70// &tuple,
71// std::index_sequence<Ind...>) {
72// return std::forward<F>(f)(std::forward<OtherArgs>(otherArgs)...,
73// std::get<Ind>(tuple)...);
74//}
75// template <typename F, typename... OtherArgs, typename... TupleTypes>
76// auto callWithExpandedTuple(F &&f, OtherArgs &&... otherArgs,
77// const std::tuple<TupleTypes...> &tuple) {
78// return callExpand(std::forward<F>(f), std::forward<OtherArgs>(otherArgs)..., tuple,
79// std::index_sequence_for<TupleTypes...>());
80//}
81
82#endif
this file contains various miscellanious utility functions & helpers *
typename make_void< Ts... >::type void_t
Definition: introspect.hpp:12
iostream cout
Standard output stream.
Definition: std.hpp:267
iostream endl
End-of-line manipulator.
Definition: std.hpp:274
debug_type(void(*)(U), const std::string &p_str)
Definition: introspect.hpp:23