FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
ref.hpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_UTIL_REF_HPP
5#define FTXUI_UTIL_REF_HPP
6
8#include <memory>
9#include <string>
10#include <string_view>
11#include <variant>
12#include <vector>
13
14#include "ftxui/util/export.hpp"
15
16namespace ftxui {
17
18/// @brief アダプター。不変オブジェクトを所有または参照します。
19template <typename T>
20class ConstRef {
21 public:
22 ConstRef() = default;
23
24 // 所有コンストラクタ:
25 ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
26
27 // 参照コンストラクタ:
28 ConstRef(const T* t) : variant_(t) {} // NOLINT
29
30 ConstRef& operator=(ConstRef&&) noexcept = default;
31 ConstRef(const ConstRef<T>&) = default;
32 ConstRef(ConstRef<T>&&) noexcept = default;
33 ~ConstRef() = default;
34
35 // "再着席可能な"参照を作成します
36 ConstRef<T>& operator=(const ConstRef<T>&) = default;
37
38 // アクセサ:
39 const T& operator()() const { return *Address(); }
40 const T& operator*() const { return *Address(); }
41 const T* operator->() const { return Address(); }
42
43 private:
44 std::variant<T, const T*> variant_ = T{};
45
46 const T* Address() const {
47 if (const T* t = std::get_if<T>(&variant_)) {
48 return t;
49 }
50 return std::get<const T*>(variant_);
51 }
52};
53
54/// @brief アダプター。可変オブジェクトを所有または参照します。
55template <typename T>
56class Ref {
57 public:
58 Ref() = default;
59
60 // 所有コンストラクタ:
61 Ref(T t)
62 : variant_(std::move(t)) {} // NOLINT
63 //
64 // Referencing constructors:
65 Ref(T* t)
66 : variant_(t) {} // NOLINT
67 //
68 ~Ref() = default;
69 Ref& operator=(Ref&&) noexcept = default;
70 Ref(const Ref<T>&) = default;
71 Ref(Ref<T>&&) noexcept = default;
72
73 // "再着席可能な"参照を作成します。
74 Ref<T>& operator=(const Ref<T>&) = default;
75
76 // アクセサ:
77 T& operator()() { return *Address(); }
78 T& operator*() { return *Address(); }
79 T* operator->() { return Address(); }
80 const T& operator()() const { return *Address(); }
81 const T& operator*() const { return *Address(); }
82 const T* operator->() const { return Address(); }
83
84 private:
85 std::variant<T, T*> variant_ = T{};
86
87 const T* Address() const {
88 if (const T* t = std::get_if<T>(&variant_)) {
89 return t;
90 }
91 return std::get<T*>(variant_);
92 }
93 T* Address() {
94 if (T* t = std::get_if<T>(&variant_)) {
95 return t;
96 }
97 return std::get<T*>(variant_);
98 }
99};
100
101/// @brief アダプター。定数文字列を所有または参照します。便宜上、このクラスは複数の可変文字列を共有表現に変換します。
102class FTXUI_EXPORT(SCREEN) StringRef : public Ref<std::string> {
103 public:
104 using Ref<std::string>::Ref;
105
106 // 所有コンストラクタ:
107 StringRef(const wchar_t* ref) // NOLINT
108 : StringRef(to_string(std::wstring(ref))) {}
109 StringRef(const char* ref) // NOLINT
110 : StringRef(std::string(ref)) {}
111 StringRef(std::string_view ref) // NOLINT
112 : StringRef(std::string(ref)) {}
113 StringRef(std::wstring_view ref) // NOLINT
114 : StringRef(to_string(ref)) {}
115};
116
117/// @brief アダプター。定数文字列を所有または参照します。便宜上、このクラスは複数の不変文字列を共有表現に変換します。
118class FTXUI_EXPORT(SCREEN) ConstStringRef : public ConstRef<std::string> {
119 public:
120 using ConstRef<std::string>::ConstRef;
121
122 // 参照コンストラクタ:
123 ConstStringRef(const std::wstring* ref) // NOLINT
124 : ConstStringRef(to_string(*ref)) {}
125
126 // 所有コンストラクタ:
127 ConstStringRef(const std::wstring ref) // NOLINT
128 : ConstStringRef(to_string(ref)) {}
129 ConstStringRef(std::wstring_view ref) // NOLINT
130 : ConstStringRef(to_string(ref)) {}
131 ConstStringRef(const wchar_t* ref) // NOLINT
132 : ConstStringRef(to_string(std::wstring(ref))) {}
133 ConstStringRef(const char* ref) // NOLINT
134 : ConstStringRef(std::string(ref)) {}
135 ConstStringRef(std::string_view ref) // NOLINT
136 : ConstStringRef(std::string(ref)) {}
137};
138
139/// @brief アダプター。文字列のリストを参照します。
140///
141/// サポートされている入力:
142/// - `std::vector<std::string>`
143/// - `std::vector<std::string>*`
144/// - `std::vector<std::wstring>*`
145/// - `Adapter*`
146/// - `std::unique_ptr<Adapter>`
147class FTXUI_EXPORT(SCREEN) ConstStringListRef {
148 public:
149 // 独自のアダプタを持ち込む:
150 class Adapter {
151 public:
152 Adapter() = default;
153 Adapter(const Adapter&) = default;
154 Adapter& operator=(const Adapter&) = default;
155 Adapter(Adapter&&) = default;
156 Adapter& operator=(Adapter&&) = default;
157 virtual ~Adapter() = default;
158 virtual size_t size() const = 0;
159 virtual std::string_view operator[](size_t i) const = 0;
160 };
161 using Variant = std::variant<const std::vector<std::string>, //
162 const std::vector<std::string>*, //
163 const std::vector<std::string_view>, //
164 const std::vector<std::string_view>*, //
165 const std::vector<std::wstring>*, //
166 Adapter*, //
167 std::unique_ptr<Adapter> //
168 >;
169
170 ConstStringListRef() = default;
171 ~ConstStringListRef() = default;
172 ConstStringListRef& operator=(const ConstStringListRef&) = default;
173 ConstStringListRef& operator=(ConstStringListRef&&) = default;
174 ConstStringListRef(ConstStringListRef&&) = default;
175 ConstStringListRef(const ConstStringListRef&) = default;
176
177 ConstStringListRef(std::vector<std::string> value) { // NOLINT
178 variant_ = std::make_shared<Variant>(value);
179 }
180 ConstStringListRef(const std::vector<std::string>* value) { // NOLINT
181 variant_ = std::make_shared<Variant>(value);
182 }
183 ConstStringListRef(std::vector<std::string_view> value) { // NOLINT
184 variant_ = std::make_shared<Variant>(value);
185 }
186 ConstStringListRef(const std::vector<std::string_view>* value) { // NOLINT
187 variant_ = std::make_shared<Variant>(value);
188 }
189 ConstStringListRef(const std::vector<std::wstring>* value) { // NOLINT
190 variant_ = std::make_shared<Variant>(value);
191 }
192 ConstStringListRef(Adapter* adapter) { // NOLINT
193 variant_ = std::make_shared<Variant>(adapter);
194 }
195 template <typename AdapterType>
196 ConstStringListRef(std::unique_ptr<AdapterType> adapter) { // NOLINT
197 variant_ = std::make_shared<Variant>(
198 static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
199 }
200
201 size_t size() const {
202 return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
203 }
204
205 std::string_view operator[](size_t i) const {
206 return variant_ ? std::visit(IndexedGetter{i}, *variant_) : "";
207 }
208
209 private:
210 struct IndexedGetter {
211 size_t i;
212 std::string_view operator()(const std::vector<std::string>& v) const {
213 return v[i];
214 }
215 std::string_view operator()(const std::vector<std::string>* v) const {
216 return (*v)[i];
217 }
218 std::string_view operator()(const std::vector<std::string_view>& v) const {
219 return v[i];
220 }
221 std::string_view operator()(const std::vector<std::string_view>* v) const {
222 return (*v)[i];
223 }
224 std::string_view operator()(
225 [[maybe_unused]] const std::vector<std::wstring>* v) const {
226 return ""; // 一時的な修正: 一時的な変換へのビューを返すことはできません。
227 }
228 std::string_view operator()(Adapter* v) const { return (*v)[i]; }
229 std::string_view operator()(const std::unique_ptr<Adapter>& v) const {
230 return (*v)[i];
231 }
232 };
233
234 struct SizeVisitor {
235 size_t operator()(const std::vector<std::string>& v) const {
236 return v.size();
237 }
238 size_t operator()(const std::vector<std::string>* v) const {
239 return v->size();
240 }
241 size_t operator()(const std::vector<std::string_view>& v) const {
242 return v.size();
243 }
244 size_t operator()(const std::vector<std::string_view>* v) const {
245 return v->size();
246 }
247 size_t operator()(const std::vector<std::wstring>* v) const {
248 return v->size();
249 }
250 size_t operator()(const Adapter* v) const { return v->size(); }
251 size_t operator()(const std::unique_ptr<Adapter>& v) const {
252 return v->size();
253 }
254 };
255
256 std::shared_ptr<Variant> variant_;
257};
258
259} // namespace ftxui
260
261#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
アダプター。不変オブジェクトを所有または参照します。
Definition ref.hpp:20
const T & operator*() const
Definition ref.hpp:40
ConstRef()=default
ConstRef(const T *t)
Definition ref.hpp:28
ConstRef(T t)
Definition ref.hpp:25
const T * operator->() const
Definition ref.hpp:41
ConstRef & operator=(ConstRef &&) noexcept=default
アダプター。可変オブジェクトを所有または参照します。
Definition ref.hpp:56
const T & operator*() const
Definition ref.hpp:81
~Ref()=default
T & operator*()
Definition ref.hpp:78
T * operator->()
Definition ref.hpp:79
Ref()=default
Ref(T t)
Definition ref.hpp:61
const T * operator->() const
Definition ref.hpp:82
Ref & operator=(Ref &&) noexcept=default
Ref(T *t)
Definition ref.hpp:65
const T & operator()() const
Definition ref.hpp:80
FTXUI ftxui::名前空間
Definition animation.hpp:11