18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#ifndef META_BASE_EXPECTED_H
168bf80f4bSopenharmony_ci#define META_BASE_EXPECTED_H
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <meta/base/interface_macros.h>
198bf80f4bSopenharmony_ci#include <meta/base/meta_types.h>
208bf80f4bSopenharmony_ci#include <meta/base/namespace.h>
218bf80f4bSopenharmony_ci
228bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_cienum GenericError : int16_t {
258bf80f4bSopenharmony_ci    SUCCESS = 0,
268bf80f4bSopenharmony_ci    FAIL = -1,
278bf80f4bSopenharmony_ci    INVALID_ARGUMENT = -2,
288bf80f4bSopenharmony_ci    INCOMPATIBLE_TYPES = -3,
298bf80f4bSopenharmony_ci    NOT_FOUND = -4,
308bf80f4bSopenharmony_ci    RECURSIVE_CALL = -5,
318bf80f4bSopenharmony_ci};
328bf80f4bSopenharmony_ci
338bf80f4bSopenharmony_citemplate<typename Type, typename Error>
348bf80f4bSopenharmony_ciclass Expected {
358bf80f4bSopenharmony_cipublic:
368bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
378bf80f4bSopenharmony_ci    constexpr Expected(Type t) : hasValue_(true), value_(BASE_NS::move(t)) {}
388bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
398bf80f4bSopenharmony_ci    constexpr Expected(Error e) : error_(BASE_NS::move(e)) {}
408bf80f4bSopenharmony_ci    ~Expected()
418bf80f4bSopenharmony_ci    {
428bf80f4bSopenharmony_ci        if (hasValue_) {
438bf80f4bSopenharmony_ci            value_.~Type();
448bf80f4bSopenharmony_ci        }
458bf80f4bSopenharmony_ci    }
468bf80f4bSopenharmony_ci    META_DEFAULT_COPY_MOVE(Expected)
478bf80f4bSopenharmony_ci
488bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
498bf80f4bSopenharmony_ci    constexpr operator bool() const
508bf80f4bSopenharmony_ci    {
518bf80f4bSopenharmony_ci        return hasValue_;
528bf80f4bSopenharmony_ci    }
538bf80f4bSopenharmony_ci
548bf80f4bSopenharmony_ci    constexpr const Type& operator*() const
558bf80f4bSopenharmony_ci    {
568bf80f4bSopenharmony_ci        return GetValue();
578bf80f4bSopenharmony_ci    }
588bf80f4bSopenharmony_ci    constexpr Type& operator*()
598bf80f4bSopenharmony_ci    {
608bf80f4bSopenharmony_ci        return GetValue();
618bf80f4bSopenharmony_ci    }
628bf80f4bSopenharmony_ci
638bf80f4bSopenharmony_ci    constexpr Error GetError() const
648bf80f4bSopenharmony_ci    {
658bf80f4bSopenharmony_ci        return !hasValue_ ? error_ : Error {};
668bf80f4bSopenharmony_ci    }
678bf80f4bSopenharmony_ci
688bf80f4bSopenharmony_ci    constexpr const Type& GetValue() const
698bf80f4bSopenharmony_ci    {
708bf80f4bSopenharmony_ci        CORE_ASSERT(hasValue_);
718bf80f4bSopenharmony_ci        return value_;
728bf80f4bSopenharmony_ci    }
738bf80f4bSopenharmony_ci    constexpr Type& GetValue()
748bf80f4bSopenharmony_ci    {
758bf80f4bSopenharmony_ci        CORE_ASSERT(hasValue_);
768bf80f4bSopenharmony_ci        return value_;
778bf80f4bSopenharmony_ci    }
788bf80f4bSopenharmony_ci
798bf80f4bSopenharmony_ciprivate:
808bf80f4bSopenharmony_ci    bool hasValue_ {};
818bf80f4bSopenharmony_ci    union {
828bf80f4bSopenharmony_ci        Error error_;
838bf80f4bSopenharmony_ci        Type value_;
848bf80f4bSopenharmony_ci    };
858bf80f4bSopenharmony_ci};
868bf80f4bSopenharmony_ci
878bf80f4bSopenharmony_ci// we don't have is_enum so for now just specialising for GenericError
888bf80f4bSopenharmony_citemplate<>
898bf80f4bSopenharmony_ciclass Expected<void, GenericError> {
908bf80f4bSopenharmony_cipublic:
918bf80f4bSopenharmony_ci    constexpr Expected() = default;
928bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
938bf80f4bSopenharmony_ci    constexpr Expected(GenericError e) : error_(e) {}
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
968bf80f4bSopenharmony_ci    constexpr operator bool() const
978bf80f4bSopenharmony_ci    {
988bf80f4bSopenharmony_ci        return error_ == 0;
998bf80f4bSopenharmony_ci    }
1008bf80f4bSopenharmony_ci
1018bf80f4bSopenharmony_ci    constexpr GenericError GetError() const
1028bf80f4bSopenharmony_ci    {
1038bf80f4bSopenharmony_ci        return error_;
1048bf80f4bSopenharmony_ci    }
1058bf80f4bSopenharmony_ci
1068bf80f4bSopenharmony_ciprivate:
1078bf80f4bSopenharmony_ci    GenericError error_ {};
1088bf80f4bSopenharmony_ci};
1098bf80f4bSopenharmony_ci
1108bf80f4bSopenharmony_ciusing ReturnError = Expected<void, GenericError>;
1118bf80f4bSopenharmony_ci
1128bf80f4bSopenharmony_citemplate<typename Enum>
1138bf80f4bSopenharmony_ciclass ReturnValue {
1148bf80f4bSopenharmony_cipublic:
1158bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
1168bf80f4bSopenharmony_ci    constexpr ReturnValue(Enum code) : code_(code) {}
1178bf80f4bSopenharmony_ci
1188bf80f4bSopenharmony_ci    /* NOLINTNEXTLINE(google-explicit-constructor) */
1198bf80f4bSopenharmony_ci    constexpr operator bool() const
1208bf80f4bSopenharmony_ci    {
1218bf80f4bSopenharmony_ci        return BASE_NS::underlying_type_t<Enum>(code_) > 0;
1228bf80f4bSopenharmony_ci    }
1238bf80f4bSopenharmony_ci
1248bf80f4bSopenharmony_ci    constexpr Enum GetCode() const
1258bf80f4bSopenharmony_ci    {
1268bf80f4bSopenharmony_ci        return code_;
1278bf80f4bSopenharmony_ci    }
1288bf80f4bSopenharmony_ci
1298bf80f4bSopenharmony_ci    bool operator==(Enum v) const
1308bf80f4bSopenharmony_ci    {
1318bf80f4bSopenharmony_ci        return code_ == v;
1328bf80f4bSopenharmony_ci    }
1338bf80f4bSopenharmony_ci
1348bf80f4bSopenharmony_ci    bool operator!=(Enum v) const
1358bf80f4bSopenharmony_ci    {
1368bf80f4bSopenharmony_ci        return code_ != v;
1378bf80f4bSopenharmony_ci    }
1388bf80f4bSopenharmony_ci
1398bf80f4bSopenharmony_ciprivate:
1408bf80f4bSopenharmony_ci    Enum code_ {};
1418bf80f4bSopenharmony_ci};
1428bf80f4bSopenharmony_ci
1438bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
1448bf80f4bSopenharmony_ci
1458bf80f4bSopenharmony_ci#endif
146