1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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"
// Used for template specialization.
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 {
// Implements stateful value_s. Similar to std::optional, but multi-state
// allowing nullable value_s.
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 {
// Indicates a valid value_; can be null.
kValue,
// Indicates an error, such as call or parser errors.
kError,
// Indicates a called property is not applicable to the object.
kNotApplicable,
// Indicates the property can't have an associated object.
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_;
};
} // namespace ui
#endif // UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_OPTIONAL_H_
|