tmay.hpp
Go to the documentation of this file.
1
2#pragma once
3
8#include "indep/helper/tmedium.inl"
9#include <optional>
10
11namespace by {
12
18 template <typename T> class tmay: private std::optional<T> {
19 BY(ME(tmay, std::optional<T>))
20
21 public:
22 tmay() = default;
23
24 tmay(const T& value);
25
26 tmay(const tmedium<T>& value);
27
28 public:
29 T* operator->();
30 const T* operator->() const BY_CONST_FUNC(operator->());
31
32 T& operator*();
33 const T& operator*() const BY_CONST_FUNC(operator*());
34
35 operator T&();
36 operator const T&() const;
37
38 operator nbool() const;
39
40 public:
41 nbool has() const;
42
43 T* get();
44 const T* get() const BY_CONST_FUNC(get())
45
46 virtual void rel();
47
48 virtual void set(const T& arg);
49 };
50
51 // extension for OR macro:
52 template <typename T, typename F> tmedium<T> operator|(T* t, F&& f) {
53 f(t);
54 // this returns null-reference but take it easy.
55 // it'll never be used.
56 return t;
57 }
58
59 template <typename T, typename F> tmedium<T> operator|(const T* t, F&& f) {
60 f(t);
61 // this may return null-reference but take it easy.
62 // it'll never be used.
63 return t;
64 }
65
66 template <typename T, typename F> T& operator|(tmay<T>& t, F&& f) {
67 f(t);
68 // this may return null-reference but take it easy.
69 // it'll never be used.
70 return *t;
71 }
72
73 template <typename T, typename F> const T& operator|(const tmay<T>& t, F&& f) {
74 f(t);
75 // this may return null-reference but take it easy.
76 // it'll never be used.
77 return *t;
78 }
79
80 template <typename T, typename F> tmay<T> operator|(tmay<T>&& t, F&& f) {
81 f(t);
82 // this may return null-reference but take it easy.
83 // it'll never be used.
84 return t;
85 }
86
87 // extension for typeTrait:
88 template <typename T> struct typeTrait<tmay<T>> {
89 static nbool isNul(const tmay<T>& it) { return !it.has(); }
90
91 static constexpr nbool is_ptr = false;
92 static constexpr nbool is_ref = false;
93 static constexpr nbool is_like_ptr = true;
94 };
95
96 template <typename T> struct typeTrait<tmay<T>&> {
97 static nbool isNul(const tmay<T>& it) { return !it.has(); }
98
99 static constexpr nbool is_ptr = false;
100 static constexpr nbool is_ref = true;
101 static constexpr nbool is_like_ptr = true;
102 };
103
104 // extension for TO macro:
105 template <typename T, typename F>
106 auto operator->*(tmay<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
107 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
108 }
109
110 template <typename T, typename F>
111 auto operator->*(const tmay<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
112 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
113 }
114
115 template <typename T, typename F>
116 auto operator->*(tmay<T>&& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
117 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
118 }
119
120} // namespace by
Optional value wrapper with byeol-style interface.
Definition: tmay.hpp:18
Medium class used exclusively in the OR macro for safe reference handling.
Definition: tmedium.hpp:51
Type trait utilities for template metaprogramming.
Definition: typeTrait.hpp:13