out.h 519 B

12345678910111213141516171819202122232425262728293031323334
  1. #pragma once
  2. #include <utility>
  3. namespace jvalidate::detail {
  4. constexpr struct discard_out_t {
  5. } discard_out;
  6. template <typename T> class out {
  7. private:
  8. T * ref_ = nullptr;
  9. public:
  10. out() = default;
  11. out(discard_out_t) {}
  12. out(T & ref) : ref_(&ref) {}
  13. explicit operator bool() const { return ref_; }
  14. void operator=(T && val) {
  15. if (ref_) {
  16. *ref_ = std::move(val);
  17. }
  18. return *this;
  19. }
  20. void operator=(T const & val) {
  21. if (ref_) {
  22. *ref_ = val;
  23. }
  24. return *this;
  25. }
  26. };
  27. }