1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "float_wrapper.h" 17 18 #include "ability_base_log_wrapper.h" 19 20 namespace OHOS { 21 namespace AAFwk { 22 IINTERFACE_IMPL_1(Float, Object, IFloat); 23 GetValue(float & value)24ErrCode Float::GetValue(float &value) /* [out] */ 25 { 26 VALIDATE_NOT_NULL(&value); 27 28 value = value_; 29 return ERR_OK; 30 } 31 Equals(IObject & other)32bool Float::Equals(IObject &other) /* [in] */ 33 { 34 Float *otherObj = static_cast<Float *>(IFloat::Query(&other)); 35 return otherObj != nullptr && otherObj->value_ == value_; 36 } 37 ToString()38std::string Float::ToString() 39 { 40 return std::to_string(value_); 41 } 42 Box(float value)43sptr<IFloat> Float::Box(float value) /* [in] */ 44 { 45 sptr<IFloat> object = new Float(value); 46 return object; 47 } 48 Unbox(IFloat * object)49float Float::Unbox(IFloat *object) /* [in] */ 50 { 51 float value; 52 object->GetValue(value); 53 return value; 54 } 55 Parse(const std::string & str)56sptr<IFloat> Float::Parse(const std::string &str) /* [in] */ 57 { 58 sptr<IFloat> object; 59 std::size_t idx; 60 try { 61 float value = std::stof(str, &idx); 62 if (idx != 0) { 63 object = sptr<Float>::MakeSptr(value); 64 } 65 } catch (...) { 66 ABILITYBASE_LOGE("failed to convert to float: %{public}s", str.c_str()); 67 } 68 return object; 69 } 70 } // namespace AAFwk 71 } // namespace OHOS