FTXUI 6.1.9 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
7#include <ftxui/screen/string.hpp>
8#include <memory>
9#include <string>
10#include <variant>
11#include <vector>
12
13 namespace ftxui {
14
15/// @brief An adapter. Own or reference an immutable object.
16 template <typename T>
17 class ConstRef {
18 public:
19 ConstRef() = default;
20 ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
21 ConstRef(const T* t) : variant_(t) {} // NOLINT
22 ConstRef& operator=(ConstRef&&) noexcept = default;
23 ConstRef(const ConstRef<T>&) = default;
24 ConstRef(ConstRef<T>&&) noexcept = default;
25 ~ConstRef() = default;
26
27 // Make a "reseatable" reference
28 ConstRef<T>& operator=(const ConstRef<T>&) = default;
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.
45 template <typename T>
46 class 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;
52 Ref& operator=(Ref&&) noexcept = default;
53 Ref(const Ref<T>&) = default;
54 Ref(Ref<T>&&) noexcept = default;
55
56 // Make a "reseatable" reference.
57 Ref<T>& operator=(const Ref<T>&) = default;
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.
82 class StringRef : public Ref<std::string> {
83 public:
84 using Ref<std::string>::Ref;
85
86 StringRef(const wchar_t* ref) // NOLINT
87 : StringRef(to_string(std::wstring(ref))) {}
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.
94 class ConstStringRef : public ConstRef<std::string> {
95 public:
96 using ConstRef<std::string>::ConstRef;
97
98 ConstStringRef(const std::wstring* ref) // NOLINT
99 : ConstStringRef(to_string(*ref)) {}
100 ConstStringRef(const std::wstring ref) // NOLINT
101 : ConstStringRef(to_string(ref)) {}
102 ConstStringRef(const wchar_t* ref) // NOLINT
103 : ConstStringRef(to_string(std::wstring(ref))) {}
104 ConstStringRef(const char* ref) // NOLINT
105 : ConstStringRef(std::string(ref)) {}
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>`
116 class ConstStringListRef {
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
137 ConstStringListRef() = default;
138 ~ConstStringListRef() = default;
139 ConstStringListRef& operator=(const ConstStringListRef&) = default;
140 ConstStringListRef& operator=(ConstStringListRef&&) = default;
141 ConstStringListRef(ConstStringListRef&&) = default;
142 ConstStringListRef(const ConstStringListRef&) = default;
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 }
156 ConstStringListRef(Adapter* adapter) // NOLINT
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 */
ftxui::ConstRef
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
ftxui::ConstRef::operator*
const T & operator*() const
Definition ref.hpp:32
ftxui::ConstRef::ConstRef
ConstRef()=default
ftxui::ConstRef::ConstRef
ConstRef(const T *t)
Definition ref.hpp:21
ftxui::ConstRef::ConstRef
ConstRef(T t)
Definition ref.hpp:20
ftxui::ConstRef::operator->
const T * operator->() const
Definition ref.hpp:33
ftxui::ConstRef::operator=
ConstRef & operator=(ConstRef &&) noexcept=default
ftxui::ConstStringListRef::Adapter
Definition ref.hpp:119
ftxui::ConstStringListRef::Adapter::operator=
Adapter & operator=(Adapter &&)=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter(Adapter &&)=default
ftxui::ConstStringListRef::Adapter::operator=
Adapter & operator=(const Adapter &)=default
ftxui::ConstStringListRef::Adapter::size
virtual size_t size() const =0
ftxui::ConstStringListRef::Adapter::operator[]
virtual std::string operator[](size_t i) const =0
ftxui::ConstStringListRef::Adapter::~Adapter
virtual ~Adapter()=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter()=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter(const Adapter &)=default
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition ref.hpp:116
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(std::vector< std::string > value)
Definition ref.hpp:144
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::string > *value)
Definition ref.hpp:148
ftxui::ConstStringListRef::size
size_t size() const
Definition ref.hpp:167
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef()=default
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(ConstStringListRef &&)=default
ftxui::ConstStringListRef::operator=
ConstStringListRef & operator=(ConstStringListRef &&)=default
ftxui::ConstStringListRef::~ConstStringListRef
~ConstStringListRef()=default
ftxui::ConstStringListRef::Variant
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:130
ftxui::ConstStringListRef::operator=
ConstStringListRef & operator=(const ConstStringListRef &)=default
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(std::unique_ptr< AdapterType > adapter)
Definition ref.hpp:161
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::wstring > *value)
Definition ref.hpp:152
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const ConstStringListRef &)=default
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(Adapter *adapter)
Definition ref.hpp:156
ftxui::ConstStringListRef::operator[]
std::string operator[](size_t i) const
Definition ref.hpp:171
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const char *ref)
Definition ref.hpp:104
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::wstring ref)
Definition ref.hpp:100
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::wstring *ref)
Definition ref.hpp:98
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const wchar_t *ref)
Definition ref.hpp:102
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
ftxui::Ref::operator*
const T & operator*() const
Definition ref.hpp:64
ftxui::Ref::~Ref
~Ref()=default
ftxui::Ref::operator*
T & operator*()
Definition ref.hpp:61
ftxui::Ref::operator->
T * operator->()
Definition ref.hpp:62
ftxui::Ref::Ref
Ref()=default
ftxui::Ref::Ref
Ref(T t)
Definition ref.hpp:49
ftxui::Ref::operator->
const T * operator->() const
Definition ref.hpp:65
ftxui::Ref::operator=
Ref & operator=(Ref &&) noexcept=default
ftxui::Ref::Ref
Ref(T *t)
Definition ref.hpp:50
ftxui::Ref::operator()
const T & operator()() const
Definition ref.hpp:63
ftxui::StringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
ftxui::StringRef::StringRef
StringRef(const char *ref)
Definition ref.hpp:88
ftxui::StringRef::StringRef
StringRef(const wchar_t *ref)
Definition ref.hpp:86
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::to_string
std::string to_string(const std::wstring &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1565
string.hpp