1 /*
2 * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_COMMON_COMMON_DEF_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_COMMON_COMMON_DEF_H
18
19 #include <functional>
20 #include <memory>
21
22 #include "base/memory/referenced.h"
23
24 #define ACE_DEFINE_COMPONENT_EVENT(name, func) \
25 private: \
26 std::unique_ptr<std::function<func>> event##name##_; \
27 public: \
28 const std::unique_ptr<std::function<func>>& Get##name() const \
29 { \
30 return event##name##_; \
31 } \
32 void Set##name(std::function<func>&& event##name) \
33 { \
34 if (event##name) { \
35 event##name##_ = std::make_unique<std::function<func>>(std::move(event##name)); \
36 } else { \
37 event##name##_.reset(); \
38 } \
39 }
40
41 #define ACE_DEFINE_COMPONENT_PROP(name, type, ...) \
42 private: \
43 type prop##name##_ { __VA_ARGS__ }; \
44 public: \
45 type Get##name() const \
46 { \
47 return prop##name##_; \
48 } \
49 void Set##name(const type& prop##name) \
50 { \
51 prop##name##_ = prop##name; \
52 }
53
54 namespace OHOS::Ace {
55 namespace V1 = ::OHOS::Ace;
56 namespace V2 {
57
58 template<class C, class R, class... Args>
ResumeEventCallback(const RefPtr<C> & component,const std::unique_ptr<std::function<R (Args...)>> & (C::* getMethod)()const,R defValue,const Args &...args)59 R ResumeEventCallback(const RefPtr<C>& component,
60 const std::unique_ptr<std::function<R(Args...)>>& (C::*getMethod)() const, R defValue, const Args&... args)
61 {
62 const auto& funcPtr = ((*component).*getMethod)();
63 return (funcPtr && (*funcPtr)) ? (*funcPtr)(args...) : defValue;
64 }
65
66 template<class C, class... Args>
ResumeEventCallback(const RefPtr<C> & component,const std::unique_ptr<std::function<void (Args...)>> & (C::* getMethod)()const,const Args &...args)67 void ResumeEventCallback(const RefPtr<C>& component,
68 const std::unique_ptr<std::function<void(Args...)>>& (C::*getMethod)() const, const Args&... args)
69 {
70 const auto& funcPtr = ((*component).*getMethod)();
71 if (funcPtr && (*funcPtr)) {
72 (*funcPtr)(args...);
73 }
74 }
75
76 } // namespace V2
77 } // namespace OHOS::Ace
78
79 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_COMMON_COMMON_DEF_H
80