• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef OHOS_CAMERA_DPS_ENABLE_SHARED_CREATE_H
17 #define OHOS_CAMERA_DPS_ENABLE_SHARED_CREATE_H
18 
19 #include <memory>
20 
21 namespace OHOS {
22 namespace CameraStandard {
23 namespace DeferredProcessing {
24 template <typename Class>
25 class EnableSharedCreate {
26 public:
27     virtual ~EnableSharedCreate() = default;
28 
29     template <typename... Args>
Create(Args &&...args)30     static std::shared_ptr<Class> Create(Args&&...args)
31     {
32         struct MakeSharedHelper : Class {
33             explicit MakeSharedHelper(Args&&...args) : Class(std::forward<Args>(args)...) {}
34         };
35 
36         std::shared_ptr<Class> sharedObj = std::make_shared<MakeSharedHelper>(std::forward<Args>(args)...);
37         EnableSharedFromThis(sharedObj, static_cast<EnableSharedCreate*>(sharedObj.get()));
38         return sharedObj;
39     }
40 
shared_from_this()41     std::shared_ptr<Class> shared_from_this()
42     {
43         return std::shared_ptr<Class>(this->weakThis_);
44     }
45 
shared_from_this()46     std::shared_ptr<const Class> shared_from_this() const
47     {
48         return std::shared_ptr<const Class>(this->weakThis_);
49     }
50 
weak_from_this()51     std::weak_ptr<Class> weak_from_this() noexcept
52     {
53         return this->weakThis_;
54     }
55 
weak_from_this()56     std::weak_ptr<const Class> weak_from_this() const noexcept
57     {
58         return this->weakThis_;
59     }
60 
61 protected:
InitWeakThis(const std::shared_ptr<Class> & weak)62     inline void InitWeakThis(const std::shared_ptr<Class>& weak)
63     {
64         weakThis_ = weak;
65     }
66 
67 private:
68     template<typename X, typename Y>
69     friend void EnableSharedFromThis(const std::shared_ptr<X>& sp, Y* base);
70 
71     mutable std::weak_ptr<Class> weakThis_;
72 };
73 
74 template<typename X, typename Y>
EnableSharedFromThis(const std::shared_ptr<X> & sp,Y * base)75 void EnableSharedFromThis(const std::shared_ptr<X>& sp, Y* base)
76 {
77     if (base != nullptr) {
78         base->InitWeakThis(sp);
79     }
80 }
81 
82 template <typename Class>
83 class EnableSharedCreateInit : public EnableSharedCreate<Class> {
84 public:
85     template <typename... Args>
Create(Args &&...args)86     static std::shared_ptr<Class> Create(Args&&...args)
87     {
88         auto sharedObj = EnableSharedCreate<Class>::Create(std::forward<Args>(args)...);
89         if (sharedObj && (sharedObj->Initialize() != 0)) {
90             sharedObj = nullptr;
91         }
92         return sharedObj;
93     }
94 
95     virtual int32_t Initialize() = 0;
96 };
97 } // namespace DeferredProcessing
98 } // namespace CameraStandard
99 } // namespace OHOS
100 #endif // OHOS_CAMERA_DPS_ENABLE_SHARED_CREATE_H