Loading...
Searching...
No Matches
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
50 template <typename T> class tmay: private std::optional<T> {
51 BY(ME(tmay, std::optional<T>))
52
53 public:
54 tmay() = default;
55 tmay(const T& value);
56 tmay(const tmedium<T>& value);
57
58 public:
59 T* operator->();
60 const T* operator->() const BY_CONST_FUNC(operator->());
61 T& operator*();
62 const T& operator*() const BY_CONST_FUNC(operator*());
63 operator T&();
64 operator const T&() const;
65 operator nbool() const;
66
67 public:
68 nbool has() const;
75 T* get();
76 const T* get() const BY_CONST_FUNC(get())
80 virtual void rel();
81 virtual void set(const T& arg);
82 };
83
84 // extension for OR macro:
86 f(t);
87 // this returns null-reference but take it easy.
88 // it'll never be used.
89 return t;
90 }
91
92 template <typename T, typename F> tmedium<T> operator|(const T* t, F&& f) {
93 f(t);
94 // this may return null-reference but take it easy.
95 // it'll never be used.
96 return t;
97 }
98
99 template <typename T, typename F> T& operator|(tmay<T>& t, F&& f) {
100 f(t);
101 // this may return null-reference but take it easy.
102 // it'll never be used.
103 return *t;
104 }
105
106 template <typename T, typename F> const T& operator|(const tmay<T>& t, F&& f) {
107 f(t);
108 // this may return null-reference but take it easy.
109 // it'll never be used.
110 return *t;
111 }
112
113 template <typename T, typename F> tmay<T> operator|(tmay<T>&& t, F&& f) {
114 f(t);
115 // this may return null-reference but take it easy.
116 // it'll never be used.
117 return t;
118 }
119
120 // extension for typeTrait:
121 template <typename T> struct typeTrait<tmay<T>> {
122 static nbool isNul(const tmay<T>& it) { return !it.has(); }
123
124 static constexpr nbool is_ptr = false;
125 static constexpr nbool is_ref = false;
126 static constexpr nbool is_like_ptr = true;
127 };
128
129 template <typename T> struct typeTrait<tmay<T>&> {
130 static nbool isNul(const tmay<T>& it) { return !it.has(); }
131
132 static constexpr nbool is_ptr = false;
133 static constexpr nbool is_ref = true;
134 static constexpr nbool is_like_ptr = true;
135 };
136
137 // extension for TO macro:
138 template <typename T, typename F>
139 auto operator->*(tmay<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
140 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
141 }
142
143 template <typename T, typename F>
144 auto operator->*(const tmay<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
145 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
146 }
147
148 template <typename T, typename F>
149 auto operator->*(tmay<T>&& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
150 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
151 }
152
153} // namespace by
Optional value wrapper for error indication without exceptions.
Definition tmay.hpp:50
const T * get() const BY_CONST_FUNC(get()) virtual void rel()
Releases/clears the contained value, making the instance empty.
T * get()
Gets the contained value.
Medium class used exclusively in the OR macro for safe reference handling.
Definition tmedium.hpp:52
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34
Type trait utilities for template metaprogramming.
Definition typeTrait.hpp:14