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_IDS_H
168bf80f4bSopenharmony_ci#define META_BASE_IDS_H
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <base/containers/string.h>
198bf80f4bSopenharmony_ci#include <base/util/uid_util.h>
208bf80f4bSopenharmony_ci
218bf80f4bSopenharmony_ci#include <meta/base/meta_types.h>
228bf80f4bSopenharmony_ci#include <meta/base/namespace.h>
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
258bf80f4bSopenharmony_ci
268bf80f4bSopenharmony_citemplate<typename>
278bf80f4bSopenharmony_ciclass IdBase {
288bf80f4bSopenharmony_cipublic:
298bf80f4bSopenharmony_ci    constexpr IdBase(const BASE_NS::Uid& id = {}) noexcept : id_(id) {}
308bf80f4bSopenharmony_ci
318bf80f4bSopenharmony_ci    explicit constexpr IdBase(const char (&str)[37]) noexcept : id_(str) {}
328bf80f4bSopenharmony_ci
338bf80f4bSopenharmony_ci    BASE_NS::string ToString() const noexcept
348bf80f4bSopenharmony_ci    {
358bf80f4bSopenharmony_ci        return BASE_NS::string(BASE_NS::to_string(id_));
368bf80f4bSopenharmony_ci    }
378bf80f4bSopenharmony_ci
388bf80f4bSopenharmony_ci    constexpr BASE_NS::Uid ToUid() const noexcept
398bf80f4bSopenharmony_ci    {
408bf80f4bSopenharmony_ci        return id_;
418bf80f4bSopenharmony_ci    }
428bf80f4bSopenharmony_ci
438bf80f4bSopenharmony_ci    constexpr bool IsValid() const noexcept
448bf80f4bSopenharmony_ci    {
458bf80f4bSopenharmony_ci        return id_.data[0] && id_.data[1];
468bf80f4bSopenharmony_ci    }
478bf80f4bSopenharmony_ci
488bf80f4bSopenharmony_ci    constexpr bool operator==(const IdBase& r) const noexcept
498bf80f4bSopenharmony_ci    {
508bf80f4bSopenharmony_ci        return id_ == r.id_;
518bf80f4bSopenharmony_ci    }
528bf80f4bSopenharmony_ci    constexpr bool operator!=(const IdBase& r) const noexcept
538bf80f4bSopenharmony_ci    {
548bf80f4bSopenharmony_ci        return id_ != r.id_;
558bf80f4bSopenharmony_ci    }
568bf80f4bSopenharmony_ci    constexpr bool operator<(const IdBase& r) const noexcept
578bf80f4bSopenharmony_ci    {
588bf80f4bSopenharmony_ci        return id_ < r.id_;
598bf80f4bSopenharmony_ci    }
608bf80f4bSopenharmony_ci    constexpr int Compare(const IdBase& r) const noexcept
618bf80f4bSopenharmony_ci    {
628bf80f4bSopenharmony_ci        return id_.compare(r.id_);
638bf80f4bSopenharmony_ci    }
648bf80f4bSopenharmony_ci
658bf80f4bSopenharmony_ciprotected:
668bf80f4bSopenharmony_ci    BASE_NS::Uid id_;
678bf80f4bSopenharmony_ci};
688bf80f4bSopenharmony_ci
698bf80f4bSopenharmony_ciclass TypeId : public IdBase<TypeId> {
708bf80f4bSopenharmony_cipublic:
718bf80f4bSopenharmony_ci    using IdBase<TypeId>::IdBase;
728bf80f4bSopenharmony_ci};
738bf80f4bSopenharmony_ci
748bf80f4bSopenharmony_ciclass ObjectId : public IdBase<ObjectId> {
758bf80f4bSopenharmony_cipublic:
768bf80f4bSopenharmony_ci    using IdBase<ObjectId>::IdBase;
778bf80f4bSopenharmony_ci};
788bf80f4bSopenharmony_ci
798bf80f4bSopenharmony_ciclass InstanceId : public IdBase<InstanceId> {
808bf80f4bSopenharmony_cipublic:
818bf80f4bSopenharmony_ci    using IdBase<InstanceId>::IdBase;
828bf80f4bSopenharmony_ci};
838bf80f4bSopenharmony_ci
848bf80f4bSopenharmony_ciMETA_TYPE(TypeId);
858bf80f4bSopenharmony_ciMETA_TYPE(ObjectId);
868bf80f4bSopenharmony_ciMETA_TYPE(InstanceId);
878bf80f4bSopenharmony_ci
888bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
898bf80f4bSopenharmony_ci
908bf80f4bSopenharmony_ciBASE_BEGIN_NAMESPACE()
918bf80f4bSopenharmony_citemplate<>
928bf80f4bSopenharmony_ciinline uint64_t hash(const META_NS::TypeId& value)
938bf80f4bSopenharmony_ci{
948bf80f4bSopenharmony_ci    return hash(value.ToUid());
958bf80f4bSopenharmony_ci}
968bf80f4bSopenharmony_citemplate<>
978bf80f4bSopenharmony_ciinline uint64_t hash(const META_NS::ObjectId& value)
988bf80f4bSopenharmony_ci{
998bf80f4bSopenharmony_ci    return hash(value.ToUid());
1008bf80f4bSopenharmony_ci}
1018bf80f4bSopenharmony_citemplate<>
1028bf80f4bSopenharmony_ciinline uint64_t hash(const META_NS::InstanceId& value)
1038bf80f4bSopenharmony_ci{
1048bf80f4bSopenharmony_ci    return hash(value.ToUid());
1058bf80f4bSopenharmony_ci}
1068bf80f4bSopenharmony_ciBASE_END_NAMESPACE()
1078bf80f4bSopenharmony_ci
1088bf80f4bSopenharmony_ci#endif
109