FTXUI  3.0.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 { return address_ ? *address_ : owned_; }
70 const std::string* operator->() const {
71 return address_ ? address_ : &owned_;
72 }
73
74 private:
75 const std::string owned_;
76 const std::string* address_ = nullptr;
77};
78
79/// @brief An adapter. Reference a list of strings.
81 public:
82 ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
83 ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
84
85 size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
86 std::string operator[](size_t i) const {
87 return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
88 }
89
90 private:
91 const std::vector<std::string>* ref_ = nullptr;
92 const std::vector<std::wstring>* ref_wide_ = nullptr;
93};
94
95} // namespace ftxui
96
97#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
98
99// Copyright 2020 Arthur Sonzogni. All rights reserved.
100// Use of this source code is governed by the MIT license that can be found in
101// 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:80
size_t size() const
Definition ref.hpp:85
ConstStringListRef(const std::vector< std::wstring > *ref)
Definition ref.hpp:83
ConstStringListRef(const std::vector< std::string > *ref)
Definition ref.hpp:82
std::string operator[](size_t i) const
Definition ref.hpp:86
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:69
const std::string * operator->() const
Definition ref.hpp:70
ConstStringRef(std::wstring ref)
Definition ref.hpp:65
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:443
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:437