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