Loading...
Searching...
No Matches
ref.hpp
Go to the documentation of this file.
// 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.
#ifndef FTXUI_UTIL_REF_HPP
#define FTXUI_UTIL_REF_HPP
#include <ftxui/screen/string.hpp>
#include <memory>
#include <string>
#include <variant>
#include <vector>
namespace ftxui {
/// @brief An adapter. Own or reference an immutable object.
template <typename T>
class ConstRef {
public:
ConstRef() = default;
ConstRef(T t) : variant_(std::move(t)) {} // NOLINT
ConstRef(const T* t) : variant_(t) {} // NOLINT
ConstRef& operator=(ConstRef&&) noexcept = default;
ConstRef(const ConstRef<T>&) = default;
ConstRef(ConstRef<T>&&) noexcept = default;
~ConstRef() = default;
// Make a "reseatable" reference
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(); }
private:
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_);
}
};
/// @brief An adapter. Own or reference an mutable object.
template <typename T>
class Ref {
public:
Ref() = default;
Ref(T t) : variant_(std::move(t)) {} // NOLINT
Ref(T* t) : variant_(t) {} // NOLINT
~Ref() = default;
Ref& operator=(Ref&&) noexcept = default;
Ref(const Ref<T>&) = default;
Ref(Ref<T>&&) noexcept = default;
// Make a "reseatable" reference.
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(); }
private:
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_);
}
};
/// @brief An adapter. Own or reference a constant string. For convenience, this
/// class convert multiple mutable string toward a shared representation.
class StringRef : public Ref<std::string> {
public:
using Ref<std::string>::Ref;
StringRef(const wchar_t* ref) // NOLINT
: StringRef(to_string(std::wstring(ref))) {}
StringRef(const char* ref) // NOLINT
: StringRef(std::string(ref)) {}
};
/// @brief An adapter. Own or reference a constant string. For convenience, this
/// class convert multiple immutable string toward a shared representation.
class ConstStringRef : public ConstRef<std::string> {
public:
using ConstRef<std::string>::ConstRef;
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
: ConstStringRef(to_string(std::wstring(ref))) {}
ConstStringRef(const char* ref) // NOLINT
: ConstStringRef(std::string(ref)) {}
};
/// @brief An adapter. Reference a list of strings.
///
/// Supported input:
/// - `std::vector<std::string>`
/// - `std::vector<std::string>*`
/// - `std::vector<std::wstring>*`
/// - `Adapter*`
/// - `std::unique_ptr<Adapter>`
class ConstStringListRef {
public:
// Bring your own adapter:
class Adapter {
public:
Adapter() = default;
Adapter(const Adapter&) = default;
Adapter& operator=(const Adapter&) = default;
Adapter(Adapter&&) = default;
Adapter& operator=(Adapter&&) = default;
virtual ~Adapter() = default;
virtual size_t size() const = 0;
virtual std::string operator[](size_t i) const = 0;
};
using Variant = std::variant<const std::vector<std::string>, //
const std::vector<std::string>*, //
const std::vector<std::wstring>*, //
Adapter*, //
std::unique_ptr<Adapter> //
>;
ConstStringListRef() = default;
~ConstStringListRef() = default;
ConstStringListRef& operator=(const ConstStringListRef&) = default;
ConstStringListRef& operator=(ConstStringListRef&&) = default;
ConstStringListRef(ConstStringListRef&&) = default;
ConstStringListRef(const ConstStringListRef&) = default;
ConstStringListRef(std::vector<std::string> value) // NOLINT
{
variant_ = std::make_shared<Variant>(value);
}
ConstStringListRef(const std::vector<std::string>* value) // NOLINT
{
variant_ = std::make_shared<Variant>(value);
}
ConstStringListRef(const std::vector<std::wstring>* value) // NOLINT
{
variant_ = std::make_shared<Variant>(value);
}
ConstStringListRef(Adapter* adapter) // NOLINT
{
variant_ = std::make_shared<Variant>(adapter);
}
template <typename AdapterType>
ConstStringListRef(std::unique_ptr<AdapterType> adapter) // NOLINT
{
variant_ = std::make_shared<Variant>(
static_cast<std::unique_ptr<Adapter>>(std::move(adapter)));
}
size_t size() const {
return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
}
std::string operator[](size_t i) const {
return variant_ ? std::visit(IndexedGetter(i), *variant_) : "";
}
private:
struct SizeVisitor {
size_t operator()(const std::vector<std::string>& v) const {
return v.size();
}
size_t operator()(const std::vector<std::string>* v) const {
return v->size();
}
size_t operator()(const std::vector<std::wstring>* v) const {
return v->size();
}
size_t operator()(const Adapter* v) const { return v->size(); }
size_t operator()(const std::unique_ptr<Adapter>& v) const {
return v->size();
}
};
struct IndexedGetter {
IndexedGetter(size_t index) // NOLINT
: index_(index) {}
size_t index_;
std::string operator()(const std::vector<std::string>& v) const {
return v[index_];
}
std::string operator()(const std::vector<std::string>* v) const {
return (*v)[index_];
}
std::string operator()(const std::vector<std::wstring>* v) const {
return to_string((*v)[index_]);
}
std::string operator()(const Adapter* v) const { return (*v)[index_]; }
std::string operator()(const std::unique_ptr<Adapter>& v) const {
return (*v)[index_];
}
};
std::shared_ptr<Variant> variant_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
ftxui::ConstRef
An adapter. Own or reference an immutable object.
Definition ref.hpp:17
ftxui::ConstRef::operator*
const T & operator*() const
Definition ref.hpp:32
ftxui::ConstRef::ConstRef
ConstRef()=default
ftxui::ConstRef::ConstRef
ConstRef(const T *t)
Definition ref.hpp:21
ftxui::ConstRef::ConstRef
ConstRef(T t)
Definition ref.hpp:20
ftxui::ConstRef::operator->
const T * operator->() const
Definition ref.hpp:33
ftxui::ConstRef::operator=
ConstRef & operator=(ConstRef &&) noexcept=default
ftxui::ConstStringListRef::Adapter
Definition ref.hpp:119
ftxui::ConstStringListRef::Adapter::operator=
Adapter & operator=(Adapter &&)=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter(Adapter &&)=default
ftxui::ConstStringListRef::Adapter::operator=
Adapter & operator=(const Adapter &)=default
ftxui::ConstStringListRef::Adapter::size
virtual size_t size() const =0
ftxui::ConstStringListRef::Adapter::operator[]
virtual std::string operator[](size_t i) const =0
ftxui::ConstStringListRef::Adapter::~Adapter
virtual ~Adapter()=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter()=default
ftxui::ConstStringListRef::Adapter::Adapter
Adapter(const Adapter &)=default
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition ref.hpp:116
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(std::vector< std::string > value)
Definition ref.hpp:144
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::string > *value)
Definition ref.hpp:148
ftxui::ConstStringListRef::size
size_t size() const
Definition ref.hpp:167
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef()=default
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(ConstStringListRef &&)=default
ftxui::ConstStringListRef::operator=
ConstStringListRef & operator=(ConstStringListRef &&)=default
ftxui::ConstStringListRef::~ConstStringListRef
~ConstStringListRef()=default
ftxui::ConstStringListRef::operator=
ConstStringListRef & operator=(const ConstStringListRef &)=default
ftxui::ConstStringListRef::Variant
std::variant< const std::vector< std::string >, const std::vector< std::string > *, const std::vector< std::wstring > *, Adapter *, std::unique_ptr< Adapter > > Variant
Definition ref.hpp:135
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(std::unique_ptr< AdapterType > adapter)
Definition ref.hpp:161
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const std::vector< std::wstring > *value)
Definition ref.hpp:152
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(const ConstStringListRef &)=default
ftxui::ConstStringListRef::ConstStringListRef
ConstStringListRef(Adapter *adapter)
Definition ref.hpp:156
ftxui::ConstStringListRef::operator[]
std::string operator[](size_t i) const
Definition ref.hpp:171
ftxui::ConstStringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:94
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const char *ref)
Definition ref.hpp:104
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::wstring ref)
Definition ref.hpp:100
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const std::wstring *ref)
Definition ref.hpp:98
ftxui::ConstStringRef::ConstStringRef
ConstStringRef(const wchar_t *ref)
Definition ref.hpp:102
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition ref.hpp:46
ftxui::Ref::operator*
const T & operator*() const
Definition ref.hpp:64
ftxui::Ref::~Ref
~Ref()=default
ftxui::Ref::operator*
T & operator*()
Definition ref.hpp:61
ftxui::Ref::operator->
T * operator->()
Definition ref.hpp:62
ftxui::Ref::Ref
Ref()=default
ftxui::Ref::Ref
Ref(T t)
Definition ref.hpp:49
ftxui::Ref::operator->
const T * operator->() const
Definition ref.hpp:65
ftxui::Ref::operator=
Ref & operator=(Ref &&) noexcept=default
ftxui::Ref::Ref
Ref(T *t)
Definition ref.hpp:50
ftxui::Ref::operator()
const T & operator()() const
Definition ref.hpp:63
ftxui::StringRef
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
Definition ref.hpp:82
ftxui::StringRef::StringRef
StringRef(const char *ref)
Definition ref.hpp:88
ftxui::StringRef::StringRef
StringRef(const wchar_t *ref)
Definition ref.hpp:86
ftxui
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::to_string
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition string.cpp:1565
string.hpp