| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #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;
- void operator()(T & in) const { in = std::forward<U>(val); }
- void operator()(T * in) const { *in = std::forward<U>(val); }
- } visitor{std::forward<U>(val)};
- std::visit(visitor, ref_);
- return static_cast<T const &>(*this);
- }
- };
- }
|