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_EVENT_ACE_EVENT_HELPER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENT_HELPER_H 18 19 #include <functional> 20 21 #include "base/log/log.h" 22 #include "base/memory/referenced.h" 23 #include "core/event/ace_event_handler.h" 24 #include "core/event/back_end_event_manager.h" 25 #include "core/pipeline/pipeline_context.h" 26 27 namespace OHOS::Ace { 28 29 template<class> 30 class AceSyncEvent; 31 32 template<class... Args> 33 class AceSyncEvent<void(Args...)> final { 34 public: 35 AceSyncEvent() = delete; 36 ~AceSyncEvent() = delete; 37 38 // Uses eventId and context to create Ace back-end event. 39 // For sync event, the result needs to be referenced by out parameters. 40 // When the event id starts with "Backend_" flag, then uses backend event manager processing. Create(const EventMarker & marker,const WeakPtr<PipelineContext> & context)41 static std::function<void(Args...)> Create(const EventMarker& marker, const WeakPtr<PipelineContext>& context) 42 { 43 if (marker.IsEmpty()) { 44 return nullptr; 45 } 46 return [marker, context](Args... args) { 47 auto refContext = context.Upgrade(); 48 if (!refContext) { 49 LOGE("fail to fire sync event due to context is nullptr"); 50 return; 51 } 52 refContext->FireSyncEvent(marker, std::forward<Args>(args)...); 53 }; 54 } 55 }; 56 57 template<class> 58 class AceAsyncEvent; 59 60 template<class... Args> 61 class AceAsyncEvent<void(Args...)> final { 62 public: 63 AceAsyncEvent() = delete; 64 ~AceAsyncEvent() = delete; 65 66 // Uses eventId and context to create Ace back-end event. 67 // When the event id starts with "Backend_" flag, then uses backend event manager processing. Create(const EventMarker & marker,const WeakPtr<PipelineContext> & context)68 static std::function<void(Args...)> Create(const EventMarker& marker, const WeakPtr<PipelineContext>& context) 69 { 70 if ((marker.IsEmpty()) && (!marker.HasPreFunction())) { 71 return nullptr; 72 } 73 return [marker, context](Args... args) { 74 auto refContext = context.Upgrade(); 75 if (!refContext) { 76 LOGE("fail to fire async event due to context is nullptr"); 77 return; 78 } 79 marker.CallPreFunction(); 80 if (marker.IsEmpty()) { 81 LOGI("just call pre function, fail to fire async event due to marker is empty"); 82 return; 83 } 84 refContext->FireAsyncEvent(marker, std::forward<Args>(args)...); 85 }; 86 } 87 }; 88 89 } // namespace OHOS::Ace 90 91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_ACE_EVENT_HELPER_H 92