FTXUI  0.8.1
C++ functional terminal UI.
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 
7 namespace ftxui {
8 
9 /// @brief An adapter. Own or reference an immutable object.
10 template <typename T>
11 class ConstRef {
12  public:
13  ConstRef() {}
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.
26 template <typename T>
27 class 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.
43 class 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.
ftxui::StringRef::operator*
std::string & operator*()
Definition: ref.hpp:49
ftxui::Ref::operator*
T & operator*()
Definition: ref.hpp:32
ftxui::ConstRef::operator->
const T * operator->()
Definition: ref.hpp:18
ftxui::ConstRef
An adapter. Own or reference an immutable object.
Definition: ref.hpp:11
ftxui::ConstStringListRef::operator[]
std::string operator[](size_t i) const
Definition: ref.hpp:100
ftxui::ConstRef::operator*
const T & operator*()
Definition: ref.hpp:16
ftxui::WideStringRef::WideStringRef
WideStringRef(std::wstring *ref)
Definition: ref.hpp:61
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(std::string ref)
Definition: ref.hpp:80
ftxui
Definition: captured_mouse.hpp:6
ftxui::Ref::Ref
Ref()
Definition: ref.hpp:29
ftxui::to_string
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:297
ftxui::ConstStringRef::operator->
const std::string * operator->()
Definition: ref.hpp:86
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::string *ref)
Definition: ref.hpp:78
ftxui::to_wstring
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition: string.cpp:303
ftxui::ConstRef::operator()
const T & operator()()
Definition: ref.hpp:17
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const char *ref)
Definition: ref.hpp:83
ftxui::WideStringRef::WideStringRef
WideStringRef(std::wstring ref)
Definition: ref.hpp:62
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::wstring > *ref)
Definition: ref.hpp:97
ftxui::StringRef::StringRef
StringRef(std::string *ref)
Definition: ref.hpp:45
ftxui::WideStringRef::operator*
std::wstring & operator*()
Definition: ref.hpp:66
string.hpp
ftxui::ConstRef::ConstRef
ConstRef(T t)
Definition: ref.hpp:14
ftxui::StringRef::operator->
std::string * operator->()
Definition: ref.hpp:50
ftxui::Ref::Ref
Ref(T *t)
Definition: ref.hpp:31
ftxui::StringRef::StringRef
StringRef(const wchar_t *ref)
Definition: ref.hpp:47
ftxui::StringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition: ref.hpp:43
ftxui::Ref::Ref
Ref(T t)
Definition: ref.hpp:30
ftxui::Ref::operator->
T * operator->()
Definition: ref.hpp:34
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::string > *ref)
Definition: ref.hpp:96
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(std::wstring ref)
Definition: ref.hpp:81
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition: ref.hpp:27
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition: ref.hpp:94
ftxui::StringRef::StringRef
StringRef(const char *ref)
Definition: ref.hpp:48
ftxui::WideStringRef::operator->
std::wstring * operator->()
Definition: ref.hpp:67
ftxui::ConstRef::ConstRef
ConstRef()
Definition: ref.hpp:13
ftxui::WideStringRef::WideStringRef
WideStringRef(const char *ref)
Definition: ref.hpp:64
ftxui::WideStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition: ref.hpp:59
ftxui::ConstStringListRef::size
size_t size() const
Definition: ref.hpp:99
ftxui::ConstRef::ConstRef
ConstRef(const T *t)
Definition: ref.hpp:15
ftxui::Ref::operator()
T & operator()()
Definition: ref.hpp:33
ftxui::StringRef::StringRef
StringRef(std::string ref)
Definition: ref.hpp:46
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:76
ftxui::ConstStringRef::operator*
const std::string & operator*()
Definition: ref.hpp:85
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::wstring *ref)
Definition: ref.hpp:79
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const wchar_t *ref)
Definition: ref.hpp:82
ftxui::WideStringRef::WideStringRef
WideStringRef(const wchar_t *ref)
Definition: ref.hpp:63