FE 0.6.0
A header-only C++ library for writing frontends
Loading...
Searching...
No Matches
cast.h
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4
5namespace fe {
6
7template<class T>
8concept Nodeable = requires(T n) {
9 T::Node;
10 n.node();
11};
12
13/// Inherit from this class using [CRTP](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern),
14/// for some nice `dynamic_cast`-style wrappers.
15template<class B>
17public:
18 // clang-format off
19 /// `static_cast` with debug check.
20 template<class T> T* as() { assert(isa<T>()); return static_cast<T*>(this); }
21
22 /// `dynamic_cast`.
23 /// If @p T isa fe::Nodeable, it will use `node()`, otherwise a `dynamic_cast`.
24 template<class T>
25 T* isa() {
26 if constexpr (Nodeable<T>) {
27 return static_cast<B*>(this)->node() == T::Node ? static_cast<T*>(this) : nullptr;
28 } else {
29 return dynamic_cast<T*>(static_cast<B*>(this));
30 }
31 }
32
33 template<class T> const T* as() const { return const_cast<RuntimeCast*>(this)->template as<T >(); } ///< `const` version.
34 template<class T> const T* isa() const { return const_cast<RuntimeCast*>(this)->template isa<T >(); } ///< `const` version.
35 // clang-format on
36};
37
38} // namespace fe
Inherit from this class using CRTP, for some nice dynamic_cast-style wrappers.
Definition cast.h:16
T * as()
static_cast with debug check.
Definition cast.h:20
T * isa()
dynamic_cast.
Definition cast.h:25
const T * isa() const
const version.
Definition cast.h:34
const T * as() const
const version.
Definition cast.h:33
Definition arena.h:10