| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #pragma once
- #include <type_traits>
- #include <utility>
- #include <variant>
- namespace jvalidate::detail {
- constexpr struct discard_out_t {
- } discard_out;
- template <typename T>
- requires(std::is_same_v<T, std::decay_t<T>>)
- class out {
- private:
- T * ref_ = nullptr;
- public:
- out() = default;
- out(discard_out_t) {}
- out(T & ref) : ref_(&ref) {}
- explicit operator bool() const { return ref_; }
- template <typename U>
- requires std::is_constructible_v<T, U>
- void operator=(U && val) {
- if (ref_) {
- *ref_ = std::forward<U>(val);
- }
- return *this;
- }
- };
- template <typename T>
- requires(std::is_same_v<T, std::decay_t<T>>)
- class inout {
- private:
- std::variant<T, T *> ref_;
- public:
- inout(T && value) : ref_(std::move(value)) {}
- inout(T & ref) : ref_(&ref) {}
- operator T const &() const {
- struct {
- T const & operator()(T const & in) const { return in; }
- T const & operator()(T * in) const { return *in; }
- } visitor;
- return std::visit(visitor, ref_);
- }
- template <typename U>
- requires std::is_constructible_v<T, U>
- T const & operator=(U && val) {
- struct {
- U && val;
- T const & operator()(T & in) const { return in = std::forward<U>(val); }
- T const & operator()(T * in) const { return *in = std::forward<U>(val); }
- } visitor{std::forward<U>(val)};
- return std::visit(visitor, ref_);
- }
- };
- }
|