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 {
|
||||
public:
|
||||
ConstRef() = default;
|
||||
|
||||
// Owning constructors:
|
||||
ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
|
||||
|
||||
// Referencing constructors:
|
||||
ConstRef(const T* t) : variant_(t) {} // NOLINT
|
||||
|
||||
ConstRef& operator=(ConstRef&&) noexcept = default;
|
||||
ConstRef(const ConstRef<T>&) = default;
|
||||
ConstRef(ConstRef<T>&&) noexcept = default;
|
||||
@@ -47,8 +52,13 @@ template <typename T>
|
||||
class Ref {
|
||||
public:
|
||||
Ref() = default;
|
||||
|
||||
// Owning constructors:
|
||||
Ref(T t) : variant_(std::move(t)) {} // NOLINT
|
||||
//
|
||||
// Referencing constructors:
|
||||
Ref(T* t) : variant_(t) {} // NOLINT
|
||||
//
|
||||
~Ref() = default;
|
||||
Ref& operator=(Ref&&) noexcept = default;
|
||||
Ref(const Ref<T>&) = default;
|
||||
@@ -84,10 +94,15 @@ class StringRef : public Ref<std::string> {
|
||||
public:
|
||||
using Ref<std::string>::Ref;
|
||||
|
||||
// Owning constructors:
|
||||
StringRef(const wchar_t* ref) // NOLINT
|
||||
: StringRef(to_string(std::wstring(ref))) {}
|
||||
StringRef(const char* ref) // NOLINT
|
||||
: 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
|
||||
@@ -96,14 +111,21 @@ class ConstStringRef : public ConstRef<std::string> {
|
||||
public:
|
||||
using ConstRef<std::string>::ConstRef;
|
||||
|
||||
// Referencing constructors:
|
||||
ConstStringRef(const std::wstring* ref) // NOLINT
|
||||
: ConstStringRef(to_string(*ref)) {}
|
||||
|
||||
// Owning constructors:
|
||||
ConstStringRef(const std::wstring ref) // NOLINT
|
||||
: ConstStringRef(to_string(ref)) {}
|
||||
ConstStringRef(std::wstring_view ref) // NOLINT
|
||||
: ConstStringRef(to_string(ref)) {}
|
||||
ConstStringRef(const wchar_t* ref) // NOLINT
|
||||
: ConstStringRef(to_string(std::wstring(ref))) {}
|
||||
ConstStringRef(const char* ref) // NOLINT
|
||||
: ConstStringRef(std::string(ref)) {}
|
||||
ConstStringRef(std::string_view ref) // NOLINT
|
||||
: ConstStringRef(std::string(ref)) {}
|
||||
};
|
||||
|
||||
/// @brief An adapter. Reference a list of strings.
|
||||
@@ -144,33 +166,26 @@ class ConstStringListRef {
|
||||
ConstStringListRef(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);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::string>* value) // NOLINT
|
||||
{
|
||||
ConstStringListRef(const std::vector<std::string>* value) {// NOLINT
|
||||
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);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::string_view>* value) // NOLINT
|
||||
{
|
||||
ConstStringListRef(const std::vector<std::string_view>* value) { // NOLINT
|
||||
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);
|
||||
}
|
||||
ConstStringListRef(Adapter* adapter) // NOLINT
|
||||
{
|
||||
ConstStringListRef(Adapter* adapter) { // NOLINT
|
||||
variant_ = std::make_shared<Variant>(adapter);
|
||||
}
|
||||
template <typename AdapterType>
|
||||
ConstStringListRef(std::unique_ptr<AdapterType> adapter) // NOLINT
|
||||
{
|
||||
ConstStringListRef(std::unique_ptr<AdapterType> adapter) { // NOLINT
|
||||
variant_ = std::make_shared<Variant>(
|
||||
static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
|
||||
}
|
||||
@@ -179,66 +194,34 @@ class ConstStringListRef {
|
||||
return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
|
||||
}
|
||||
|
||||
std::string operator[](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::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 {};
|
||||
std::string_view operator[](size_t i) const {
|
||||
return variant_ ? std::visit(IndexedGetter{i}, *variant_) : "";
|
||||
}
|
||||
|
||||
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 {
|
||||
size_t operator()(const std::vector<std::string>& v) const {
|
||||
return v.size();
|
||||
|
||||
Reference in New Issue
Block a user