FTXUI  5.0.0
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 <variant>
11#include <vector>
12
13namespace ftxui {
14
15/// @brief An adapter. Own or reference an immutable object.
16template <typename T>
17class ConstRef {
18 public:
19 ConstRef() = default;
20 ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
21 ConstRef(const T* t) : variant_(t) {} // NOLINT
26
27 // Make a "reseatable" reference
29
30 // Accessors:
31 const T& operator()() const { return *Address(); }
32 const T& operator*() const { return *Address(); }
33 const T* operator->() const { return Address(); }
34
35 private:
36 std::variant<T, const T*> variant_ = T{};
37
38 const T* Address() const {
39 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
40 : std::get<const T*>(variant_);
41 }
42};
43
44/// @brief An adapter. Own or reference an mutable object.
45template <typename T>
46class Ref {
47 public:
48 Ref() = default;
49 Ref(T t) : variant_(std::move(t)) {} // NOLINT
50 Ref(T* t) : variant_(t) {} // NOLINT
51 ~Ref() = default;
55
56 // Make a "reseatable" reference.
58
59 // Accessors:
60 T& operator()() { return *Address(); }
61 T& operator*() { return *Address(); }
62 T* operator->() { return Address(); }
63 const T& operator()() const { return *Address(); }
64 const T& operator*() const { return *Address(); }
65 const T* operator->() const { return Address(); }
66
67 private:
68 std::variant<T, T*> variant_ = T{};
69
70 const T* Address() const {
71 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
72 : std::get<T*>(variant_);
73 }
74 T* Address() {
75 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
76 : std::get<T*>(variant_);
77 }
78};
79
80/// @brief An adapter. Own or reference a constant string. For convenience, this
81/// class convert multiple mutable string toward a shared representation.
82class StringRef : public Ref<std::string> {
83 public:
84 using Ref<std::string>::Ref;
85
86 StringRef(const wchar_t* ref) // NOLINT
88 StringRef(const char* ref) // NOLINT
89 : StringRef(std::string(ref)) {}
90};
91
92/// @brief An adapter. Own or reference a constant string. For convenience, this
93/// class convert multiple immutable string toward a shared representation.
94class ConstStringRef : public ConstRef<std::string> {
95 public:
96 using ConstRef<std::string>::ConstRef;
97
98 ConstStringRef(const std::wstring* ref) // NOLINT
100 ConstStringRef(const std::wstring ref) // NOLINT
102 ConstStringRef(const wchar_t* ref) // NOLINT
104 ConstStringRef(const char* ref) // NOLINT
106};
107
108/// @brief An adapter. Reference a list of strings.
109///
110/// Supported input:
111/// - `std::vector<std::string>`
112/// - `std::vector<std::string>*`
113/// - `std::vector<std::wstring>*`
114/// - `Adapter*`
115/// - `std::unique_ptr<Adapter>`
117 public:
118 // Bring your own adapter:
119 class Adapter {
120 public:
121 Adapter() = default;
122 Adapter(const Adapter&) = default;
123 Adapter& operator=(const Adapter&) = default;
124 Adapter(Adapter&&) = default;
125 Adapter& operator=(Adapter&&) = default;
126 virtual ~Adapter() = default;
127 virtual size_t size() const = 0;
128 virtual std::string operator[](size_t i) const = 0;
129 };
130 using Variant = std::variant<const std::vector<std::string>, //
131 const std::vector<std::string>*, //
132 const std::vector<std::wstring>*, //
133 Adapter*, //
134 std::unique_ptr<Adapter> //
135 >;
136
143
144 ConstStringListRef(std::vector<std::string> value) // NOLINT
145 {
146 variant_ = std::make_shared<Variant>(value);
147 }
148 ConstStringListRef(const std::vector<std::string>* value) // NOLINT
149 {
150 variant_ = std::make_shared<Variant>(value);
151 }
152 ConstStringListRef(const std::vector<std::wstring>* value) // NOLINT
153 {
154 variant_ = std::make_shared<Variant>(value);
155 }
157 {
158 variant_ = std::make_shared<Variant>(adapter);
159 }
160 template <typename AdapterType>
161 ConstStringListRef(std::unique_ptr<AdapterType> adapter) // NOLINT
162 {
163 variant_ = std::make_shared<Variant>(
164 static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
165 }
166
167 size_t size() const {
168 return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
169 }
170
171 std::string operator[](size_t i) const {
172 return variant_ ? std::visit(IndexedGetter(i), *variant_) : "";
173 }
174
175 private:
176 struct SizeVisitor {
177 size_t operator()(const std::vector<std::string>& v) const {
178 return v.size();
179 }
180 size_t operator()(const std::vector<std::string>* v) const {
181 return v->size();
182 }
183 size_t operator()(const std::vector<std::wstring>* v) const {
184 return v->size();
185 }
186 size_t operator()(const Adapter* v) const { return v->size(); }
187 size_t operator()(const std::unique_ptr<Adapter>& v) const {
188 return v->size();
189 }
190 };
191
192 struct IndexedGetter {
193 IndexedGetter(size_t index) // NOLINT
194 : index_(index) {}
195 size_t index_;
196 std::string operator()(const std::vector<std::string>& v) const {
197 return v[index_];
198 }
199 std::string operator()(const std::vector<std::string>* v) const {
200 return (*v)[index_];
201 }
202 std::string operator()(const std::vector<std::wstring>* v) const {
203 return to_string((*v)[index_]);
204 }
205 std::string operator()(const Adapter* v) const { return (*v)[index_]; }
206 std::string operator()(const std::unique_ptr<Adapter>& v) const {
207 return (*v)[index_];
208 }
209 };
210
211 std::shared_ptr<Variant> variant_;
212};
213
214} // namespace ftxui
215
216#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
const T & operator*() const
Definition ref.hpp:32
ConstRef()=default
ConstRef(const T *t)
Definition ref.hpp:21
ConstRef(T t)
Definition ref.hpp:20
const T * operator->() const
Definition ref.hpp:33
ConstRef & operator=(ConstRef &&) noexcept=default
Adapter & operator=(Adapter &&)=default
Adapter & operator=(const Adapter &)=default
virtual size_t size() const =0
virtual std::string operator[](size_t i) const =0
Adapter(const Adapter &)=default
An adapter. Reference a list of strings.
Definition ref.hpp:116
ConstStringListRef(std::vector< std::string > value)
Definition ref.hpp:144
ConstStringListRef(const std::vector< std::string > *value)
Definition ref.hpp:148
size_t size() const
Definition ref.hpp:167
ConstStringListRef(ConstStringListRef &&)=default
ConstStringListRef & operator=(ConstStringListRef &&)=default
ConstStringListRef & operator=(const ConstStringListRef &)=default
std::variant< const std::vector< std::string >, const std::vector< std::string > *, const std::vector< std::wstring > *, Adapter *, std::unique_ptr< Adapter > > Variant
Definition ref.hpp:135
ConstStringListRef(std::unique_ptr< AdapterType > adapter)
Definition ref.hpp:161
ConstStringListRef(const std::vector< std::wstring > *value)
Definition ref.hpp:152
ConstStringListRef(const ConstStringListRef &)=default
ConstStringListRef(Adapter *adapter)
Definition ref.hpp:156
std::string operator[](size_t i) const
Definition ref.hpp:171
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ConstStringRef(const char *ref)
Definition ref.hpp:104
ConstStringRef(const std::wstring ref)
Definition ref.hpp:100
ConstStringRef(const std::wstring *ref)
Definition ref.hpp:98
ConstStringRef(const wchar_t *ref)
Definition ref.hpp:102
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
const T & operator*() const
Definition ref.hpp:64
~Ref()=default
T & operator*()
Definition ref.hpp:61
T * operator->()
Definition ref.hpp:62
Ref()=default
Ref(T t)
Definition ref.hpp:49
const T * operator->() const
Definition ref.hpp:65
Ref & operator=(Ref &&) noexcept=default
Ref(T *t)
Definition ref.hpp:50
const T & operator()() const
Definition ref.hpp:63
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
StringRef(const char *ref)
Definition ref.hpp:88
StringRef(const wchar_t *ref)
Definition ref.hpp:86
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1565