• 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 META_API_PROPERTY_ARRAY_PROPERTY_EVENT_HANDLER_H
17 #define META_API_PROPERTY_ARRAY_PROPERTY_EVENT_HANDLER_H
18 
19 #include <meta/api/property/array_property_changes_recognizer.h>
20 #include <meta/api/property/property_event_handler.h>
21 #include <meta/interface/property/array_property.h>
22 #include <meta/interface/simple_event.h>
23 
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25 
26 /// Helper to have events with array property element changes
27 template<typename ValueType>
28 class ArrayPropertyChangedEventHandler {
29 public:
30     ArrayPropertyChangedEventHandler() = default;
31 
32     using Property = ArrayProperty<ValueType>;
33     using ArrayChange = ArrayChanges<ValueType>;
34     using FunctionType = void(const ArrayChange&);
35 
36     template<typename T>
37     bool Subscribe(const Property& property, T* instance, void (T::*callback)(const ArrayChange&))
38     {
39         changesRecognizer_.SetValue(property);
40         return handler_.Subscribe(
41             property, [=]() { (instance->*callback)(changesRecognizer_.OnArrayPropertyChanged(property)); });
42     }
43 
44     bool Subscribe(const Property& property, void (*callback)(const ArrayChange&))
45     {
46         changesRecognizer_.SetValue(property);
47         return handler_.Subscribe(property, [=]() { callback(changesRecognizer_.OnArrayPropertyChanged(property)); });
48     }
49 
50     template<typename Func, typename = EnableIfCanInvokeWithArguments<Func, FunctionType>>
51     bool Subscribe(const Property& property, Func func, const ITaskQueue::Ptr& queue = nullptr)
52     {
53         changesRecognizer_.SetValue(property);
54         return handler_.Subscribe(
55             property,
56             [this, property, f = BASE_NS::move(func)]() { f(changesRecognizer_.OnArrayPropertyChanged(property)); },
57             queue);
58     }
59 
60     template<typename Func, typename = EnableIfCanInvokeWithArguments<Func, FunctionType>>
61     bool Subscribe(const Property& property, Func func, const BASE_NS::Uid& queueId)
62     {
63         changesRecognizer_.SetValue(property);
64         return handler_.Subscribe(
65             property,
66             [this, property, f = BASE_NS::move(func)]() { f(changesRecognizer_.OnArrayPropertyChanged(property)); },
67             queueId);
68     }
69 
70     void Unsubscribe()
71     {
72         handler_.Unsubscribe();
73         changesRecognizer_.Clear();
74     }
75 
76 private:
77     ArrayPropertyChangesRecognizer<ValueType> changesRecognizer_;
78     META_NS::PropertyChangedEventHandler handler_;
79 };
80 
81 META_END_NAMESPACE()
82 
83 #endif
84