2021-07-08 04:13:33 +08:00
|
|
|
#ifndef FTXUI_UTIL_REF_HPP
|
|
|
|
#define FTXUI_UTIL_REF_HPP
|
|
|
|
|
|
|
|
#include <ftxui/screen/string.hpp>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace ftxui {
|
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
/// @brief An adapter. Own or reference an immutable object.
|
2021-07-08 04:13:33 +08:00
|
|
|
template <typename T>
|
|
|
|
class ConstRef {
|
|
|
|
public:
|
|
|
|
ConstRef() {}
|
2021-07-10 17:50:17 +08:00
|
|
|
ConstRef(T t) : owned_(t) {}
|
2021-07-08 04:13:33 +08:00
|
|
|
ConstRef(const T* t) : address_(t) {}
|
2023-06-25 23:22:05 +08:00
|
|
|
ConstRef(const ConstRef& t) : owned_(t.owned_), address_(t.address_) {}
|
|
|
|
ConstRef& operator=(const ConstRef& t) {
|
|
|
|
owned_ = t.owned_;
|
|
|
|
address_ = t.address_;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-01-19 02:48:58 +08:00
|
|
|
const T& operator*() const { return address_ ? *address_ : owned_; }
|
|
|
|
const T& operator()() const { return address_ ? *address_ : owned_; }
|
|
|
|
const T* operator->() const { return address_ ? address_ : &owned_; }
|
2021-07-08 04:13:33 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
T owned_;
|
|
|
|
const T* address_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
/// @brief An adapter. Own or reference an mutable object.
|
|
|
|
template <typename T>
|
2021-07-10 19:20:43 +08:00
|
|
|
class Ref {
|
2021-07-10 18:29:39 +08:00
|
|
|
public:
|
|
|
|
Ref() {}
|
2022-02-16 21:09:08 +08:00
|
|
|
Ref(const T& t) : owned_(t) {}
|
|
|
|
Ref(T&& t) : owned_(std::forward<T>(t)) {}
|
2023-03-27 02:20:02 +08:00
|
|
|
Ref(T* t) : owned_(), address_(t) {}
|
2023-06-25 23:22:05 +08:00
|
|
|
Ref(const Ref& t) : owned_(t.owned_), address_(t.address_) {}
|
|
|
|
Ref& operator=(const Ref& t) {
|
|
|
|
owned_ = t.owned_;
|
|
|
|
address_ = t.address_;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-07-10 18:29:39 +08:00
|
|
|
T& operator*() { return address_ ? *address_ : owned_; }
|
|
|
|
T& operator()() { return address_ ? *address_ : owned_; }
|
|
|
|
T* operator->() { return address_ ? address_ : &owned_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T owned_;
|
|
|
|
T* address_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-07-10 17:50:17 +08:00
|
|
|
/// @brief An adapter. Own or reference a constant string. For convenience, this
|
|
|
|
/// class convert multiple mutable string toward a shared representation.
|
2021-07-08 04:13:33 +08:00
|
|
|
class StringRef {
|
|
|
|
public:
|
2021-08-09 05:25:20 +08:00
|
|
|
StringRef(std::string* ref) : address_(ref) {}
|
|
|
|
StringRef(std::string ref) : owned_(std::move(ref)) {}
|
|
|
|
StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
|
|
|
|
StringRef(const char* ref) : StringRef(std::string(ref)) {}
|
2023-06-25 23:22:05 +08:00
|
|
|
StringRef(const StringRef& t) : owned_(t.owned_), address_(t.address_) {}
|
|
|
|
StringRef& operator=(const StringRef& t) {
|
|
|
|
owned_ = t.owned_;
|
|
|
|
address_ = t.address_;
|
|
|
|
return *this;
|
|
|
|
}
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string& operator*() { return address_ ? *address_ : owned_; }
|
2023-05-02 19:32:37 +08:00
|
|
|
std::string& operator()() { return address_ ? *address_ : owned_; }
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string* operator->() { return address_ ? address_ : &owned_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string owned_;
|
|
|
|
std::string* address_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-07-10 17:50:17 +08:00
|
|
|
/// @brief An adapter. Own or reference a constant string. For convenience, this
|
|
|
|
/// class convert multiple immutable string toward a shared representation.
|
2021-07-08 04:13:33 +08:00
|
|
|
class ConstStringRef {
|
|
|
|
public:
|
2021-08-09 05:25:20 +08:00
|
|
|
ConstStringRef(const std::string* ref) : address_(ref) {}
|
|
|
|
ConstStringRef(const std::wstring* ref) : ConstStringRef(to_string(*ref)) {}
|
|
|
|
ConstStringRef(std::string ref) : owned_(std::move(ref)) {}
|
|
|
|
ConstStringRef(std::wstring ref) : ConstStringRef(to_string(ref)) {}
|
2021-07-08 04:13:33 +08:00
|
|
|
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
|
|
|
|
ConstStringRef(const char* ref)
|
|
|
|
: ConstStringRef(to_wstring(std::string(ref))) {}
|
2023-06-25 23:22:05 +08:00
|
|
|
ConstStringRef(const ConstStringRef& t)
|
|
|
|
: owned_(t.owned_), address_(t.address_) {}
|
|
|
|
ConstStringRef& operator=(const ConstStringRef& t) {
|
|
|
|
owned_ = t.owned_;
|
|
|
|
address_ = t.address_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ConstStringRef& operator=(ConstStringRef&& t) {
|
|
|
|
owned_ = std::move(t.owned_);
|
|
|
|
address_ = t.address_;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-08-13 22:26:53 +08:00
|
|
|
const std::string& operator()() const {
|
|
|
|
return address_ ? *address_ : owned_;
|
|
|
|
}
|
2022-01-19 02:48:58 +08:00
|
|
|
const std::string& operator*() const { return address_ ? *address_ : owned_; }
|
|
|
|
const std::string* operator->() const {
|
|
|
|
return address_ ? address_ : &owned_;
|
|
|
|
}
|
2021-08-09 05:25:20 +08:00
|
|
|
|
|
|
|
private:
|
2023-06-25 23:22:05 +08:00
|
|
|
std::string owned_;
|
2021-08-09 05:25:20 +08:00
|
|
|
const std::string* address_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief An adapter. Reference a list of strings.
|
|
|
|
class ConstStringListRef {
|
|
|
|
public:
|
2023-06-25 23:22:05 +08:00
|
|
|
ConstStringListRef() = default;
|
2021-08-09 05:25:20 +08:00
|
|
|
ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
|
|
|
|
ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
|
|
|
|
|
2023-06-25 23:22:05 +08:00
|
|
|
size_t size() const {
|
|
|
|
if (ref_) {
|
|
|
|
return ref_->size();
|
|
|
|
}
|
|
|
|
if (ref_wide_) {
|
|
|
|
return ref_wide_->size();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string operator[](size_t i) const {
|
2023-06-25 23:22:05 +08:00
|
|
|
if (ref_) {
|
|
|
|
return (*ref_)[i];
|
|
|
|
}
|
|
|
|
if (ref_wide_) {
|
|
|
|
return to_string((*ref_wide_)[i]);
|
|
|
|
}
|
|
|
|
return "";
|
2021-08-09 05:25:20 +08:00
|
|
|
}
|
2021-07-08 04:13:33 +08:00
|
|
|
|
|
|
|
private:
|
2021-08-09 05:25:20 +08:00
|
|
|
const std::vector<std::string>* ref_ = nullptr;
|
|
|
|
const std::vector<std::wstring>* ref_wide_ = nullptr;
|
2021-07-08 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ftxui
|
|
|
|
|
|
|
|
#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
|
|
|
|
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|