• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_NG_PATTERNS_INDICATOR_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_INDICATOR_EVENT_HUB_H
18 
19 namespace OHOS::Ace::NG {
20 
21 using ChangeEvent = std::function<void(int32_t index)>;
22 using ChangeEventPtr = std::shared_ptr<ChangeEvent>;
23 
24 class IndicatorEventHub : public EventHub {
25     DECLARE_ACE_TYPE(IndicatorEventHub, EventHub);
26 
27 public:
28     IndicatorEventHub() = default;
29     ~IndicatorEventHub() override = default;
30 
31     /* Using shared_ptr to enable event modification without adding again */
AddOnChangeEvent(const ChangeEventPtr & changeEvent)32     void AddOnChangeEvent(const ChangeEventPtr& changeEvent)
33     {
34         changeEvents_.emplace_back(changeEvent);
35     }
36 
FireChangeEvent(int32_t index)37     void FireChangeEvent(int32_t index) const
38     {
39         if (!changeEvents_.empty()) {
40             std::for_each(changeEvents_.begin(), changeEvents_.end(), [index](const ChangeEventPtr& changeEvent) {
41                 auto event = *changeEvent;
42                 event(index);
43             });
44         }
45     }
46 
47 private:
48     std::list<ChangeEventPtr> changeEvents_;
49 };
50 
51 } // namespace OHOS::Ace::NG
52 
53 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_INDICATOR_EVENT_HUB_H