FTXUI  5.0.0
C++ functional terminal UI.
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 <string>
9#include <variant>
10
11namespace ftxui {
12
13/// @brief An adapter. Own or reference an immutable object.
14template <typename T>
15class ConstRef {
16 public:
17 ConstRef() = default;
18 ConstRef(const ConstRef<T>&) = default;
19 ConstRef(ConstRef<T>&&) = default;
20 ConstRef(T t) : variant_(std::move(t)) {}
21 ConstRef(const T* t) : variant_(t) {}
22
23 // Make a "reseatable" reference
24 ConstRef<T>& operator=(const ConstRef<T>&) = default;
25
26 // Accessors:
27 const T& operator()() const { return *Address(); }
28 const T& operator*() const { return *Address(); }
29 const T* operator->() const { return Address(); }
30
31 private:
32 std::variant<T, const T*> variant_ = T{};
33
34 const T* Address() const {
35 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
36 : std::get<const T*>(variant_);
37 }
38};
39
40/// @brief An adapter. Own or reference an mutable object.
41template <typename T>
42class Ref {
43 public:
44 Ref() = default;
45 Ref(const Ref<T>&) = default;
46 Ref(Ref<T>&&) = default;
47 Ref(T t) : variant_(std::move(t)) {}
48 Ref(T* t) : variant_(t) {}
49
50 // Make a "reseatable" reference.
51 Ref<T>& operator=(const Ref<T>&) = default;
52
53 // Accessors:
54 T& operator()() { return *Address(); }
55 T& operator*() { return *Address(); }
56 T* operator->() { return Address(); }
57 const T& operator()() const { return *Address(); }
58 const T& operator*() const { return *Address(); }
59 const T* operator->() const { return Address(); }
60
61 private:
62 std::variant<T, T*> variant_ = T{};
63
64 const T* Address() const {
65 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
66 : std::get<T*>(variant_);
67 }
68 T* Address() {
69 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
70 : std::get<T*>(variant_);
71 }
72};
73
74/// @brief An adapter. Own or reference a constant string. For convenience, this
75/// class convert multiple mutable string toward a shared representation.
76class StringRef : public Ref<std::string> {
77 public:
78 using Ref<std::string>::Ref;
79
80 StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
81 StringRef(const char* ref) : StringRef(std::string(ref)) {}
82};
83
84/// @brief An adapter. Own or reference a constant string. For convenience, this
85/// class convert multiple immutable string toward a shared representation.
86class ConstStringRef : public ConstRef<std::string> {
87 public:
88 using ConstRef<std::string>::ConstRef;
89
90 ConstStringRef(const std::wstring* ref) : ConstStringRef(to_string(*ref)) {}
91 ConstStringRef(const std::wstring ref) : ConstStringRef(to_string(ref)) {}
92 ConstStringRef(const wchar_t* ref)
93 : ConstStringRef(to_string(std::wstring(ref))) {}
94 ConstStringRef(const char* ref) : ConstStringRef(std::string(ref)) {}
95};
96
97/// @brief An adapter. Reference a list of strings.
99 public:
101 ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
102 ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
103 ConstStringListRef(const ConstStringListRef& other) = default;
105
106 size_t size() const {
107 if (ref_) {
108 return ref_->size();
109 }
110 if (ref_wide_) {
111 return ref_wide_->size();
112 }
113 return 0;
114 }
115
116 std::string operator[](size_t i) const {
117 if (ref_) {
118 return (*ref_)[i];
119 }
120 if (ref_wide_) {
121 return to_string((*ref_wide_)[i]);
122 }
123 return "";
124 }
125
126 private:
127 const std::vector<std::string>* ref_ = nullptr;
128 const std::vector<std::wstring>* ref_wide_ = nullptr;
129};
130
131} // namespace ftxui
132
133#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
An adapter. Own or reference an immutable object.
Definition: ref.hpp:15
ConstRef(const ConstRef< T > &)=default
const T & operator*() const
Definition: ref.hpp:28
ConstRef(ConstRef< T > &&)=default
ConstRef()=default
ConstRef(const T *t)
Definition: ref.hpp:21
ConstRef< T > & operator=(const ConstRef< T > &)=default
ConstRef(T t)
Definition: ref.hpp:20
const T * operator->() const
Definition: ref.hpp:29
const T & operator()() const
Definition: ref.hpp:27
An adapter. Reference a list of strings.
Definition: ref.hpp:98
size_t size() const
Definition: ref.hpp:106
ConstStringListRef(const std::vector< std::wstring > *ref)
Definition: ref.hpp:102
ConstStringListRef(const std::vector< std::string > *ref)
Definition: ref.hpp:101
ConstStringListRef & operator=(const ConstStringListRef &other)=default
ConstStringListRef(const ConstStringListRef &other)=default
std::string operator[](size_t i) const
Definition: ref.hpp:116
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:86
ConstStringRef(const char *ref)
Definition: ref.hpp:94
ConstStringRef(const std::wstring ref)
Definition: ref.hpp:91
ConstStringRef(const std::wstring *ref)
Definition: ref.hpp:90
ConstStringRef(const wchar_t *ref)
Definition: ref.hpp:92
An adapter. Own or reference an mutable object.
Definition: ref.hpp:42
Ref< T > & operator=(const Ref< T > &)=default
const T & operator*() const
Definition: ref.hpp:58
T & operator*()
Definition: ref.hpp:55
T * operator->()
Definition: ref.hpp:56
Ref()=default
T & operator()()
Definition: ref.hpp:54
Ref(T t)
Definition: ref.hpp:47
const T * operator->() const
Definition: ref.hpp:59
Ref(Ref< T > &&)=default
Ref(T *t)
Definition: ref.hpp:48
Ref(const Ref< T > &)=default
const T & operator()() const
Definition: ref.hpp:57
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition: ref.hpp:76
StringRef(const char *ref)
Definition: ref.hpp:81
StringRef(const wchar_t *ref)
Definition: ref.hpp:80
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:1565