mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Add std::string_view to ConstStringRef
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
This commit is contained in:
@@ -18,8 +18,13 @@ template <typename T>
|
|||||||
class ConstRef {
|
class ConstRef {
|
||||||
public:
|
public:
|
||||||
ConstRef() = default;
|
ConstRef() = default;
|
||||||
|
|
||||||
|
// Owning constructors:
|
||||||
ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
|
ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
|
||||||
|
|
||||||
|
// Referencing constructors:
|
||||||
ConstRef(const T* t) : variant_(t) {} // NOLINT
|
ConstRef(const T* t) : variant_(t) {} // NOLINT
|
||||||
|
|
||||||
ConstRef& operator=(ConstRef&&) noexcept = default;
|
ConstRef& operator=(ConstRef&&) noexcept = default;
|
||||||
ConstRef(const ConstRef<T>&) = default;
|
ConstRef(const ConstRef<T>&) = default;
|
||||||
ConstRef(ConstRef<T>&&) noexcept = default;
|
ConstRef(ConstRef<T>&&) noexcept = default;
|
||||||
@@ -47,8 +52,13 @@ template <typename T>
|
|||||||
class Ref {
|
class Ref {
|
||||||
public:
|
public:
|
||||||
Ref() = default;
|
Ref() = default;
|
||||||
|
|
||||||
|
// Owning constructors:
|
||||||
Ref(T t) : variant_(std::move(t)) {} // NOLINT
|
Ref(T t) : variant_(std::move(t)) {} // NOLINT
|
||||||
|
//
|
||||||
|
// Referencing constructors:
|
||||||
Ref(T* t) : variant_(t) {} // NOLINT
|
Ref(T* t) : variant_(t) {} // NOLINT
|
||||||
|
//
|
||||||
~Ref() = default;
|
~Ref() = default;
|
||||||
Ref& operator=(Ref&&) noexcept = default;
|
Ref& operator=(Ref&&) noexcept = default;
|
||||||
Ref(const Ref<T>&) = default;
|
Ref(const Ref<T>&) = default;
|
||||||
@@ -84,10 +94,15 @@ class StringRef : public Ref<std::string> {
|
|||||||
public:
|
public:
|
||||||
using Ref<std::string>::Ref;
|
using Ref<std::string>::Ref;
|
||||||
|
|
||||||
|
// Owning constructors:
|
||||||
StringRef(const wchar_t* ref) // NOLINT
|
StringRef(const wchar_t* ref) // NOLINT
|
||||||
: StringRef(to_string(std::wstring(ref))) {}
|
: StringRef(to_string(std::wstring(ref))) {}
|
||||||
StringRef(const char* ref) // NOLINT
|
StringRef(const char* ref) // NOLINT
|
||||||
: StringRef(std::string(ref)) {}
|
: StringRef(std::string(ref)) {}
|
||||||
|
StringRef(std::string_view ref) // NOLINT
|
||||||
|
: StringRef(std::string(ref)) {}
|
||||||
|
StringRef(std::wstring_view ref) // NOLINT
|
||||||
|
: StringRef(to_string(ref)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @brief An adapter. Own or reference a constant string. For convenience, this
|
/// @brief An adapter. Own or reference a constant string. For convenience, this
|
||||||
@@ -96,14 +111,21 @@ class ConstStringRef : public ConstRef<std::string> {
|
|||||||
public:
|
public:
|
||||||
using ConstRef<std::string>::ConstRef;
|
using ConstRef<std::string>::ConstRef;
|
||||||
|
|
||||||
|
// Referencing constructors:
|
||||||
ConstStringRef(const std::wstring* ref) // NOLINT
|
ConstStringRef(const std::wstring* ref) // NOLINT
|
||||||
: ConstStringRef(to_string(*ref)) {}
|
: ConstStringRef(to_string(*ref)) {}
|
||||||
|
|
||||||
|
// Owning constructors:
|
||||||
ConstStringRef(const std::wstring ref) // NOLINT
|
ConstStringRef(const std::wstring ref) // NOLINT
|
||||||
: ConstStringRef(to_string(ref)) {}
|
: ConstStringRef(to_string(ref)) {}
|
||||||
|
ConstStringRef(std::wstring_view ref) // NOLINT
|
||||||
|
: ConstStringRef(to_string(ref)) {}
|
||||||
ConstStringRef(const wchar_t* ref) // NOLINT
|
ConstStringRef(const wchar_t* ref) // NOLINT
|
||||||
: ConstStringRef(to_string(std::wstring(ref))) {}
|
: ConstStringRef(to_string(std::wstring(ref))) {}
|
||||||
ConstStringRef(const char* ref) // NOLINT
|
ConstStringRef(const char* ref) // NOLINT
|
||||||
: ConstStringRef(std::string(ref)) {}
|
: ConstStringRef(std::string(ref)) {}
|
||||||
|
ConstStringRef(std::string_view ref) // NOLINT
|
||||||
|
: ConstStringRef(std::string(ref)) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @brief An adapter. Reference a list of strings.
|
/// @brief An adapter. Reference a list of strings.
|
||||||
@@ -144,33 +166,26 @@ class ConstStringListRef {
|
|||||||
ConstStringListRef(ConstStringListRef&&) = default;
|
ConstStringListRef(ConstStringListRef&&) = default;
|
||||||
ConstStringListRef(const ConstStringListRef&) = default;
|
ConstStringListRef(const ConstStringListRef&) = default;
|
||||||
|
|
||||||
ConstStringListRef(std::vector<std::string> value) // NOLINT
|
ConstStringListRef(std::vector<std::string> value) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(value);
|
variant_ = std::make_shared<Variant>(value);
|
||||||
}
|
}
|
||||||
ConstStringListRef(const std::vector<std::string>* value) // NOLINT
|
ConstStringListRef(const std::vector<std::string>* value) {// NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(value);
|
variant_ = std::make_shared<Variant>(value);
|
||||||
}
|
}
|
||||||
ConstStringListRef(std::vector<std::string_view> value) // NOLINT
|
ConstStringListRef(std::vector<std::string_view> value) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(value);
|
variant_ = std::make_shared<Variant>(value);
|
||||||
}
|
}
|
||||||
ConstStringListRef(const std::vector<std::string_view>* value) // NOLINT
|
ConstStringListRef(const std::vector<std::string_view>* value) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(value);
|
variant_ = std::make_shared<Variant>(value);
|
||||||
}
|
}
|
||||||
ConstStringListRef(const std::vector<std::wstring>* value) // NOLINT
|
ConstStringListRef(const std::vector<std::wstring>* value) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(value);
|
variant_ = std::make_shared<Variant>(value);
|
||||||
}
|
}
|
||||||
ConstStringListRef(Adapter* adapter) // NOLINT
|
ConstStringListRef(Adapter* adapter) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(adapter);
|
variant_ = std::make_shared<Variant>(adapter);
|
||||||
}
|
}
|
||||||
template <typename AdapterType>
|
template <typename AdapterType>
|
||||||
ConstStringListRef(std::unique_ptr<AdapterType> adapter) // NOLINT
|
ConstStringListRef(std::unique_ptr<AdapterType> adapter) { // NOLINT
|
||||||
{
|
|
||||||
variant_ = std::make_shared<Variant>(
|
variant_ = std::make_shared<Variant>(
|
||||||
static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
|
static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
|
||||||
}
|
}
|
||||||
@@ -179,66 +194,34 @@ class ConstStringListRef {
|
|||||||
return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
|
return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string operator[](size_t i) const {
|
std::string_view operator[](size_t i) const {
|
||||||
if (!variant_) {
|
return variant_ ? std::visit(IndexedGetter{i}, *variant_) : "";
|
||||||
return "";
|
|
||||||
}
|
|
||||||
auto& v = *variant_;
|
|
||||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
|
||||||
return std::get<const std::vector<std::string>>(v)[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
|
||||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
|
||||||
return std::string(std::get<const std::vector<std::string_view>>(v)[i]);
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
|
||||||
return std::string(
|
|
||||||
(*std::get<const std::vector<std::string_view>*>(v))[i]);
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
|
||||||
return to_string((*std::get<const std::vector<std::wstring>*>(v))[i]);
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<Adapter*>(v)) {
|
|
||||||
return std::string((*std::get<Adapter*>(v))[i]);
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
|
||||||
return std::string((*std::get<std::unique_ptr<Adapter>>(v))[i]);
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view at(size_t i) const {
|
|
||||||
if (!variant_) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
auto& v = *variant_;
|
|
||||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
|
||||||
return std::get<const std::vector<std::string>>(v)[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
|
||||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
|
||||||
return std::get<const std::vector<std::string_view>>(v)[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
|
||||||
return (*std::get<const std::vector<std::string_view>*>(v))[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<Adapter*>(v)) {
|
|
||||||
return (*std::get<Adapter*>(v))[i];
|
|
||||||
}
|
|
||||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
|
||||||
return (*std::get<std::unique_ptr<Adapter>>(v))[i];
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct IndexedGetter {
|
||||||
|
size_t i;
|
||||||
|
std::string_view operator()(const std::vector<std::string>& v) const {
|
||||||
|
return v[i];
|
||||||
|
}
|
||||||
|
std::string_view operator()(const std::vector<std::string>* v) const {
|
||||||
|
return (*v)[i];
|
||||||
|
}
|
||||||
|
std::string_view operator()(const std::vector<std::string_view>& v) const {
|
||||||
|
return std::string(v[i]);
|
||||||
|
}
|
||||||
|
std::string_view operator()(const std::vector<std::string_view>* v) const {
|
||||||
|
return std::string((*v)[i]);
|
||||||
|
}
|
||||||
|
std::string_view operator()(const std::vector<std::wstring>* v) const {
|
||||||
|
return to_string((*v)[i]);
|
||||||
|
}
|
||||||
|
std::string_view operator()(Adapter* v) const { return std::string((*v)[i]); }
|
||||||
|
std::string_view operator()(const std::unique_ptr<Adapter>& v) const {
|
||||||
|
return (*v)[i];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
struct SizeVisitor {
|
struct SizeVisitor {
|
||||||
size_t operator()(const std::vector<std::string>& v) const {
|
size_t operator()(const std::vector<std::string>& v) const {
|
||||||
return v.size();
|
return v.size();
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ class MenuBase : public ComponentBase, public MenuOption {
|
|||||||
const bool is_selected = (selected() == i);
|
const bool is_selected = (selected() == i);
|
||||||
|
|
||||||
const EntryState state = {
|
const EntryState state = {
|
||||||
entries[i], false, is_selected, is_focused, i,
|
std::string(entries[i]), false, is_selected, is_focused, i,
|
||||||
};
|
};
|
||||||
|
|
||||||
Element element = (entries_option.transform ? entries_option.transform
|
Element element = (entries_option.transform ? entries_option.transform
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class RadioboxBase : public ComponentBase, public RadioboxOption {
|
|||||||
const bool is_focused = (focused_entry() == i) && is_menu_focused;
|
const bool is_focused = (focused_entry() == i) && is_menu_focused;
|
||||||
const bool is_selected = (hovered_ == i);
|
const bool is_selected = (hovered_ == i);
|
||||||
auto state = EntryState{
|
auto state = EntryState{
|
||||||
entries[i], selected() == i, is_selected, is_focused, i,
|
std::string(entries[i]), selected() == i, is_selected, is_focused, i,
|
||||||
};
|
};
|
||||||
auto element =
|
auto element =
|
||||||
(transform ? transform : RadioboxOption::Simple().transform)(state);
|
(transform ? transform : RadioboxOption::Simple().transform)(state);
|
||||||
|
|||||||
Reference in New Issue
Block a user