mlt-ffi 0.2.0

Diplomat FFI bindings for mlt-core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#ifndef DIPLOMAT_RUNTIME_CPP_H
#define DIPLOMAT_RUNTIME_CPP_H

#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>

#if __cplusplus >= 202002L
#include <span>
#else
#include <array>
#endif

#ifndef DIPLOMAT_LIFETIME_BOUND
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(msvc::lifetimebound)
#define DIPLOMAT_LIFETIME_BOUND [[msvc::lifetimebound]]
#elif __has_cpp_attribute(clang::lifetimebound)
#define DIPLOMAT_LIFETIME_BOUND [[clang::lifetimebound]]
#endif
#endif
#endif

#ifndef DIPLOMAT_LIFETIME_BOUND
#define DIPLOMAT_LIFETIME_BOUND
#endif

namespace diplomat {

namespace capi {
extern "C" {

static_assert(sizeof(char) == sizeof(uint8_t), "your architecture's `char` is not 8 bits");
static_assert(sizeof(char16_t) == sizeof(uint16_t), "your architecture's `char16_t` is not 16 bits");
static_assert(sizeof(char32_t) == sizeof(uint32_t), "your architecture's `char32_t` is not 32 bits");

typedef struct DiplomatWrite {
    void* context;
    char* buf;
    size_t len;
    size_t cap;
    bool grow_failed;
    void (*flush)(struct DiplomatWrite*);
    bool (*grow)(struct DiplomatWrite*, size_t);
} DiplomatWrite;

bool diplomat_is_str(const char* buf, size_t len);

#define MAKE_SLICES(name, c_ty)              \
    typedef struct Diplomat##name##View {    \
        const c_ty* data;                    \
        size_t len;                          \
    } Diplomat##name##View;                  \
    typedef struct Diplomat##name##ViewMut { \
        c_ty* data;                          \
        size_t len;                          \
    } Diplomat##name##ViewMut;               \
    typedef struct Diplomat##name##Array {   \
        const c_ty* data;                    \
        size_t len;                          \
    } Diplomat##name##Array;

#define MAKE_SLICES_AND_OPTIONS(name, c_ty) \
    MAKE_SLICES(name, c_ty)                 \
    typedef struct Option##name {           \
        union {                             \
            c_ty ok;                        \
        };                                  \
        bool is_ok;                         \
    } Option##name;                         \
    typedef struct Option##name##View {     \
        union {                             \
            Diplomat##name##View ok;        \
        };                                  \
        bool is_ok;                         \
    } Option##name##View;                   \
    typedef struct Option##name##ViewMut {  \
        union {                             \
            Diplomat##name##ViewMut ok;     \
        };                                  \
        bool is_ok;                         \
    } Option##name##ViewMut;                \
    typedef struct Option##name##Array {    \
        union {                             \
            Diplomat##name##Array ok;       \
        };                                  \
        bool is_ok;                         \
    } Option##name##Array;

MAKE_SLICES_AND_OPTIONS(I8, int8_t)
MAKE_SLICES_AND_OPTIONS(U8, uint8_t)
MAKE_SLICES_AND_OPTIONS(I16, int16_t)
MAKE_SLICES_AND_OPTIONS(U16, uint16_t)
MAKE_SLICES_AND_OPTIONS(I32, int32_t)
MAKE_SLICES_AND_OPTIONS(U32, uint32_t)
MAKE_SLICES_AND_OPTIONS(I64, int64_t)
MAKE_SLICES_AND_OPTIONS(U64, uint64_t)
MAKE_SLICES_AND_OPTIONS(Isize, intptr_t)
MAKE_SLICES_AND_OPTIONS(Usize, size_t)
MAKE_SLICES_AND_OPTIONS(F32, float)
MAKE_SLICES_AND_OPTIONS(F64, double)
MAKE_SLICES_AND_OPTIONS(Bool, bool)
MAKE_SLICES_AND_OPTIONS(Char, char32_t)
MAKE_SLICES_AND_OPTIONS(String, char)
MAKE_SLICES_AND_OPTIONS(String16, char16_t)
MAKE_SLICES_AND_OPTIONS(Strings, DiplomatStringView)
MAKE_SLICES_AND_OPTIONS(Strings16, DiplomatString16View)

} // extern "C"
} // namespace capi

extern "C" inline void _flush(capi::DiplomatWrite* w) {
    std::string* string = reinterpret_cast<std::string*>(w->context);
    string->resize(w->len);
}

extern "C" inline bool _grow_impl(capi::DiplomatWrite* w, uintptr_t requested) {
    std::string* string = reinterpret_cast<std::string*>(w->context);
    string->resize(requested);
    w->cap = string->length();
    w->buf = &(*string)[0];
    return true;
}

extern "C" inline bool _grow(capi::DiplomatWrite* w, size_t requested) {
    return _grow_impl(w, static_cast<uintptr_t>(requested));
}

inline capi::DiplomatWrite WriteFromString(std::string& string) {
    capi::DiplomatWrite w;
    w.context = &string;
    w.buf = &string[0];
    w.len = string.length();
    w.cap = string.length();
    // Will never become true, as _grow is infallible.
    w.grow_failed = false;
    w.flush = _flush;
    w.grow = _grow;
    return w;
}

// This "trait" allows one to use _write() methods to efficiently
// write to a custom string type. To do this you need to write a specialized
// `WriteTrait<YourType>` (see WriteTrait<std::string> below)
// that is capable of constructing a DiplomatWrite, which can wrap
// your string type with appropriate resize/flush functionality.
template <typename T>
struct WriteTrait {
    // Fill in this method on a specialization to implement this trait
    // static inline capi::DiplomatWrite Construct(T& t);
};

template <>
struct WriteTrait<std::string> {
    static inline capi::DiplomatWrite Construct(std::string& t) { return diplomat::WriteFromString(t); }
};

template <class T>
struct Ok {
    T inner;

    // Move constructor always allowed
    Ok(T&& i)
        : inner(std::forward<T>(i)) {}

    //  copy constructor allowed only for trivially copyable types
    template <typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
    Ok(const T& i)
        : inner(i) {}

    Ok() = default;
    Ok(Ok&&) noexcept = default;
    Ok(const Ok&) = default;
    Ok& operator=(const Ok&) = default;
    Ok& operator=(Ok&&) noexcept = default;
};

template <class T>
struct Err {
    T inner;

    // Move constructor always allowed
    Err(T&& i)
        : inner(std::forward<T>(i)) {}

    //  copy constructor allowed only for trivially copyable types
    template <typename X = T, typename = typename std::enable_if<std::is_trivially_copyable<X>::value>::type>
    Err(const T& i)
        : inner(i) {}

    Err() = default;
    Err(Err&&) noexcept = default;
    Err(const Err&) = default;
    Err& operator=(const Err&) = default;
    Err& operator=(Err&&) noexcept = default;
};

template <class T, class E>
class result {
protected:
    std::variant<Ok<T>, Err<E>> val;

public:
    result(Ok<T>&& v)
        : val(std::move(v)) {}
    result(Err<E>&& v)
        : val(std::move(v)) {}
    result() = default;
    result(const result&) = default;
    result& operator=(const result&) = default;
    result& operator=(result&&) noexcept = default;
    result(result&&) noexcept = default;
    ~result() = default;
    bool is_ok() const { return std::holds_alternative<Ok<T>>(this->val); }
    bool is_err() const { return std::holds_alternative<Err<E>>(this->val); }

    template <typename U = T, typename std::enable_if_t<!std::is_reference_v<U>, std::nullptr_t> = nullptr>
    std::optional<T> ok() && {
        if (!this->is_ok()) {
            return std::nullopt;
        }
        return std::make_optional(std::move(std::get<Ok<T>>(std::move(this->val)).inner));
    }

    template <typename U = E, typename std::enable_if_t<!std::is_reference_v<U>, std::nullptr_t> = nullptr>
    std::optional<E> err() && {
        if (!this->is_err()) {
            return std::nullopt;
        }
        return std::make_optional(std::move(std::get<Err<E>>(std::move(this->val)).inner));
    }

    // std::optional does not work with reference types directly, so wrap them if present
    template <typename U = T, typename std::enable_if_t<std::is_reference_v<U>, std::nullptr_t> = nullptr>
    std::optional<std::reference_wrapper<std::remove_reference_t<T>>> ok() && {
        if (!this->is_ok()) {
            return std::nullopt;
        }
        return std::make_optional(std::reference_wrapper(std::forward<T>(std::get<Ok<T>>(std::move(this->val)).inner)));
    }

    template <typename U = E, typename std::enable_if_t<std::is_reference_v<U>, std::nullptr_t> = nullptr>
    std::optional<std::reference_wrapper<std::remove_reference_t<E>>> err() && {
        if (!this->is_err()) {
            return std::nullopt;
        }
        return std::make_optional(
            std::reference_wrapper(std::forward<E>(std::get<Err<E>>(std::move(this->val)).inner)));
    }

    void set_ok(T&& t) { this->val = Ok<T>(std::move(t)); }

    void set_err(E&& e) { this->val = Err<E>(std::move(e)); }

    template <typename T2>
    result<T2, E> replace_ok(T2&& t) {
        if (this->is_err()) {
            return result<T2, E>(Err<E>(std::get<Err<E>>(std::move(this->val))));
        } else {
            return result<T2, E>(Ok<T2>(std::move(t)));
        }
    }
};

class Utf8Error {};

// Use custom std::span on C++17, otherwise use std::span
#if __cplusplus >= 202002L

constexpr std::size_t dynamic_extent = std::dynamic_extent;
template <class T, std::size_t E = dynamic_extent>
using span = std::span<T, E>;

#else // __cplusplus < 202002L

// C++-17-compatible-ish std::span
constexpr size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
template <class T, std::size_t Extent = dynamic_extent>
class span {
public:
    constexpr span(T* data = nullptr, size_t size = Extent)
        : data_(data),
          size_(size) {}

    constexpr span(const span<T>& o)
        : data_(o.data_),
          size_(o.size_) {}
    template <size_t N>
    constexpr span(std::array<typename std::remove_const_t<T>, N>& arr)
        : data_(const_cast<T*>(arr.data())),
          size_(N) {}

    constexpr T* data() const noexcept { return this->data_; }
    constexpr size_t size() const noexcept { return this->size_; }

    constexpr T* begin() const noexcept { return data(); }
    constexpr T* end() const noexcept { return data() + size(); }

    void operator=(span<T> o) {
        data_ = o.data_;
        size_ = o.size_;
    }

private:
    T* data_;
    size_t size_;
};

#endif // __cplusplus >= 202002L

// An ABI stable std::basic_string_view equivalent for the case of string
// views in slices
template <class CharT, class Traits = std::char_traits<CharT>>
class basic_string_view_for_slice {
public:
    using std_string_view = std::basic_string_view<CharT, Traits>;
    using traits_type = typename std_string_view::traits_type;
    using value_type = typename std_string_view::value_type;
    using pointer = typename std_string_view::pointer;
    using const_pointer = typename std_string_view::const_pointer;
    using size_type = typename std_string_view::size_type;
    using difference_type = typename std_string_view::difference_type;

    constexpr basic_string_view_for_slice() noexcept
        : basic_string_view_for_slice{std_string_view{}} {}

    constexpr basic_string_view_for_slice(const basic_string_view_for_slice& other) noexcept = default;

    constexpr basic_string_view_for_slice(const const_pointer s, const size_type count)
        : basic_string_view_for_slice{std_string_view{s, count}} {}

    constexpr basic_string_view_for_slice(const const_pointer s)
        : basic_string_view_for_slice{std_string_view{s}} {}

    constexpr basic_string_view_for_slice& operator=(const basic_string_view_for_slice& view) noexcept = default;

    constexpr basic_string_view_for_slice(const std_string_view& s) noexcept
        : data_{s.data(), s.size()} {}

    constexpr basic_string_view_for_slice& operator=(const std_string_view& s) noexcept {
        data_ = {s.data(), s.size()};
        return *this;
    }

    constexpr operator std_string_view() const noexcept { return {data(), size()}; }
    constexpr std_string_view as_sv() const noexcept { return *this; }

    constexpr const_pointer data() const noexcept { return data_.data; }
    constexpr size_type size() const noexcept { return data_.len; }

private:
    using capi_type =
        std::conditional_t<std::is_same_v<value_type, char>,
                           capi::DiplomatStringView,
                           std::conditional_t<std::is_same_v<value_type, char16_t>, capi::DiplomatString16View, void>>;

    static_assert(!std::is_void_v<capi_type>, "ABI compatible string_views are only supported for char and char16_t");

    capi_type data_;
};

// We only implement these specialisations as diplomat doesn't provide c abi
// types for others
using string_view_for_slice = basic_string_view_for_slice<char>;
using u16string_view_for_slice = basic_string_view_for_slice<char16_t>;

using string_view_span = span<const string_view_for_slice>;
using u16string_view_span = span<const u16string_view_for_slice>;

// Interop between std::function & our C Callback wrapper type

template <typename T, typename = void>
struct as_ffi {
    using type = T;
};

template <typename T>
struct as_ffi<T, std::void_t<decltype(std::declval<std::remove_pointer_t<T>>().AsFFI())>> {
    using type = decltype(std::declval<std::remove_pointer_t<T>>().AsFFI());
};

template <typename T>
struct as_ffi<std::unique_ptr<T>> {
    using type = decltype(std::declval<T>().AsFFI());
};

template <typename T>
using as_ffi_t = typename as_ffi<T>::type;

template <typename T>
using replace_string_view_t = std::conditional_t<std::is_same_v<T, std::string_view>, capi::DiplomatStringView, T>;

template <typename T, typename = void>
struct diplomat_c_span_convert {
    using type = T;
};

#define MAKE_SLICE_CONVERTERS(name, c_ty)                                                      \
    template <typename T>                                                                      \
    struct diplomat_c_span_convert<T, std::enable_if_t<std::is_same_v<T, span<const c_ty>>>> { \
        using type = diplomat::capi::Diplomat##name##View;                                     \
    };                                                                                         \
    template <typename T>                                                                      \
    struct diplomat_c_span_convert<T, std::enable_if_t<std::is_same_v<T, span<c_ty>>>> {       \
        using type = diplomat::capi::Diplomat##name##ViewMut;                                  \
    };

#if !defined(__sun) || !defined(_CHAR_IS_SIGNED)
// int8_t and char are the same type on Solaris. Guard this definition to avoid
// conflicts.
MAKE_SLICE_CONVERTERS(I8, int8_t)
#endif
MAKE_SLICE_CONVERTERS(U8, uint8_t)
MAKE_SLICE_CONVERTERS(I16, int16_t)
MAKE_SLICE_CONVERTERS(U16, uint16_t)
MAKE_SLICE_CONVERTERS(I32, int32_t)
MAKE_SLICE_CONVERTERS(U32, uint32_t)
MAKE_SLICE_CONVERTERS(I64, int64_t)
MAKE_SLICE_CONVERTERS(U64, uint64_t)
MAKE_SLICE_CONVERTERS(F32, float)
MAKE_SLICE_CONVERTERS(F64, double)
MAKE_SLICE_CONVERTERS(Bool, bool)
MAKE_SLICE_CONVERTERS(Char, char32_t)
MAKE_SLICE_CONVERTERS(String, char)
MAKE_SLICE_CONVERTERS(String16, char16_t)

template <typename T>
using diplomat_c_span_convert_t = typename diplomat_c_span_convert<T>::type;

template <typename T>
struct is_unique_ptr {
    static constexpr bool value = false;
    using type = T;
};

template <typename T>
struct is_unique_ptr<std::unique_ptr<T>> {
    static constexpr bool value = true;
    using type = T;
};

template <typename T>
constexpr bool is_unique_ptr_v = is_unique_ptr<T>::value;

template <typename T>
struct is_optional {
    static constexpr bool value = false;
};

template <typename T>
struct is_optional<std::optional<T>> {
    static constexpr bool value = true;
};

template <typename T>
constexpr bool is_optional_v = is_optional<T>::value;

/// Replace the argument types from the std::function with the argument types for th function pointer
template <typename T>
using replace_fn_t = diplomat_c_span_convert_t<replace_string_view_t<as_ffi_t<T>>>;

namespace fn_trait_helpers {
// For a given T, creates a function that take in the C ABI version & return the C++ type.
template <typename T>
T replace(replace_fn_t<T> val) {
    if constexpr (std::is_same_v<T, std::string_view>) {
        return std::string_view{val.data, val.len};
    } else if constexpr (!std::is_same_v<T, diplomat_c_span_convert_t<T>>) {
        return T{val.data, val.len};
    } else if constexpr (!std::is_same_v<T, as_ffi_t<T>>) {
        if constexpr (std::is_lvalue_reference_v<T>) {
            return *std::remove_reference_t<T>::FromFFI(val);
        } else if constexpr (is_unique_ptr_v<T>) {
            return T(is_unique_ptr<T>::type::FromFFI(val));
        } else {
            return T::FromFFI(val);
        }
    } else {
        return val;
    }
}

template <typename T>
replace_fn_t<T> replace_ret(T val) {
    if constexpr (std::is_same_v<T, std::string_view>) {
        return {val.data(), val.size()};
    } else if constexpr (!std::is_same_v<T, diplomat_c_span_convert_t<T>>) {
        // Can we convert straight away to our slice type, or (in the case of ABI compatible structs), do we have to do
        // a reinterpret cast?
        if constexpr (std::is_same_v<decltype(std::declval<T>().data()), decltype(replace_fn_t<T>::data)>) {
            return replace_fn_t<T>{val.data(), val.size()};
        } else {
            return replace_fn_t<T>{reinterpret_cast<decltype(replace_fn_t<T>::data)>(val.data()), val.size()};
        }
    } else if constexpr (!std::is_same_v<T, as_ffi_t<T>>) {
        return val.AsFFI();
    } else {
        return val;
    }
}

template <typename TOut, typename T>
TOut replace_optional_ret(std::optional<T> optional) {
    constexpr bool has_ok = !std::is_same_v<T, std::monostate>;

    bool is_ok = optional.has_value();

    TOut out;
    out.is_ok = is_ok;

    if constexpr (has_ok) {
        if (is_ok) {
            out.ok = replace_ret<T>(optional.value());
        }
    }
    return out;
}

template <typename T, typename E, typename TOut>
TOut replace_result(diplomat::result<T, E> res) {
    auto is_ok = res.is_ok();

    constexpr bool has_ok = !std::is_same_v<T, std::monostate>;
    constexpr bool has_err = !std::is_same_v<E, std::monostate>;

    TOut out;
    out.is_ok = is_ok;

    if constexpr (has_ok) {
        if (is_ok) {
            auto val = std::move(res).ok().value();
            if constexpr (is_optional_v<T>) {
                out.ok = replace_optional_ret<decltype(out.ok)>(val);
            } else {
                out.ok = replace_ret<T>(val);
            }
        }
    }

    if constexpr (has_err) {
        if (!is_ok) {
            auto val = std::move(res).err().value();
            if constexpr (is_optional_v<E>) {
                out.err = replace_optional_ret<decltype(out.err)>(val);
            } else {
                out.err = replace_ret<E>(val);
            }
        }
    }

    return out;
}
} // namespace fn_trait_helpers

template <typename T>
struct fn_traits;

template <typename Ret, typename... Args>
struct fn_traits<std::function<Ret(Args...)>> {
    using fn_ptr_t = Ret(Args...);
    using function_t = std::function<fn_ptr_t>;
    using ret = Ret;

    static Ret c_run_callback(const void* cb, replace_fn_t<Args>... args) {
        return (*reinterpret_cast<const function_t*>(cb))(fn_trait_helpers::replace<Args>(args)...);
    }

    template <typename T, typename E, typename TOut>
    static TOut c_run_callback_result(const void* cb, replace_fn_t<Args>... args) {
        result<T, E> res = c_run_callback(cb, args...);

        return fn_trait_helpers::replace_result<T, E, TOut>(res);
    }

    // For DiplomatOption<>
    template <typename T, typename TOut>
    static TOut c_run_callback_diplomat_option(const void* cb, replace_fn_t<Args>... args) {
        std::optional<T> ret = c_run_callback(cb, args...);

        return fn_trait_helpers::replace_optional_ret<TOut>(ret);
    }

    // All we need to do is just convert one pointer to another, while keeping the arguments the same:
    template <typename T>
    static T c_run_callback_diplomat_opaque(const void* cb, replace_fn_t<Args>... args) {
        Ret out = c_run_callback(cb, args...);

        if constexpr (std::is_pointer_v<Ret>) {
            return out->AsFFI();
        } else {
            return out.AsFFI();
        }
    }

    static void c_delete(const void* cb) { delete reinterpret_cast<const function_t*>(cb); }

    fn_traits(function_t) {} // Allows less clunky construction (avoids decltype)
};

// additional deduction guide required
template <class T>
fn_traits(T) -> fn_traits<T>;

// Trait for extracting inner types from either T*, std::optional, or std::unique_ptr.
// These are the three potential types returned by next() functions
template <typename T>
struct inner { /* only T*, std::optional, and std::unique_ptr are supported */
};
template <typename T>
struct inner<T*> {
    using type = T;
};
template <typename T>
struct inner<std::unique_ptr<T>> {
    using type = T;
};
template <typename T>
struct inner<std::optional<T>> {
    using type = T;
};

template <typename T, typename U = typename inner<T>::type>
inline const U get_inner_if_present(T v) {
    if constexpr (std::is_same_v<T, U>) {
        return std::move(v);
    } else {
        return *std::move(v);
    }
}

// Adapter for iterator types
template <typename T, typename U = void>
struct has_next : std::false_type {};
template <typename T>
struct has_next<T, std::void_t<decltype(std::declval<T>().next())>> : std::true_type {};
template <typename T>
constexpr bool has_next_v = has_next<T>::value;

/// Helper template enabling native iteration over unique ptrs to objects which implement next()
template <typename T>
struct next_to_iter_helper {
    static_assert(has_next_v<T>, "next_to_iter_helper may only be used with types implementing next()");
    using next_type = decltype(std::declval<T>().next());

    // STL Iterator trait definitions
    using value_type = typename inner<next_type>::type;
    using difference_type = void;
    using reference = std::add_lvalue_reference_t<value_type>;
    using iterator_category = std::input_iterator_tag;

    next_to_iter_helper(std::unique_ptr<T>&& ptr)
        : _ptr(std::move(ptr)),
          _curr(_ptr->next()) {}

    // https://en.cppreference.com/w/cpp/named_req/InputIterator requires that the type be copyable
    next_to_iter_helper(const next_to_iter_helper& o)
        : _ptr(o._ptr),
          _curr(o._curr) {}

    void operator++() { _curr = _ptr->next(); }
    void operator++(int) { ++(*this); }
    const value_type& operator*() const { return *_curr; }

    bool operator!=(std::nullopt_t) { return (bool)_curr; }

    std::shared_ptr<T> _ptr; // shared to satisfy the copyable requirement
    next_type _curr;
};

} // namespace diplomat

#endif