• 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_INTERFACE_EVENT_H
17 #define META_INTERFACE_EVENT_H
18 
19 #include <base/containers/shared_ptr.h>
20 
21 #include <meta/base/namespace.h>
22 
23 META_BEGIN_NAMESPACE()
24 class IEvent;
25 class ICallable;
26 template<typename EventT>
27 class Event;
28 template<>
29 class Event<IEvent> {
30 public:
31     using EventType = IEvent;
32     using ValueType = BASE_NS::shared_ptr<IEvent>;
33 
event_(ev)34     Event(const ValueType& ev = nullptr) : event_(ev) {}
35 
36     ValueType operator->() const
37     {
38         return event_;
39     }
40     IEvent& operator*() const
41     {
42         return *event_;
43     }
ValueType()44     operator ValueType() const
45     {
46         return event_;
47     }
48     explicit operator bool() const
49     {
50         return event_ != nullptr;
51     }
GetEvent()52     ValueType GetEvent() const
53     {
54         return event_;
55     }
56 
57 protected:
58     BASE_NS::shared_ptr<IEvent> event_;
59 };
60 template<typename EventT>
61 class Event : public Event<IEvent> {
62 public:
63     using Event<IEvent>::Event;
64     using ValueType = BASE_NS::shared_ptr<EventT>;
65 
66     Event(const ValueType& ev = nullptr) : Event<IEvent>(interface_pointer_cast<IEvent>(ev)) {}
GetCallable()67     BASE_NS::shared_ptr<ICallable> GetCallable() const
68     {
69         return interface_pointer_cast<ICallable>(event_);
70     }
71 };
72 
73 META_END_NAMESPACE()
74 
75 #endif
76