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