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 = new (std::nothrow) UserObject(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 = new UserObject(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 
107 /**
108  * @brief Registered user-defined serialization class.
109  * @param objectName The name of the custom class.
110  * @param createFun Function object that creates an instance of a custom class.
111  */
RegisterUserObject(const std::string & objectName,const CreateUserObjectBase & createFun)112 void UserObjectBaseLoader::RegisterUserObject(const std::string &objectName, const CreateUserObjectBase &createFun)
113 {
114     register_class_list_.emplace(objectName, createFun);
115 }
116 
117 /**
118  * @brief Represents obtaining an instance of an object of a registered serialization class.
119  * @param className The name of the custom class.
120  *
121  * @return Returns an instance of the object, or nullptr on failure.
122  */
GetUserObjectByName(const std::string & className)123 UserObjectBase *UserObjectBaseLoader::GetUserObjectByName(const std::string &className)
124 {
125     auto iter = register_class_list_.find(className);
126     if (iter != register_class_list_.end()) {
127         return iter->second();
128     } else {
129         return nullptr;
130     }
131 }
132 }  // namespace AAFwk
133 }  // namespace OHOS