mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 16:08:08 +08:00
Remove Ref<XxxOption> and add new interfaces. (#686)
1. Stop taking Ref<XxxOption> in Component constructors. Instead, use the XxxOption directly. Passing by copy avoid problems developers had where one was shared in between multiple component, causing issues. 2. Add variants of most component constructors taking a struct only. This replaces: https://github.com/ArthurSonzogni/FTXUI/pull/670 This fixes: https://github.com/ArthurSonzogni/FTXUI/issues/426
This commit is contained in:
@@ -13,6 +13,12 @@ class ConstRef {
|
||||
ConstRef() {}
|
||||
ConstRef(T t) : owned_(t) {}
|
||||
ConstRef(const T* t) : address_(t) {}
|
||||
ConstRef(const ConstRef& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
ConstRef& operator=(const ConstRef& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
const T& operator*() const { return address_ ? *address_ : owned_; }
|
||||
const T& operator()() const { return address_ ? *address_ : owned_; }
|
||||
const T* operator->() const { return address_ ? address_ : &owned_; }
|
||||
@@ -30,6 +36,12 @@ class Ref {
|
||||
Ref(const T& t) : owned_(t) {}
|
||||
Ref(T&& t) : owned_(std::forward<T>(t)) {}
|
||||
Ref(T* t) : owned_(), address_(t) {}
|
||||
Ref(const Ref& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
Ref& operator=(const Ref& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
T& operator*() { return address_ ? *address_ : owned_; }
|
||||
T& operator()() { return address_ ? *address_ : owned_; }
|
||||
T* operator->() { return address_ ? address_ : &owned_; }
|
||||
@@ -47,6 +59,12 @@ class StringRef {
|
||||
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)) {}
|
||||
StringRef(const StringRef& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
StringRef& operator=(const StringRef& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
std::string& operator*() { return address_ ? *address_ : owned_; }
|
||||
std::string& operator()() { return address_ ? *address_ : owned_; }
|
||||
std::string* operator->() { return address_ ? address_ : &owned_; }
|
||||
@@ -67,6 +85,18 @@ class ConstStringRef {
|
||||
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
|
||||
ConstStringRef(const char* ref)
|
||||
: ConstStringRef(to_wstring(std::string(ref))) {}
|
||||
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;
|
||||
}
|
||||
const std::string& operator()() const {
|
||||
return address_ ? *address_ : owned_;
|
||||
}
|
||||
@@ -76,19 +106,35 @@ class ConstStringRef {
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string owned_;
|
||||
std::string owned_;
|
||||
const std::string* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief An adapter. Reference a list of strings.
|
||||
class ConstStringListRef {
|
||||
public:
|
||||
ConstStringListRef() = default;
|
||||
ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
|
||||
ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
|
||||
|
||||
size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
|
||||
size_t size() const {
|
||||
if (ref_) {
|
||||
return ref_->size();
|
||||
}
|
||||
if (ref_wide_) {
|
||||
return ref_wide_->size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string operator[](size_t i) const {
|
||||
return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
|
||||
if (ref_) {
|
||||
return (*ref_)[i];
|
||||
}
|
||||
if (ref_wide_) {
|
||||
return to_string((*ref_wide_)[i]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user