FTXUI  0.9.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
ref.hpp
Go to the documentation of this file.
1#ifndef FTXUI_UTIL_REF_HPP
2#define FTXUI_UTIL_REF_HPP
3
5#include <string>
6
7namespace ftxui {
8
9/// @brief An adapter. Own or reference an immutable object.
10template <typename T>
11class ConstRef {
12 public:
14 ConstRef(T t) : owned_(t) {}
15 ConstRef(const T* t) : address_(t) {}
16 const T& operator*() { return address_ ? *address_ : owned_; }
17 const T& operator()() { return address_ ? *address_ : owned_; }
18 const T* operator->() { return address_ ? address_ : &owned_; }
19
20 private:
21 T owned_;
22 const T* address_ = nullptr;
23};
24
25/// @brief An adapter. Own or reference an mutable object.
26template <typename T>
27class Ref {
28 public:
29 Ref() {}
30 Ref(T t) : owned_(t) {}
31 Ref(T* t) : address_(t) {}
32 T& operator*() { return address_ ? *address_ : owned_; }
33 T& operator()() { return address_ ? *address_ : owned_; }
34 T* operator->() { return address_ ? address_ : &owned_; }
35
36 private:
37 T owned_;
38 T* address_ = nullptr;
39};
40
41/// @brief An adapter. Own or reference a constant string. For convenience, this
42/// class convert multiple mutable string toward a shared representation.
43class StringRef {
44 public:
45 StringRef(std::string* ref) : address_(ref) {}
46 StringRef(std::string ref) : owned_(std::move(ref)) {}
47 StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
48 StringRef(const char* ref) : StringRef(std::string(ref)) {}
49 std::string& operator*() { return address_ ? *address_ : owned_; }
50 std::string* operator->() { return address_ ? address_ : &owned_; }
51
52 private:
53 std::string owned_;
54 std::string* address_ = nullptr;
55};
56
57/// @brief An adapter. Own or reference a constant string. For convenience, this
58/// class convert multiple mutable string toward a shared representation.
60 public:
61 WideStringRef(std::wstring* ref) : address_(ref) {}
62 WideStringRef(std::wstring ref) : owned_(std::move(ref)) {}
63 WideStringRef(const wchar_t* ref) : WideStringRef(std::wstring(ref)) {}
64 WideStringRef(const char* ref)
65 : WideStringRef(to_wstring(std::string(ref))) {}
66 std::wstring& operator*() { return address_ ? *address_ : owned_; }
67 std::wstring* operator->() { return address_ ? address_ : &owned_; }
68
69 private:
70 std::wstring owned_;
71 std::wstring* address_ = nullptr;
72};
73
74/// @brief An adapter. Own or reference a constant string. For convenience, this
75/// class convert multiple immutable string toward a shared representation.
77 public:
78 ConstStringRef(const std::string* ref) : address_(ref) {}
79 ConstStringRef(const std::wstring* ref) : ConstStringRef(to_string(*ref)) {}
80 ConstStringRef(std::string ref) : owned_(std::move(ref)) {}
81 ConstStringRef(std::wstring ref) : ConstStringRef(to_string(ref)) {}
82 ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
83 ConstStringRef(const char* ref)
84 : ConstStringRef(to_wstring(std::string(ref))) {}
85 const std::string& operator*() { return address_ ? *address_ : owned_; }
86 const std::string* operator->() { return address_ ? address_ : &owned_; }
87
88 private:
89 const std::string owned_;
90 const std::string* address_ = nullptr;
91};
92
93/// @brief An adapter. Reference a list of strings.
95 public:
96 ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
97 ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
98
99 size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
100 std::string operator[](size_t i) const {
101 return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
102 }
103
104 private:
105 const std::vector<std::string>* ref_ = nullptr;
106 const std::vector<std::wstring>* ref_wide_ = nullptr;
107};
108
109} // namespace ftxui
110
111#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
112
113// Copyright 2020 Arthur Sonzogni. All rights reserved.
114// Use of this source code is governed by the MIT license that can be found in
115// the LICENSE file.
An adapter. Own or reference an immutable object.
Definition ref.hpp:11
const T & operator*()
Definition ref.hpp:16
const T & operator()()
Definition ref.hpp:17
const T * operator->()
Definition ref.hpp:18
ConstRef(const T *t)
Definition ref.hpp:15
ConstRef(T t)
Definition ref.hpp:14
An adapter. Reference a list of strings.
Definition ref.hpp:94
size_t size() const
Definition ref.hpp:99
ConstStringListRef(const std::vector< std::wstring > *ref)
Definition ref.hpp:97
ConstStringListRef(const std::vector< std::string > *ref)
Definition ref.hpp:96
std::string operator[](size_t i) const
Definition ref.hpp:100
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:76
ConstStringRef(const char *ref)
Definition ref.hpp:83
ConstStringRef(const std::string *ref)
Definition ref.hpp:78
ConstStringRef(std::wstring ref)
Definition ref.hpp:81
ConstStringRef(const std::wstring *ref)
Definition ref.hpp:79
const std::string * operator->()
Definition ref.hpp:86
const std::string & operator*()
Definition ref.hpp:85
ConstStringRef(std::string ref)
Definition ref.hpp:80
ConstStringRef(const wchar_t *ref)
Definition ref.hpp:82
An adapter. Own or reference an mutable object.
Definition ref.hpp:27
T & operator*()
Definition ref.hpp:32
T * operator->()
Definition ref.hpp:34
T & operator()()
Definition ref.hpp:33
Ref(T t)
Definition ref.hpp:30
Ref(T *t)
Definition ref.hpp:31
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:43
std::string * operator->()
Definition ref.hpp:50
StringRef(const char *ref)
Definition ref.hpp:48
StringRef(const wchar_t *ref)
Definition ref.hpp:47
StringRef(std::string ref)
Definition ref.hpp:46
std::string & operator*()
Definition ref.hpp:49
StringRef(std::string *ref)
Definition ref.hpp:45
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:59
WideStringRef(const char *ref)
Definition ref.hpp:64
WideStringRef(std::wstring *ref)
Definition ref.hpp:61
WideStringRef(std::wstring ref)
Definition ref.hpp:62
std::wstring * operator->()
Definition ref.hpp:67
std::wstring & operator*()
Definition ref.hpp:66
WideStringRef(const wchar_t *ref)
Definition ref.hpp:63
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:303
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:297