4#ifndef FTXUI_UTIL_REF_HPP
5#define FTXUI_UTIL_REF_HPP
36 std::variant<T, const T*> variant_ = T{};
38 const T* Address()
const {
39 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
50 Ref(T* t) : variant_(t) {}
68 std::variant<T, T*> variant_ = T{};
70 const T* Address()
const {
71 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
75 return std::holds_alternative<T>(variant_) ? &std::get<T>(variant_)
127 virtual size_t size()
const = 0;
130 using Variant = std::variant<const std::vector<std::string>,
131 const std::vector<std::string>*,
132 const std::vector<std::wstring>*,
134 std::unique_ptr<Adapter>
146 variant_ = std::make_shared<Variant>(value);
150 variant_ = std::make_shared<Variant>(value);
154 variant_ = std::make_shared<Variant>(value);
158 variant_ = std::make_shared<Variant>(
adapter);
160 template <
typename AdapterType>
163 variant_ = std::make_shared<Variant>(
164 static_cast<std::unique_ptr<Adapter>
>(std::move(
adapter)));
168 return variant_ ? std::visit(SizeVisitor(), *variant_) : 0;
172 return variant_ ? std::visit(IndexedGetter(i), *variant_) :
"";
177 size_t operator()(
const std::vector<std::string>& v)
const {
180 size_t operator()(
const std::vector<std::string>* v)
const {
183 size_t operator()(
const std::vector<std::wstring>* v)
const {
186 size_t operator()(
const Adapter* v)
const {
return v->size(); }
187 size_t operator()(
const std::unique_ptr<Adapter>& v)
const {
192 struct IndexedGetter {
193 IndexedGetter(
size_t index)
196 std::string operator()(
const std::vector<std::string>& v)
const {
199 std::string operator()(
const std::vector<std::string>* v)
const {
202 std::string operator()(
const std::vector<std::wstring>* v)
const {
205 std::string operator()(
const Adapter* v)
const {
return (*v)[index_]; }
206 std::string operator()(
const std::unique_ptr<Adapter>& v)
const {
211 std::shared_ptr<Variant> variant_;
An adapter. Own or reference an immutable object.
const T & operator*() const
const T * operator->() const
ConstRef & operator=(ConstRef &&) noexcept=default
Adapter & operator=(Adapter &&)=default
Adapter(Adapter &&)=default
Adapter & operator=(const Adapter &)=default
virtual size_t size() const =0
virtual std::string operator[](size_t i) const =0
virtual ~Adapter()=default
Adapter(const Adapter &)=default
An adapter. Reference a list of strings.
ConstStringListRef(std::vector< std::string > value)
ConstStringListRef(const std::vector< std::string > *value)
ConstStringListRef()=default
ConstStringListRef(ConstStringListRef &&)=default
ConstStringListRef & operator=(ConstStringListRef &&)=default
~ConstStringListRef()=default
ConstStringListRef & operator=(const ConstStringListRef &)=default
std::variant< const std::vector< std::string >, const std::vector< std::string > *, const std::vector< std::wstring > *, Adapter *, std::unique_ptr< Adapter > > Variant
ConstStringListRef(std::unique_ptr< AdapterType > adapter)
ConstStringListRef(const std::vector< std::wstring > *value)
ConstStringListRef(const ConstStringListRef &)=default
ConstStringListRef(Adapter *adapter)
std::string operator[](size_t i) const
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
ConstStringRef(const char *ref)
ConstStringRef(const std::wstring ref)
ConstStringRef(const std::wstring *ref)
ConstStringRef(const wchar_t *ref)
An adapter. Own or reference an mutable object.
const T & operator*() const
const T * operator->() const
Ref & operator=(Ref &&) noexcept=default
const T & operator()() const
An adapter. Own or reference a constant string. For convenience, this class convert multiple mutable ...
StringRef(const char *ref)
StringRef(const wchar_t *ref)
std::shared_ptr< T > Make(Args &&... args)
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.