9#ifndef FORTRAN_COMMON_UNWRAP_H_
10#define FORTRAN_COMMON_UNWRAP_H_
12#include "indirection.h"
13#include "reference-counted.h"
41template <
typename A,
typename B>
42using Constify = std::conditional_t<std::is_const_v<B> && !std::is_const_v<A>,
43 std::add_const_t<A>, A>;
50 template <
typename A,
typename B>
51 static auto Unwrap(B &x) -> Constify<A, B> * {
52 if constexpr (std::is_same_v<std::decay_t<A>, std::decay_t<B>>) {
60 template <
typename A,
typename B>
61 static auto Unwrap(B *p) -> Constify<A, B> * {
69 template <
typename A,
typename B>
70 static auto Unwrap(
const std::unique_ptr<B> &p) -> Constify<A, B> * {
78 template <
typename A,
typename B>
79 static auto Unwrap(
const std::shared_ptr<B> &p) -> Constify<A, B> * {
87 template <
typename A,
typename B>
88 static auto Unwrap(std::optional<B> &x) -> Constify<A, B> * {
96 template <
typename A,
typename B>
97 static auto Unwrap(
const std::optional<B> &x) -> Constify<A, B> * {
105 template <
typename A,
typename... Bs>
106 static A *Unwrap(std::variant<Bs...> &u) {
107 return common::visit(
109 using Ty = std::decay_t<decltype(Unwrap<A>(x))>;
110 if constexpr (!std::is_const_v<std::remove_pointer_t<Ty>> ||
111 std::is_const_v<A>) {
119 template <
typename A,
typename... Bs>
120 static auto Unwrap(
const std::variant<Bs...> &u) -> std::add_const_t<A> * {
121 return common::visit(
122 [](
const auto &x) -> std::add_const_t<A> * {
return Unwrap<A>(x); }, u);
125 template <
typename A,
typename B>
126 static auto Unwrap(
const Reference<B> &ref) -> Constify<A, B> * {
127 return Unwrap<A>(*ref);
130 template <
typename A,
typename B,
bool COPY>
132 return Unwrap<A>(p.value());
135 template <
typename A,
typename B>
138 return Unwrap<A>(*p);
145template <
typename A,
typename B>
auto Unwrap(B &x) -> Constify<A, B> * {
146 return UnwrapperHelper::Unwrap<A>(x);
150template <
typename A,
typename B> std::optional<A> UnwrapCopy(
const B &x) {
151 if (
const A * p{Unwrap<A>(x)}) {
152 return std::make_optional<A>(*p);
Definition: reference-counted.h:35
Definition: indirection.h:31
Definition: reference.h:18
Definition: bit-population-count.h:20