#ifndef UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_OPTIONAL_H_
#define UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_OPTIONAL_H_
#include <string>
#include <variant>
#include "base/component_export.h"
#include "build/build_config.h"
template <typename T>
struct is_variant : std::false_type {};
template <typename... Args>
struct is_variant<std::variant<Args...>> : std::true_type {};
template <typename T>
inline constexpr bool is_variant_v = is_variant<T>::value;
namespace ui {
template <typename ValueType>
class COMPONENT_EXPORT(AX_PLATFORM) AXOptional final {
public:
static constexpr AXOptional Unsupported() { return AXOptional(kUnsupported); }
static constexpr AXOptional Error(const char* error_text = nullptr) {
return error_text ? AXOptional(kError, error_text) : AXOptional(kError);
}
static constexpr AXOptional Error(const std::string& error_text) {
return AXOptional(kError, error_text);
}
static constexpr AXOptional NotApplicable() {
return AXOptional(kNotApplicable);
}
static constexpr AXOptional NotNullOrError(ValueType other_value_) {
return AXOptional(other_value_, other_value_ != nullptr ? kValue : kError);
}
static constexpr AXOptional NotNullOrNotApplicable(ValueType other_value_) {
return AXOptional(other_value_,
other_value_ != nullptr ? kValue : kNotApplicable);
}
explicit constexpr AXOptional(const ValueType& value_)
: value_(value_), state_(kValue) {}
explicit constexpr AXOptional(ValueType&& value_)
: value_(std::forward<ValueType>(value_)), state_(kValue) {}
bool constexpr IsUnsupported() const { return state_ == kUnsupported; }
bool constexpr IsNotApplicable() const { return state_ == kNotApplicable; }
bool constexpr IsError() const { return state_ == kError; }
template <typename T = ValueType>
bool constexpr IsNotNull() const {
if constexpr (!is_variant_v<T>) {
return value_ != nullptr;
}
return true;
}
bool constexpr HasValue() const { return state_ == kValue; }
constexpr const ValueType& operator*() const { return value_; }
constexpr const ValueType* operator->() const { return &value_; }
bool HasStateText() const { return !state_text_.empty(); }
std::string StateText() const { return state_text_; }
std::string ToString() const {
if (IsNotNull())
return "<value>";
return StateToString();
}
std::string StateToString() const {
if (IsNotApplicable())
return "<n/a>";
if (IsUnsupported())
return "<unsupported>";
if (IsError())
return "<error>";
if (!IsNotNull())
return "<null>";
return "";
}
private:
enum State {
kValue,
kError,
kNotApplicable,
kUnsupported,
};
template <typename T = ValueType>
requires(!is_variant_v<T>)
explicit constexpr AXOptional(State state, const std::string& state_text = {})
: value_(nullptr), state_(state), state_text_(state_text) {}
template <typename T = ValueType>
requires(is_variant_v<T>)
explicit constexpr AXOptional(State state, const std::string& state_text = {})
: value_(std::monostate()), state_(state), state_text_(state_text) {}
explicit constexpr AXOptional(ValueType value, State state)
: value_(value), state_(state) {}
ValueType value_;
State state_;
std::string state_text_;
};
}
#endif