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 #include "user_object_wrapper.h"
16 #include "user_object_base.h"
17
18 namespace OHOS {
19 namespace AAFwk {
20 namespace {
21 const std::string SPLIT = "#";
22 };
23 IINTERFACE_IMPL_1(UserObject, Object, IUserObject);
24
GetValue(std::shared_ptr<UserObjectBase> &value)25 ErrCode UserObject::GetValue(std::shared_ptr<UserObjectBase> &value)
26 {
27 value = value_;
28 return ERR_OK;
29 }
Equals(IObject &other)30 bool UserObject::Equals(IObject &other)
31 {
32 if (value_ == nullptr) {
33 return false;
34 }
35
36 UserObject *otherObj = static_cast<UserObject *>(IUserObject::Query(&other));
37 if (otherObj == nullptr) {
38 return false;
39 }
40 if (value_->GetClassName() == otherObj->value_->GetClassName()) {
41 return otherObj->value_->Equals(value_);
42 }
43 return false;
44 }
45
ToString()46 std::string UserObject::ToString()
47 {
48 if (value_ == nullptr) {
49 return std::string("");
50 }
51 return value_->GetClassName() + SPLIT + value_->ToString();
52 }
53
Box(const std::shared_ptr<UserObjectBase> &value)54 sptr<IUserObject> UserObject::Box(const std::shared_ptr<UserObjectBase> &value)
55 {
56 if (value != nullptr) {
57 sptr<IUserObject> object = sptr<UserObject>::MakeSptr(value);
58 return object;
59 } else {
60 return nullptr;
61 }
62 }
63
Unbox(IUserObject *object)64 std::shared_ptr<UserObjectBase> UserObject::Unbox(IUserObject *object)
65 {
66 std::shared_ptr<UserObjectBase> value = nullptr;
67 if (object == nullptr) {
68 return nullptr;
69 }
70
71 object->GetValue(value);
72 return value;
73 }
74
Parse(const std::string &str)75 sptr<IUserObject> UserObject::Parse(const std::string &str)
76 {
77 std::size_t len = str.length();
78 if (len < 1) {
79 return nullptr;
80 }
81 std::size_t splitPos = str.find(SPLIT);
82 if (splitPos == std::string::npos) {
83 return nullptr;
84 }
85 std::string className = str.substr(0, splitPos);
86 std::string content = str.substr(className.length() + 1, len - 1);
87 if (className.length() + SPLIT.length() + content.length() != len) {
88 return nullptr;
89 }
90
91 UserObjectBase *userObjectBase =
92 static_cast<UserObjectBase *>(UserObjectBaseLoader::GetInstance().GetUserObjectByName(className));
93 if (userObjectBase != nullptr) {
94 userObjectBase->Parse(content);
95 sptr<IUserObject> ret = sptr<UserObject>::MakeSptr(std::shared_ptr<UserObjectBase>(userObjectBase));
96 return ret;
97 }
98 return nullptr;
99 }
100
GetInstance(void)101 UserObjectBaseLoader &UserObjectBaseLoader::GetInstance(void)
102 {
103 static UserObjectBaseLoader gUserObjectBaseLoader;
104 return gUserObjectBaseLoader;
105 }
106
RegisterUserObject(const std::string &objectName, const CreateUserObjectBase &createFun)107 void UserObjectBaseLoader::RegisterUserObject(const std::string &objectName, const CreateUserObjectBase &createFun)
108 {
109 registerClassList_.emplace(objectName, createFun);
110 }
111
GetUserObjectByName(const std::string &className)112 UserObjectBase *UserObjectBaseLoader::GetUserObjectByName(const std::string &className)
113 {
114 auto iter = registerClassList_.find(className);
115 if (iter != registerClassList_.end()) {
116 return iter->second();
117 } else {
118 return nullptr;
119 }
120 }
121 } // namespace AAFwk
122 } // namespace OHOS
123