• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "reflect_registration.h"
17 #include "media_log.h"
18 bool g_logOn = false;
19 namespace OHOS {
20 namespace Sharing {
21 
CreateObject(const std::string & descriptor)22 std::shared_ptr<void> ReflectRegistration::CreateObject(const std::string &descriptor)
23 {
24     std::lock_guard<std::mutex> lockGuard(creatorMutex_);
25     if (creators_.find(descriptor) == creators_.end()) {
26         return nullptr;
27     }
28 
29     return creators_[descriptor]();
30 }
31 
Register(const std::string & descriptor,Constructor creator)32 bool ReflectRegistration::Register(const std::string &descriptor, Constructor creator)
33 {
34     if (descriptor.empty()) {
35         return false;
36     }
37 
38     std::lock_guard<std::mutex> lockGuard(creatorMutex_);
39     auto it = creators_.find(descriptor);
40     if (it == creators_.end()) {
41         return creators_.insert({descriptor, creator}).second;
42     }
43 
44     return false;
45 }
46 
Unregister(const std::string & descriptor)47 void ReflectRegistration::Unregister(const std::string &descriptor)
48 {
49     std::lock_guard<std::mutex> lockGuard(creatorMutex_);
50     if (!descriptor.empty()) {
51         auto it = creators_.find(descriptor);
52         if (it != creators_.end()) {
53             creators_.erase(it);
54         }
55     }
56 }
57 
~ReflectRegistration()58 ReflectRegistration::~ReflectRegistration()
59 {
60     std::lock_guard<std::mutex> lockGuard(creatorMutex_);
61     for (auto it = creators_.begin(); it != creators_.end();) {
62         it = creators_.erase(it);
63     }
64 }
65 
66 } // namespace Sharing
67 } // namespace OHOS