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 "int_wrapper.h" 17 18 #include "ability_base_log_wrapper.h" 19 20 namespace OHOS { 21 namespace AAFwk { 22 IINTERFACE_IMPL_1(Integer, Object, IInteger); 23 GetValue(int & value)24ErrCode Integer::GetValue(int &value) /* [out] */ 25 { 26 VALIDATE_NOT_NULL(&value); 27 28 value = value_; 29 return ERR_OK; 30 } 31 Equals(IObject & other)32bool Integer::Equals(IObject &other) /* [in] */ 33 { 34 Integer *otherObj = static_cast<Integer *>(IInteger::Query(&other)); 35 return otherObj != nullptr && otherObj->value_ == value_; 36 } 37 ToString()38std::string Integer::ToString() 39 { 40 return std::to_string(value_); 41 } 42 Box(int value)43sptr<IInteger> Integer::Box(int value) /* [in] */ 44 { 45 sptr<IInteger> object = sptr<Integer>::MakeSptr(value); 46 return object; 47 } 48 Unbox(IInteger * object)49int Integer::Unbox(IInteger *object) /* [in] */ 50 { 51 int value; 52 object->GetValue(value); 53 return value; 54 } 55 Parse(const std::string & str)56sptr<IInteger> Integer::Parse(const std::string &str) /* [in] */ 57 { 58 sptr<IInteger> object; 59 std::size_t idx; 60 try { 61 int value = std::stoi(str, &idx); 62 if (idx != 0) { 63 object = sptr<Integer>::MakeSptr(value); 64 } 65 } catch (...) { 66 ABILITYBASE_LOGE("failed to convert to int: %{public}s", str.c_str()); 67 } 68 return object; 69 } 70 } // namespace AAFwk 71 } // namespace OHOS 72