FTXUI  4.1.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*() const { return address_ ? *address_ : owned_; }
17 const T& operator()() const { return address_ ? *address_ : owned_; }
18 const T* operator->() const { 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(const T& t) : owned_(t) {}
31 Ref(T&& t) : owned_(std::forward<T>(t)) {}
32 Ref(T* t) : address_(t) {}
33 T& operator*() { return address_ ? *address_ : owned_; }
34 T& operator()() { return address_ ? *address_ : owned_; }
35 T* operator->() { return address_ ? address_ : &owned_; }
36
37 private:
38 T owned_;
39 T* address_ = nullptr;
40};
41
42/// @brief An adapter. Own or reference a constant string. For convenience, this
43/// class convert multiple mutable string toward a shared representation.
44class StringRef {
45 public:
46 StringRef(std::string* ref) : address_(ref) {}
47 StringRef(std::string ref) : owned_(std::move(ref)) {}
48 StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
49 StringRef(const char* ref) : StringRef(std::string(ref)) {}
50 std::string& operator*() { return address_ ? *address_ : owned_; }
51 std::string* operator->() { return address_ ? address_ : &owned_; }
52
53 private:
54 std::string owned_;
55 std::string* address_ = nullptr;
56};
57
58/// @brief An adapter. Own or reference a constant string. For convenience, this
59/// class convert multiple immutable string toward a shared representation.
61 public:
62 ConstStringRef(const std::string* ref) : address_(ref) {}
63 ConstStringRef(const std::wstring* ref) : ConstStringRef(to_string(*ref)) {}
64 ConstStringRef(std::string ref) : owned_(std::move(ref)) {}
65 ConstStringRef(std::wstring ref) : ConstStringRef(to_string(ref)) {}
66 ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
67 ConstStringRef(const char* ref)
68 : ConstStringRef(to_wstring(std::string(ref))) {}
69 const std::string& operator()() const {
70 return address_ ? *address_ : owned_;
71 }
72 const std::string& operator*() const { return address_ ? *address_ : owned_; }
73 const std::string* operator->() const {
74 return address_ ? address_ : &owned_;
75 }
76
77 private:
78 const std::string owned_;
79 const std::string* address_ = nullptr;
80};
81
82/// @brief An adapter. Reference a list of strings.
84 public:
85 ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
86 ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
87
88 size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
89 std::string operator[](size_t i) const {
90 return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
91 }
92
93 private:
94 const std::vector<std::string>* ref_ = nullptr;
95 const std::vector<std::wstring>* ref_wide_ = nullptr;
96};
97
98} // namespace ftxui
99
100#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
101
102// Copyright 2020 Arthur Sonzogni. All rights reserved.
103// Use of this source code is governed by the MIT license that can be found in
104// the LICENSE file.
An adapter. Own or reference an immutable object.
Definition ref.hpp:11
const T & operator*() const
Definition ref.hpp:16
ConstRef(const T *t)
Definition ref.hpp:15
ConstRef(T t)
Definition ref.hpp:14
const T * operator->() const
Definition ref.hpp:18
const T & operator()() const
Definition ref.hpp:17
An adapter. Reference a list of strings.
Definition ref.hpp:83
size_t size() const
Definition ref.hpp:88
ConstStringListRef(const std::vector< std::wstring > *ref)
Definition ref.hpp:86
ConstStringListRef(const std::vector< std::string > *ref)
Definition ref.hpp:85
std::string operator[](size_t i) const
Definition ref.hpp:89
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:60
ConstStringRef(const char *ref)
Definition ref.hpp:67
ConstStringRef(const std::string *ref)
Definition ref.hpp:62
const std::string & operator*() const
Definition ref.hpp:72
const std::string * operator->() const
Definition ref.hpp:73
ConstStringRef(std::wstring ref)
Definition ref.hpp:65
const std::string & operator()() const
Definition ref.hpp:69
ConstStringRef(const std::wstring *ref)
Definition ref.hpp:63
ConstStringRef(std::string ref)
Definition ref.hpp:64
ConstStringRef(const wchar_t *ref)
Definition ref.hpp:66
An adapter. Own or reference an mutable object.
Definition ref.hpp:27
Ref(const T &t)
Definition ref.hpp:30
T & operator*()
Definition ref.hpp:33
T * operator->()
Definition ref.hpp:35
Ref(T &&t)
Definition ref.hpp:31
T & operator()()
Definition ref.hpp:34
Ref(T *t)
Definition ref.hpp:32
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:44
std::string * operator->()
Definition ref.hpp:51
StringRef(const char *ref)
Definition ref.hpp:49
StringRef(const wchar_t *ref)
Definition ref.hpp:48
StringRef(std::string ref)
Definition ref.hpp:47
std::string & operator*()
Definition ref.hpp:50
StringRef(std::string *ref)
Definition ref.hpp:46
std::wstring to_wstring(const std::string &s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1971
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1899