2023-08-19 19:56:36 +08:00
|
|
|
// 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.
|
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>
|
2023-08-13 03:18:33 +08:00
|
|
|
#include <variant>
|
2021-07-08 04:13:33 +08:00
|
|
|
|
|
|
|
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:
|
2023-08-13 03:18:33 +08:00
|
|
|
ConstRef() = default;
|
2024-05-01 17:40:49 +08:00
|
|
|
ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
|
|
|
|
ConstRef(const T* t) : variant_(t) {} // NOLINT
|
|
|
|
ConstRef& operator=(ConstRef&&) noexcept = default;
|
2023-08-13 03:18:33 +08:00
|
|
|
ConstRef(const ConstRef<T>&) = default;
|
2024-05-01 17:40:49 +08:00
|
|
|
ConstRef(ConstRef<T>&&) noexcept = default;
|
|
|
|
~ConstRef() = default;
|
2023-08-13 03:18:33 +08:00
|
|
|
|
2023-08-19 17:10:29 +08:00
|
|
|
// Make a "reseatable" reference
|
2023-08-13 03:18:33 +08:00
|
|
|
ConstRef<T>& operator=(const ConstRef<T>&) = default;
|
|
|
|
|
|
|
|
// Accessors:
|
|
|
|
const T& operator()() const { return *Address(); }
|
|
|
|
const T& operator*() const { return *Address(); }
|
|
|
|
const T* operator->() const { return Address(); }
|
2021-07-08 04:13:33 +08:00
|
|
|
|
|
|
|
private:
|
2023-08-13 03:18:33 +08:00
|
|
|
std::variant<T, const T*> variant_ = T{};
|
|
|
|
|
|
|
|
const T* Address() const {
|
|
|
|
return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
|
|
|
|
: std::get<const T*>(variant_);
|
|
|
|
}
|
2021-07-08 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
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:
|
2023-08-13 03:18:33 +08:00
|
|
|
Ref() = default;
|
2024-05-01 17:40:49 +08:00
|
|
|
Ref(T t) : variant_(std::move(t)) {} // NOLINT
|
|
|
|
Ref(T* t) : variant_(t) {} // NOLINT
|
|
|
|
~Ref() = default;
|
|
|
|
Ref& operator=(Ref&&) noexcept = default;
|
2023-08-13 03:18:33 +08:00
|
|
|
Ref(const Ref<T>&) = default;
|
2024-05-01 17:40:49 +08:00
|
|
|
Ref(Ref<T>&&) noexcept = default;
|
2023-08-13 03:18:33 +08:00
|
|
|
|
2023-08-19 17:10:29 +08:00
|
|
|
// Make a "reseatable" reference.
|
2023-08-13 03:18:33 +08:00
|
|
|
Ref<T>& operator=(const Ref<T>&) = default;
|
|
|
|
|
|
|
|
// Accessors:
|
|
|
|
T& operator()() { return *Address(); }
|
|
|
|
T& operator*() { return *Address(); }
|
|
|
|
T* operator->() { return Address(); }
|
|
|
|
const T& operator()() const { return *Address(); }
|
|
|
|
const T& operator*() const { return *Address(); }
|
|
|
|
const T* operator->() const { return Address(); }
|
2021-07-10 18:29:39 +08:00
|
|
|
|
|
|
|
private:
|
2023-08-13 03:18:33 +08:00
|
|
|
std::variant<T, T*> variant_ = T{};
|
|
|
|
|
|
|
|
const T* Address() const {
|
|
|
|
return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
|
|
|
|
: std::get<T*>(variant_);
|
|
|
|
}
|
|
|
|
T* Address() {
|
|
|
|
return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
|
|
|
|
: std::get<T*>(variant_);
|
|
|
|
}
|
2021-07-10 18:29:39 +08:00
|
|
|
};
|
|
|
|
|
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.
|
2023-08-13 03:18:33 +08:00
|
|
|
class StringRef : public Ref<std::string> {
|
2021-07-08 04:13:33 +08:00
|
|
|
public:
|
2023-08-13 03:18:33 +08:00
|
|
|
using Ref<std::string>::Ref;
|
|
|
|
|
2024-05-01 17:40:49 +08:00
|
|
|
StringRef(const wchar_t* ref) // NOLINT
|
|
|
|
: StringRef(to_string(std::wstring(ref))) {}
|
|
|
|
StringRef(const char* ref) // NOLINT
|
|
|
|
: StringRef(std::string(ref)) {}
|
2021-08-09 05:25:20 +08:00
|
|
|
};
|
|
|
|
|
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.
|
2023-08-13 03:18:33 +08:00
|
|
|
class ConstStringRef : public ConstRef<std::string> {
|
2021-07-08 04:13:33 +08:00
|
|
|
public:
|
2023-08-13 03:18:33 +08:00
|
|
|
using ConstRef<std::string>::ConstRef;
|
|
|
|
|
2024-05-01 17:40:49 +08:00
|
|
|
ConstStringRef(const std::wstring* ref) // NOLINT
|
|
|
|
: ConstStringRef(to_string(*ref)) {}
|
|
|
|
ConstStringRef(const std::wstring ref) // NOLINT
|
|
|
|
: ConstStringRef(to_string(ref)) {}
|
|
|
|
ConstStringRef(const wchar_t* ref) // NOLINT
|
2023-08-13 03:18:33 +08:00
|
|
|
: ConstStringRef(to_string(std::wstring(ref))) {}
|
2024-05-01 17:40:49 +08:00
|
|
|
ConstStringRef(const char* ref) // NOLINT
|
|
|
|
: ConstStringRef(std::string(ref)) {}
|
2021-08-09 05:25:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief An adapter. Reference a list of strings.
|
|
|
|
class ConstStringListRef {
|
|
|
|
public:
|
2023-06-25 23:22:05 +08:00
|
|
|
ConstStringListRef() = default;
|
2024-05-01 17:40:49 +08:00
|
|
|
~ConstStringListRef() = default;
|
|
|
|
ConstStringListRef(ConstStringListRef&&) = delete;
|
|
|
|
ConstStringListRef& operator=(ConstStringListRef&&) = delete;
|
|
|
|
ConstStringListRef(const std::vector<std::string>* ref) // NOLINT
|
|
|
|
: ref_(ref) {}
|
|
|
|
ConstStringListRef(const std::vector<std::wstring>* ref) // NOLINT
|
|
|
|
: ref_wide_(ref) {}
|
2023-08-13 13:49:37 +08:00
|
|
|
ConstStringListRef(const ConstStringListRef& other) = default;
|
|
|
|
ConstStringListRef& operator=(const ConstStringListRef& other) = default;
|
2021-08-09 05:25:20 +08:00
|
|
|
|
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 */
|