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 168bf80f4bSopenharmony_ci#ifndef META_API_NUMBER_H 178bf80f4bSopenharmony_ci#define META_API_NUMBER_H 188bf80f4bSopenharmony_ci 198bf80f4bSopenharmony_ci#include <meta/base/interface_traits.h> 208bf80f4bSopenharmony_ci#include <meta/interface/detail/any.h> 218bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h> 228bf80f4bSopenharmony_ci 238bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 248bf80f4bSopenharmony_ci 258bf80f4bSopenharmony_citemplate<typename T> 268bf80f4bSopenharmony_ciusing EnableIfBasicType = 278bf80f4bSopenharmony_ci BASE_NS::enable_if_t<!IsSharedOrWeakPtr_v<T> && !BASE_NS::is_convertible_v<BASE_NS::remove_const_t<T&>, IAny&>>; 288bf80f4bSopenharmony_ci 298bf80f4bSopenharmony_ciREGISTER_CLASS(Number, "751c6067-4fb8-404b-a240-4fa6f32c725a", ObjectCategoryBits::NO_CATEGORY) 308bf80f4bSopenharmony_ci 318bf80f4bSopenharmony_ciclass Number { 328bf80f4bSopenharmony_cipublic: 338bf80f4bSopenharmony_ci Number() : number_(META_NS::GetObjectRegistry().Create<IAny>(META_NS::ClassId::Number)) {} 348bf80f4bSopenharmony_ci 358bf80f4bSopenharmony_ci template<typename T, typename = EnableIfBasicType<T>> 368bf80f4bSopenharmony_ci Number(T t) : Number() 378bf80f4bSopenharmony_ci { 388bf80f4bSopenharmony_ci if constexpr (is_enum_v<T>) { 398bf80f4bSopenharmony_ci number_->SetValue(static_cast<BASE_NS::underlying_type_t<T>>(t)); 408bf80f4bSopenharmony_ci } else { 418bf80f4bSopenharmony_ci number_->SetValue(t); 428bf80f4bSopenharmony_ci } 438bf80f4bSopenharmony_ci } 448bf80f4bSopenharmony_ci 458bf80f4bSopenharmony_ci Number(IAny::ConstPtr any) : Number() 468bf80f4bSopenharmony_ci { 478bf80f4bSopenharmony_ci number_->CopyFrom(*any); 488bf80f4bSopenharmony_ci } 498bf80f4bSopenharmony_ci 508bf80f4bSopenharmony_ci Number(const IAny& any) : Number() 518bf80f4bSopenharmony_ci { 528bf80f4bSopenharmony_ci number_->CopyFrom(any); 538bf80f4bSopenharmony_ci } 548bf80f4bSopenharmony_ci 558bf80f4bSopenharmony_ci template<typename T, typename = EnableIfBasicType<T>> 568bf80f4bSopenharmony_ci Number& operator=(T t) 578bf80f4bSopenharmony_ci { 588bf80f4bSopenharmony_ci if constexpr (is_enum_v<T>) { 598bf80f4bSopenharmony_ci number_->SetValue(static_cast<BASE_NS::underlying_type_t<T>>(t)); 608bf80f4bSopenharmony_ci } else { 618bf80f4bSopenharmony_ci number_->SetValue(t); 628bf80f4bSopenharmony_ci } 638bf80f4bSopenharmony_ci return *this; 648bf80f4bSopenharmony_ci } 658bf80f4bSopenharmony_ci 668bf80f4bSopenharmony_ci Number& operator=(IAny::ConstPtr any) 678bf80f4bSopenharmony_ci { 688bf80f4bSopenharmony_ci number_->CopyFrom(*any); 698bf80f4bSopenharmony_ci return *this; 708bf80f4bSopenharmony_ci } 718bf80f4bSopenharmony_ci 728bf80f4bSopenharmony_ci Number& operator=(const IAny& any) 738bf80f4bSopenharmony_ci { 748bf80f4bSopenharmony_ci number_->CopyFrom(any); 758bf80f4bSopenharmony_ci return *this; 768bf80f4bSopenharmony_ci } 778bf80f4bSopenharmony_ci 788bf80f4bSopenharmony_ci template<typename T> 798bf80f4bSopenharmony_ci T Get() const 808bf80f4bSopenharmony_ci { 818bf80f4bSopenharmony_ci T t; 828bf80f4bSopenharmony_ci if constexpr (is_enum_v<T>) { 838bf80f4bSopenharmony_ci BASE_NS::underlying_type_t<T> ut; 848bf80f4bSopenharmony_ci number_->GetValue(ut); 858bf80f4bSopenharmony_ci t = static_cast<T>(ut); 868bf80f4bSopenharmony_ci } else { 878bf80f4bSopenharmony_ci number_->GetValue(t); 888bf80f4bSopenharmony_ci } 898bf80f4bSopenharmony_ci return t; 908bf80f4bSopenharmony_ci } 918bf80f4bSopenharmony_ci 928bf80f4bSopenharmony_ciprivate: 938bf80f4bSopenharmony_ci IAny::Ptr number_; 948bf80f4bSopenharmony_ci}; 958bf80f4bSopenharmony_ci 968bf80f4bSopenharmony_ciMETA_END_NAMESPACE() 978bf80f4bSopenharmony_ci 988bf80f4bSopenharmony_ci#endif 99