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_VERSION_H 168bf80f4bSopenharmony_ci#define META_BASE_VERSION_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/namespace.h> 228bf80f4bSopenharmony_ci 238bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 248bf80f4bSopenharmony_ci 258bf80f4bSopenharmony_ciclass Version { 268bf80f4bSopenharmony_cipublic: 278bf80f4bSopenharmony_ci constexpr Version(uint16_t major = 0, uint16_t minor = 0) : major_(major), minor_(minor) {} 288bf80f4bSopenharmony_ci 298bf80f4bSopenharmony_ci constexpr explicit Version(BASE_NS::string_view str) 308bf80f4bSopenharmony_ci { 318bf80f4bSopenharmony_ci size_t pos = ParseInt(str, major_); 328bf80f4bSopenharmony_ci if (pos != 0 && pos + 1 < str.size()) { 338bf80f4bSopenharmony_ci ParseInt(str.substr(pos + 1), minor_); 348bf80f4bSopenharmony_ci } 358bf80f4bSopenharmony_ci } 368bf80f4bSopenharmony_ci 378bf80f4bSopenharmony_ci constexpr uint16_t Major() const 388bf80f4bSopenharmony_ci { 398bf80f4bSopenharmony_ci return major_; 408bf80f4bSopenharmony_ci } 418bf80f4bSopenharmony_ci constexpr uint16_t Minor() const 428bf80f4bSopenharmony_ci { 438bf80f4bSopenharmony_ci return minor_; 448bf80f4bSopenharmony_ci } 458bf80f4bSopenharmony_ci 468bf80f4bSopenharmony_ci BASE_NS::string ToString() const 478bf80f4bSopenharmony_ci { 488bf80f4bSopenharmony_ci return BASE_NS::string(BASE_NS::to_string(major_)) + "." + BASE_NS::string(BASE_NS::to_string(minor_)); 498bf80f4bSopenharmony_ci } 508bf80f4bSopenharmony_ci 518bf80f4bSopenharmony_ciprivate: 528bf80f4bSopenharmony_ci constexpr size_t ParseInt(BASE_NS::string_view str, uint16_t& out) 538bf80f4bSopenharmony_ci { 548bf80f4bSopenharmony_ci size_t i = 0; 558bf80f4bSopenharmony_ci while (i < str.size() && str[i] >= '0' && str[i] <= '9') { 568bf80f4bSopenharmony_ci if (i) { 578bf80f4bSopenharmony_ci out *= 10; 588bf80f4bSopenharmony_ci } 598bf80f4bSopenharmony_ci out += str[i++] - '0'; 608bf80f4bSopenharmony_ci } 618bf80f4bSopenharmony_ci return i; 628bf80f4bSopenharmony_ci } 638bf80f4bSopenharmony_ci 648bf80f4bSopenharmony_ciprivate: 658bf80f4bSopenharmony_ci uint16_t major_ {}; 668bf80f4bSopenharmony_ci uint16_t minor_ {}; 678bf80f4bSopenharmony_ci}; 688bf80f4bSopenharmony_ci 698bf80f4bSopenharmony_ciconstexpr inline bool operator==(const Version& l, const Version& r) 708bf80f4bSopenharmony_ci{ 718bf80f4bSopenharmony_ci return l.Major() == r.Major() && l.Minor() == r.Minor(); 728bf80f4bSopenharmony_ci} 738bf80f4bSopenharmony_ci 748bf80f4bSopenharmony_ciconstexpr inline bool operator!=(const Version& l, const Version& r) 758bf80f4bSopenharmony_ci{ 768bf80f4bSopenharmony_ci return !(l == r); 778bf80f4bSopenharmony_ci} 788bf80f4bSopenharmony_ci 798bf80f4bSopenharmony_ciconstexpr inline bool operator<(const Version& l, const Version& r) 808bf80f4bSopenharmony_ci{ 818bf80f4bSopenharmony_ci return l.Major() < r.Major() || (l.Major() == r.Major() && l.Minor() < r.Minor()); 828bf80f4bSopenharmony_ci} 838bf80f4bSopenharmony_ci 848bf80f4bSopenharmony_ciconstexpr inline bool operator<=(const Version& l, const Version& r) 858bf80f4bSopenharmony_ci{ 868bf80f4bSopenharmony_ci return l == r || l < r; 878bf80f4bSopenharmony_ci} 888bf80f4bSopenharmony_ci 898bf80f4bSopenharmony_ciconstexpr inline bool operator>(const Version& l, const Version& r) 908bf80f4bSopenharmony_ci{ 918bf80f4bSopenharmony_ci return r < l; 928bf80f4bSopenharmony_ci} 938bf80f4bSopenharmony_ci 948bf80f4bSopenharmony_ciconstexpr inline bool operator>=(const Version& l, const Version& r) 958bf80f4bSopenharmony_ci{ 968bf80f4bSopenharmony_ci return r <= l; 978bf80f4bSopenharmony_ci} 988bf80f4bSopenharmony_ci 998bf80f4bSopenharmony_ciMETA_END_NAMESPACE() 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_ci#endif 102