1100ae2f9Sopenharmony_ci/*
2100ae2f9Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3100ae2f9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4100ae2f9Sopenharmony_ci * you may not use this file except in compliance with the License.
5100ae2f9Sopenharmony_ci * You may obtain a copy of the License at
6100ae2f9Sopenharmony_ci *
7100ae2f9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8100ae2f9Sopenharmony_ci *
9100ae2f9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10100ae2f9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11100ae2f9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12100ae2f9Sopenharmony_ci * See the License for the specific language governing permissions and
13100ae2f9Sopenharmony_ci * limitations under the License.
14100ae2f9Sopenharmony_ci */
15100ae2f9Sopenharmony_ci
16100ae2f9Sopenharmony_ci#ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_INNER_EVENT_H
17100ae2f9Sopenharmony_ci#define BASE_EVENTHANDLER_INTERFACES_INNER_API_INNER_EVENT_H
18100ae2f9Sopenharmony_ci
19100ae2f9Sopenharmony_ci#include <cstdint>
20100ae2f9Sopenharmony_ci#include <chrono>
21100ae2f9Sopenharmony_ci#include <functional>
22100ae2f9Sopenharmony_ci#include <memory>
23100ae2f9Sopenharmony_ci#include <string>
24100ae2f9Sopenharmony_ci#include <typeinfo>
25100ae2f9Sopenharmony_ci#include <variant>
26100ae2f9Sopenharmony_ci
27100ae2f9Sopenharmony_ci#include "nocopyable.h"
28100ae2f9Sopenharmony_ci
29100ae2f9Sopenharmony_cinamespace OHOS {
30100ae2f9Sopenharmony_cinamespace HiviewDFX {
31100ae2f9Sopenharmony_ciclass HiTraceId;
32100ae2f9Sopenharmony_ci}
33100ae2f9Sopenharmony_ci
34100ae2f9Sopenharmony_cinamespace AppExecFwk {
35100ae2f9Sopenharmony_ciconstexpr static uint32_t TYPE_U32_INDEX = 0u;
36100ae2f9Sopenharmony_ciusing HiTraceId = OHOS::HiviewDFX::HiTraceId;
37100ae2f9Sopenharmony_ci
38100ae2f9Sopenharmony_ciclass EventHandler;
39100ae2f9Sopenharmony_ci
40100ae2f9Sopenharmony_ciconst std::string LINE_SEPARATOR = "\n";
41100ae2f9Sopenharmony_ci
42100ae2f9Sopenharmony_cistruct Caller {
43100ae2f9Sopenharmony_ci    std::string file_ {""};
44100ae2f9Sopenharmony_ci    int         line_ {0};
45100ae2f9Sopenharmony_ci    std::string func_ {""};
46100ae2f9Sopenharmony_ci    std::string dfxName_ {""};
47100ae2f9Sopenharmony_ci#if __has_builtin(__builtin_FILE)
48100ae2f9Sopenharmony_ci    Caller(std::string file = __builtin_FILE(), int line = __builtin_LINE(),
49100ae2f9Sopenharmony_ci           std::string func = __builtin_FUNCTION())
50100ae2f9Sopenharmony_ci        : file_(file), line_(line), func_(func) {}
51100ae2f9Sopenharmony_ci#else
52100ae2f9Sopenharmony_ci    Caller() {}
53100ae2f9Sopenharmony_ci#endif
54100ae2f9Sopenharmony_ci    std::string ToString() const
55100ae2f9Sopenharmony_ci    {
56100ae2f9Sopenharmony_ci        if (file_.empty()) {
57100ae2f9Sopenharmony_ci            return std::string("[ ]");
58100ae2f9Sopenharmony_ci        }
59100ae2f9Sopenharmony_ci        size_t split = file_.find_last_of("/\\");
60100ae2f9Sopenharmony_ci        if (split == std::string::npos) {
61100ae2f9Sopenharmony_ci            split = 0;
62100ae2f9Sopenharmony_ci        }
63100ae2f9Sopenharmony_ci        std::string caller("[" + file_.substr(split + 1) + "(" + func_ + ":" + std::to_string(line_) +
64100ae2f9Sopenharmony_ci            dfxName_ + ")]");
65100ae2f9Sopenharmony_ci        return caller;
66100ae2f9Sopenharmony_ci    }
67100ae2f9Sopenharmony_ci
68100ae2f9Sopenharmony_ci    void ClearCaller()
69100ae2f9Sopenharmony_ci    {
70100ae2f9Sopenharmony_ci        file_ = "";
71100ae2f9Sopenharmony_ci        func_ = "";
72100ae2f9Sopenharmony_ci        line_ = 0;
73100ae2f9Sopenharmony_ci    }
74100ae2f9Sopenharmony_ci};
75100ae2f9Sopenharmony_ci
76100ae2f9Sopenharmony_ciclass InnerEvent final {
77100ae2f9Sopenharmony_cipublic:
78100ae2f9Sopenharmony_ci    using Clock = std::chrono::steady_clock;
79100ae2f9Sopenharmony_ci    using TimePoint = std::chrono::time_point<Clock>;
80100ae2f9Sopenharmony_ci    using Callback = std::function<void()>;
81100ae2f9Sopenharmony_ci    using Pointer = std::unique_ptr<InnerEvent, void (*)(InnerEvent *)>;
82100ae2f9Sopenharmony_ci    using EventId = std::variant<uint32_t, std::string>;
83100ae2f9Sopenharmony_ci    class Waiter {
84100ae2f9Sopenharmony_ci    public:
85100ae2f9Sopenharmony_ci        Waiter() = default;
86100ae2f9Sopenharmony_ci        virtual ~Waiter() = default;
87100ae2f9Sopenharmony_ci        DISALLOW_COPY_AND_MOVE(Waiter);
88100ae2f9Sopenharmony_ci
89100ae2f9Sopenharmony_ci        virtual void Wait() = 0;
90100ae2f9Sopenharmony_ci        virtual void Notify() = 0;
91100ae2f9Sopenharmony_ci    };
92100ae2f9Sopenharmony_ci
93100ae2f9Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(InnerEvent);
94100ae2f9Sopenharmony_ci
95100ae2f9Sopenharmony_ci    /**
96100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
97100ae2f9Sopenharmony_ci     *
98100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
99100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
100100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
101100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
102100ae2f9Sopenharmony_ci     */
103100ae2f9Sopenharmony_ci    static Pointer Get(uint32_t innerEventId, int64_t param = 0, const Caller &caller = {});
104100ae2f9Sopenharmony_ci
105100ae2f9Sopenharmony_ci    /**
106100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
107100ae2f9Sopenharmony_ci     *
108100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
109100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
110100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
111100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
112100ae2f9Sopenharmony_ci     */
113100ae2f9Sopenharmony_ci    static Pointer Get(const EventId &innerEventId, int64_t param = 0, const Caller &caller = {});
114100ae2f9Sopenharmony_ci
115100ae2f9Sopenharmony_ci    /**
116100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
117100ae2f9Sopenharmony_ci     *
118100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
119100ae2f9Sopenharmony_ci     * @param object Shared pointer of the object.
120100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
121100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
122100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
123100ae2f9Sopenharmony_ci     */
124100ae2f9Sopenharmony_ci    template<typename T>
125100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, const std::shared_ptr<T> &object,
126100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
127100ae2f9Sopenharmony_ci    {
128100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
129100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
130100ae2f9Sopenharmony_ci        return event;
131100ae2f9Sopenharmony_ci    }
132100ae2f9Sopenharmony_ci
133100ae2f9Sopenharmony_ci    /**
134100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
135100ae2f9Sopenharmony_ci     *
136100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
137100ae2f9Sopenharmony_ci     * @param object Shared pointer of the object.
138100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
139100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
140100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
141100ae2f9Sopenharmony_ci     */
142100ae2f9Sopenharmony_ci    template<typename T>
143100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, const std::shared_ptr<T> &object,
144100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
145100ae2f9Sopenharmony_ci    {
146100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
147100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
148100ae2f9Sopenharmony_ci        return event;
149100ae2f9Sopenharmony_ci    }
150100ae2f9Sopenharmony_ci
151100ae2f9Sopenharmony_ci    /**
152100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
153100ae2f9Sopenharmony_ci     *
154100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
155100ae2f9Sopenharmony_ci     * @param object Weak pointer of the object.
156100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
157100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
158100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
159100ae2f9Sopenharmony_ci     */
160100ae2f9Sopenharmony_ci    template<typename T>
161100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, const std::weak_ptr<T> &object,
162100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
163100ae2f9Sopenharmony_ci    {
164100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
165100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
166100ae2f9Sopenharmony_ci        return event;
167100ae2f9Sopenharmony_ci    }
168100ae2f9Sopenharmony_ci
169100ae2f9Sopenharmony_ci    /**
170100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
171100ae2f9Sopenharmony_ci     *
172100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
173100ae2f9Sopenharmony_ci     * @param object Weak pointer of the object.
174100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
175100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
176100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
177100ae2f9Sopenharmony_ci     */
178100ae2f9Sopenharmony_ci    template<typename T>
179100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, const std::weak_ptr<T> &object,
180100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
181100ae2f9Sopenharmony_ci    {
182100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
183100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
184100ae2f9Sopenharmony_ci        return event;
185100ae2f9Sopenharmony_ci    }
186100ae2f9Sopenharmony_ci
187100ae2f9Sopenharmony_ci    /**
188100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
189100ae2f9Sopenharmony_ci     *
190100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
191100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
192100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
193100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
194100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
195100ae2f9Sopenharmony_ci     */
196100ae2f9Sopenharmony_ci    template<typename T, typename D>
197100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, std::unique_ptr<T, D> &&object,
198100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
199100ae2f9Sopenharmony_ci    {
200100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
201100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
202100ae2f9Sopenharmony_ci        return event;
203100ae2f9Sopenharmony_ci    }
204100ae2f9Sopenharmony_ci
205100ae2f9Sopenharmony_ci    /**
206100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
207100ae2f9Sopenharmony_ci     *
208100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
209100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
210100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
211100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
212100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
213100ae2f9Sopenharmony_ci     */
214100ae2f9Sopenharmony_ci    template<typename T, typename D>
215100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, std::unique_ptr<T, D> &&object,
216100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
217100ae2f9Sopenharmony_ci    {
218100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
219100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
220100ae2f9Sopenharmony_ci        return event;
221100ae2f9Sopenharmony_ci    }
222100ae2f9Sopenharmony_ci
223100ae2f9Sopenharmony_ci    /**
224100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
225100ae2f9Sopenharmony_ci     *
226100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
227100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
228100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
229100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
230100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
231100ae2f9Sopenharmony_ci     */
232100ae2f9Sopenharmony_ci    template<typename T, typename D>
233100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, std::unique_ptr<T, D> &object,
234100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
235100ae2f9Sopenharmony_ci    {
236100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
237100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
238100ae2f9Sopenharmony_ci        return event;
239100ae2f9Sopenharmony_ci    }
240100ae2f9Sopenharmony_ci
241100ae2f9Sopenharmony_ci    /**
242100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
243100ae2f9Sopenharmony_ci     *
244100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
245100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
246100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event, default is 0.
247100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
248100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
249100ae2f9Sopenharmony_ci     */
250100ae2f9Sopenharmony_ci    template<typename T, typename D>
251100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, std::unique_ptr<T, D> &object,
252100ae2f9Sopenharmony_ci                              int64_t param = 0, const Caller &caller = {})
253100ae2f9Sopenharmony_ci    {
254100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
255100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
256100ae2f9Sopenharmony_ci        return event;
257100ae2f9Sopenharmony_ci    }
258100ae2f9Sopenharmony_ci
259100ae2f9Sopenharmony_ci    /**
260100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
261100ae2f9Sopenharmony_ci     *
262100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
263100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
264100ae2f9Sopenharmony_ci     * @param object Shared pointer of the object.
265100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
266100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
267100ae2f9Sopenharmony_ci     */
268100ae2f9Sopenharmony_ci    template<typename T>
269100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, int64_t param, const std::shared_ptr<T> &object,
270100ae2f9Sopenharmony_ci                              const Caller &caller = {})
271100ae2f9Sopenharmony_ci    {
272100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
273100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
274100ae2f9Sopenharmony_ci        return event;
275100ae2f9Sopenharmony_ci    }
276100ae2f9Sopenharmony_ci
277100ae2f9Sopenharmony_ci    /**
278100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
279100ae2f9Sopenharmony_ci     *
280100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
281100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
282100ae2f9Sopenharmony_ci     * @param object Shared pointer of the object.
283100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
284100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
285100ae2f9Sopenharmony_ci     */
286100ae2f9Sopenharmony_ci    template<typename T>
287100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, int64_t param, const std::shared_ptr<T> &object,
288100ae2f9Sopenharmony_ci                              const Caller &caller = {})
289100ae2f9Sopenharmony_ci    {
290100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
291100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
292100ae2f9Sopenharmony_ci        return event;
293100ae2f9Sopenharmony_ci    }
294100ae2f9Sopenharmony_ci
295100ae2f9Sopenharmony_ci    /**
296100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
297100ae2f9Sopenharmony_ci     *
298100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
299100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
300100ae2f9Sopenharmony_ci     * @param object Weak pointer of the object.
301100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
302100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
303100ae2f9Sopenharmony_ci     */
304100ae2f9Sopenharmony_ci    template<typename T>
305100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, int64_t param, const std::weak_ptr<T> &object,
306100ae2f9Sopenharmony_ci                              const Caller &caller = {})
307100ae2f9Sopenharmony_ci    {
308100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
309100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
310100ae2f9Sopenharmony_ci        return event;
311100ae2f9Sopenharmony_ci    }
312100ae2f9Sopenharmony_ci
313100ae2f9Sopenharmony_ci    /**
314100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
315100ae2f9Sopenharmony_ci     *
316100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
317100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
318100ae2f9Sopenharmony_ci     * @param object Weak pointer of the object.
319100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
320100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
321100ae2f9Sopenharmony_ci     */
322100ae2f9Sopenharmony_ci    template<typename T>
323100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, int64_t param, const std::weak_ptr<T> &object,
324100ae2f9Sopenharmony_ci                              const Caller &caller = {})
325100ae2f9Sopenharmony_ci    {
326100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
327100ae2f9Sopenharmony_ci        event->SaveSharedPtr(object);
328100ae2f9Sopenharmony_ci        return event;
329100ae2f9Sopenharmony_ci    }
330100ae2f9Sopenharmony_ci
331100ae2f9Sopenharmony_ci    /**
332100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
333100ae2f9Sopenharmony_ci     *
334100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
335100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
336100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
337100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
338100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
339100ae2f9Sopenharmony_ci     */
340100ae2f9Sopenharmony_ci    template<typename T, typename D>
341100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, int64_t param, std::unique_ptr<T, D> &&object,
342100ae2f9Sopenharmony_ci                              const Caller &caller = {})
343100ae2f9Sopenharmony_ci    {
344100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
345100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
346100ae2f9Sopenharmony_ci        return event;
347100ae2f9Sopenharmony_ci    }
348100ae2f9Sopenharmony_ci
349100ae2f9Sopenharmony_ci    /**
350100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
351100ae2f9Sopenharmony_ci     *
352100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
353100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
354100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
355100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
356100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
357100ae2f9Sopenharmony_ci     */
358100ae2f9Sopenharmony_ci    template<typename T, typename D>
359100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, int64_t param, std::unique_ptr<T, D> &&object,
360100ae2f9Sopenharmony_ci                              const Caller &caller = {})
361100ae2f9Sopenharmony_ci    {
362100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
363100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
364100ae2f9Sopenharmony_ci        return event;
365100ae2f9Sopenharmony_ci    }
366100ae2f9Sopenharmony_ci
367100ae2f9Sopenharmony_ci    /**
368100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
369100ae2f9Sopenharmony_ci     *
370100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
371100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
372100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
373100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
374100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
375100ae2f9Sopenharmony_ci     */
376100ae2f9Sopenharmony_ci    template<typename T, typename D>
377100ae2f9Sopenharmony_ci    static inline Pointer Get(uint32_t innerEventId, int64_t param, std::unique_ptr<T, D> &object,
378100ae2f9Sopenharmony_ci                              const Caller &caller = {})
379100ae2f9Sopenharmony_ci    {
380100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
381100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
382100ae2f9Sopenharmony_ci        return event;
383100ae2f9Sopenharmony_ci    }
384100ae2f9Sopenharmony_ci
385100ae2f9Sopenharmony_ci    /**
386100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
387100ae2f9Sopenharmony_ci     *
388100ae2f9Sopenharmony_ci     * @param innerEventId The id of the event.
389100ae2f9Sopenharmony_ci     * @param param Basic parameter of the event.
390100ae2f9Sopenharmony_ci     * @param object Unique pointer of the object.
391100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
392100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance.
393100ae2f9Sopenharmony_ci     */
394100ae2f9Sopenharmony_ci    template<typename T, typename D>
395100ae2f9Sopenharmony_ci    static inline Pointer Get(const EventId &innerEventId, int64_t param, std::unique_ptr<T, D> &object,
396100ae2f9Sopenharmony_ci                              const Caller &caller = {})
397100ae2f9Sopenharmony_ci    {
398100ae2f9Sopenharmony_ci        auto event = Get(innerEventId, param, caller);
399100ae2f9Sopenharmony_ci        event->SaveUniquePtr(object);
400100ae2f9Sopenharmony_ci        return event;
401100ae2f9Sopenharmony_ci    }
402100ae2f9Sopenharmony_ci
403100ae2f9Sopenharmony_ci    /**
404100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
405100ae2f9Sopenharmony_ci     *
406100ae2f9Sopenharmony_ci     * @param callback Callback for task.
407100ae2f9Sopenharmony_ci     * @param name Name of task.
408100ae2f9Sopenharmony_ci     * @param caller Caller info of the event, default is caller's file, func and line.
409100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance, if callback is invalid, returns nullptr object.
410100ae2f9Sopenharmony_ci     */
411100ae2f9Sopenharmony_ci    static Pointer Get(const Callback &callback, const std::string &name = std::string(),
412100ae2f9Sopenharmony_ci                       const Caller &caller = {});
413100ae2f9Sopenharmony_ci
414100ae2f9Sopenharmony_ci    /**
415100ae2f9Sopenharmony_ci     * Get InnerEvent instance from pool.
416100ae2f9Sopenharmony_ci     *
417100ae2f9Sopenharmony_ci     * @return Returns the pointer of InnerEvent instance
418100ae2f9Sopenharmony_ci     */
419100ae2f9Sopenharmony_ci    static Pointer Get();
420100ae2f9Sopenharmony_ci
421100ae2f9Sopenharmony_ci    /**
422100ae2f9Sopenharmony_ci     * Get owner of the event.
423100ae2f9Sopenharmony_ci     *
424100ae2f9Sopenharmony_ci     * @return Returns owner of the event after it has been sent.
425100ae2f9Sopenharmony_ci     */
426100ae2f9Sopenharmony_ci    inline std::shared_ptr<EventHandler> GetOwner() const
427100ae2f9Sopenharmony_ci    {
428100ae2f9Sopenharmony_ci        return owner_.lock();
429100ae2f9Sopenharmony_ci    }
430100ae2f9Sopenharmony_ci
431100ae2f9Sopenharmony_ci    /**
432100ae2f9Sopenharmony_ci     * Get weak owner of the event.
433100ae2f9Sopenharmony_ci     *
434100ae2f9Sopenharmony_ci     * @return Returns owner of the event after it has been sent.
435100ae2f9Sopenharmony_ci     */
436100ae2f9Sopenharmony_ci    inline std::weak_ptr<EventHandler> GetWeakOwner() const
437100ae2f9Sopenharmony_ci    {
438100ae2f9Sopenharmony_ci        return owner_;
439100ae2f9Sopenharmony_ci    }
440100ae2f9Sopenharmony_ci
441100ae2f9Sopenharmony_ci    /**
442100ae2f9Sopenharmony_ci     * Set owner of the event.
443100ae2f9Sopenharmony_ci     *
444100ae2f9Sopenharmony_ci     * @param owner Owner for the event.
445100ae2f9Sopenharmony_ci     */
446100ae2f9Sopenharmony_ci    inline void SetOwner(const std::shared_ptr<EventHandler> &owner)
447100ae2f9Sopenharmony_ci    {
448100ae2f9Sopenharmony_ci        owner_ = owner;
449100ae2f9Sopenharmony_ci    }
450100ae2f9Sopenharmony_ci
451100ae2f9Sopenharmony_ci    /**
452100ae2f9Sopenharmony_ci     * Get handle time of the event.
453100ae2f9Sopenharmony_ci     *
454100ae2f9Sopenharmony_ci     * @return Returns handle time of the event after it has been sent.
455100ae2f9Sopenharmony_ci     */
456100ae2f9Sopenharmony_ci    inline const TimePoint &GetHandleTime() const
457100ae2f9Sopenharmony_ci    {
458100ae2f9Sopenharmony_ci        return handleTime_;
459100ae2f9Sopenharmony_ci    }
460100ae2f9Sopenharmony_ci
461100ae2f9Sopenharmony_ci    /**
462100ae2f9Sopenharmony_ci     * Set handle time of the event.
463100ae2f9Sopenharmony_ci     *
464100ae2f9Sopenharmony_ci     * @param handleTime Handle time of the event.
465100ae2f9Sopenharmony_ci     */
466100ae2f9Sopenharmony_ci    inline void SetHandleTime(const TimePoint &handleTime)
467100ae2f9Sopenharmony_ci    {
468100ae2f9Sopenharmony_ci        handleTime_ = handleTime;
469100ae2f9Sopenharmony_ci    }
470100ae2f9Sopenharmony_ci
471100ae2f9Sopenharmony_ci    /**
472100ae2f9Sopenharmony_ci     * Get send time of the event.
473100ae2f9Sopenharmony_ci     *
474100ae2f9Sopenharmony_ci     * @return Returns send time of the event after it has been sent.
475100ae2f9Sopenharmony_ci     */
476100ae2f9Sopenharmony_ci    inline const TimePoint &GetSendTime() const
477100ae2f9Sopenharmony_ci    {
478100ae2f9Sopenharmony_ci        return sendTime_;
479100ae2f9Sopenharmony_ci    }
480100ae2f9Sopenharmony_ci
481100ae2f9Sopenharmony_ci    /**
482100ae2f9Sopenharmony_ci     * Set send time of the event.
483100ae2f9Sopenharmony_ci     *
484100ae2f9Sopenharmony_ci     * @param sendTime Send time of the event.
485100ae2f9Sopenharmony_ci     */
486100ae2f9Sopenharmony_ci    inline void SetSendTime(const TimePoint &sendTime)
487100ae2f9Sopenharmony_ci    {
488100ae2f9Sopenharmony_ci        sendTime_ = sendTime;
489100ae2f9Sopenharmony_ci    }
490100ae2f9Sopenharmony_ci
491100ae2f9Sopenharmony_ci    /**
492100ae2f9Sopenharmony_ci     * Set send kernel thread id of the event.
493100ae2f9Sopenharmony_ci     *
494100ae2f9Sopenharmony_ci     * @param senderKernelThreadId Send kernel thread id of the event
495100ae2f9Sopenharmony_ci     */
496100ae2f9Sopenharmony_ci    inline void SetSenderKernelThreadId(uint64_t senderKernelThreadId)
497100ae2f9Sopenharmony_ci    {
498100ae2f9Sopenharmony_ci        senderKernelThreadId_ = senderKernelThreadId;
499100ae2f9Sopenharmony_ci    }
500100ae2f9Sopenharmony_ci
501100ae2f9Sopenharmony_ci    /**
502100ae2f9Sopenharmony_ci     * Get the kernel thread id of the event.
503100ae2f9Sopenharmony_ci     *
504100ae2f9Sopenharmony_ci     * @return Returns kernel thread id of the event after it has been sent.
505100ae2f9Sopenharmony_ci     */
506100ae2f9Sopenharmony_ci    inline uint64_t GetSenderKernelThreadId()
507100ae2f9Sopenharmony_ci    {
508100ae2f9Sopenharmony_ci        return senderKernelThreadId_;
509100ae2f9Sopenharmony_ci    }
510100ae2f9Sopenharmony_ci
511100ae2f9Sopenharmony_ci    /**
512100ae2f9Sopenharmony_ci     * Get id of the event.
513100ae2f9Sopenharmony_ci     * Make sure {@link #hasTask} returns false.
514100ae2f9Sopenharmony_ci     *
515100ae2f9Sopenharmony_ci     * @return Returns id of the event after it has been sent.
516100ae2f9Sopenharmony_ci     */
517100ae2f9Sopenharmony_ci    inline uint32_t GetInnerEventId() const
518100ae2f9Sopenharmony_ci    {
519100ae2f9Sopenharmony_ci        if (innerEventId_.index() != TYPE_U32_INDEX) {
520100ae2f9Sopenharmony_ci            return 0u;
521100ae2f9Sopenharmony_ci        }
522100ae2f9Sopenharmony_ci        return std::get<uint32_t>(innerEventId_);
523100ae2f9Sopenharmony_ci    }
524100ae2f9Sopenharmony_ci
525100ae2f9Sopenharmony_ci    /**
526100ae2f9Sopenharmony_ci     * Get id of the event.
527100ae2f9Sopenharmony_ci     * Make sure {@link #hasTask} returns false.
528100ae2f9Sopenharmony_ci     *
529100ae2f9Sopenharmony_ci     * @return Returns id of the event after it has been sent.
530100ae2f9Sopenharmony_ci     */
531100ae2f9Sopenharmony_ci    inline EventId GetInnerEventIdEx() const
532100ae2f9Sopenharmony_ci    {
533100ae2f9Sopenharmony_ci        return innerEventId_;
534100ae2f9Sopenharmony_ci    }
535100ae2f9Sopenharmony_ci
536100ae2f9Sopenharmony_ci    /**
537100ae2f9Sopenharmony_ci     * Get basic param of the event.
538100ae2f9Sopenharmony_ci     * Make sure {@link #hasTask} returns false.
539100ae2f9Sopenharmony_ci     *
540100ae2f9Sopenharmony_ci     * @return Returns basic param of the event after it has been sent.
541100ae2f9Sopenharmony_ci     */
542100ae2f9Sopenharmony_ci    inline int64_t GetParam() const
543100ae2f9Sopenharmony_ci    {
544100ae2f9Sopenharmony_ci        return param_;
545100ae2f9Sopenharmony_ci    }
546100ae2f9Sopenharmony_ci
547100ae2f9Sopenharmony_ci    /**
548100ae2f9Sopenharmony_ci     * Get saved object.
549100ae2f9Sopenharmony_ci     *
550100ae2f9Sopenharmony_ci     * @return Returns shared pointer of saved object.
551100ae2f9Sopenharmony_ci     */
552100ae2f9Sopenharmony_ci    template<typename T>
553100ae2f9Sopenharmony_ci    std::shared_ptr<T> GetSharedObject() const
554100ae2f9Sopenharmony_ci    {
555100ae2f9Sopenharmony_ci        const std::shared_ptr<T> &sharedObject = *reinterpret_cast<const std::shared_ptr<T> *>(smartPtr_);
556100ae2f9Sopenharmony_ci        if (CalculateSmartPtrTypeId(sharedObject) == smartPtrTypeId_) {
557100ae2f9Sopenharmony_ci            return sharedObject;
558100ae2f9Sopenharmony_ci        }
559100ae2f9Sopenharmony_ci
560100ae2f9Sopenharmony_ci        const std::weak_ptr<T> &weakObject = *reinterpret_cast<const std::weak_ptr<T> *>(smartPtr_);
561100ae2f9Sopenharmony_ci        if (CalculateSmartPtrTypeId(weakObject) == smartPtrTypeId_) {
562100ae2f9Sopenharmony_ci            return weakObject.lock();
563100ae2f9Sopenharmony_ci        }
564100ae2f9Sopenharmony_ci
565100ae2f9Sopenharmony_ci        WarnSmartPtrCastMismatch();
566100ae2f9Sopenharmony_ci        return nullptr;
567100ae2f9Sopenharmony_ci    }
568100ae2f9Sopenharmony_ci
569100ae2f9Sopenharmony_ci    /**
570100ae2f9Sopenharmony_ci     * Get saved object.
571100ae2f9Sopenharmony_ci     *
572100ae2f9Sopenharmony_ci     * @return Returns unique pointer of saved object.
573100ae2f9Sopenharmony_ci     */
574100ae2f9Sopenharmony_ci    template<typename T>
575100ae2f9Sopenharmony_ci    std::unique_ptr<T> GetUniqueObject() const
576100ae2f9Sopenharmony_ci    {
577100ae2f9Sopenharmony_ci        std::unique_ptr<T> &object = *reinterpret_cast<std::unique_ptr<T> *>(smartPtr_);
578100ae2f9Sopenharmony_ci        if (CalculateSmartPtrTypeId(object) == smartPtrTypeId_) {
579100ae2f9Sopenharmony_ci            return std::move(object);
580100ae2f9Sopenharmony_ci        }
581100ae2f9Sopenharmony_ci
582100ae2f9Sopenharmony_ci        WarnSmartPtrCastMismatch();
583100ae2f9Sopenharmony_ci        return nullptr;
584100ae2f9Sopenharmony_ci    }
585100ae2f9Sopenharmony_ci
586100ae2f9Sopenharmony_ci    /**
587100ae2f9Sopenharmony_ci     * Get saved object.
588100ae2f9Sopenharmony_ci     *
589100ae2f9Sopenharmony_ci     * @return Returns unique pointer of saved object.
590100ae2f9Sopenharmony_ci     */
591100ae2f9Sopenharmony_ci    template<typename T, typename D>
592100ae2f9Sopenharmony_ci    std::unique_ptr<T, D> GetUniqueObject() const
593100ae2f9Sopenharmony_ci    {
594100ae2f9Sopenharmony_ci        std::unique_ptr<T, D> &object = *reinterpret_cast<std::unique_ptr<T, D> *>(smartPtr_);
595100ae2f9Sopenharmony_ci        if (CalculateSmartPtrTypeId(object) == smartPtrTypeId_) {
596100ae2f9Sopenharmony_ci            return std::move(object);
597100ae2f9Sopenharmony_ci        }
598100ae2f9Sopenharmony_ci
599100ae2f9Sopenharmony_ci        WarnSmartPtrCastMismatch();
600100ae2f9Sopenharmony_ci        return std::unique_ptr<T, D>(nullptr, nullptr);
601100ae2f9Sopenharmony_ci    }
602100ae2f9Sopenharmony_ci
603100ae2f9Sopenharmony_ci    /**
604100ae2f9Sopenharmony_ci     * Get task name.
605100ae2f9Sopenharmony_ci     * Make sure {@link #hasTask} returns true.
606100ae2f9Sopenharmony_ci     *
607100ae2f9Sopenharmony_ci     * @return Returns the task name.
608100ae2f9Sopenharmony_ci     */
609100ae2f9Sopenharmony_ci    inline const std::string &GetTaskName() const
610100ae2f9Sopenharmony_ci    {
611100ae2f9Sopenharmony_ci        return taskName_;
612100ae2f9Sopenharmony_ci    }
613100ae2f9Sopenharmony_ci
614100ae2f9Sopenharmony_ci    /**
615100ae2f9Sopenharmony_ci     * Get task callback.
616100ae2f9Sopenharmony_ci     * Make sure {@link #hasTask} returns true.
617100ae2f9Sopenharmony_ci     *
618100ae2f9Sopenharmony_ci     * @return Returns the callback of the task.
619100ae2f9Sopenharmony_ci     */
620100ae2f9Sopenharmony_ci    inline const Callback &GetTaskCallback() const
621100ae2f9Sopenharmony_ci    {
622100ae2f9Sopenharmony_ci        return taskCallback_;
623100ae2f9Sopenharmony_ci    }
624100ae2f9Sopenharmony_ci
625100ae2f9Sopenharmony_ci    /**
626100ae2f9Sopenharmony_ci     * Get task caller info.
627100ae2f9Sopenharmony_ci     *
628100ae2f9Sopenharmony_ci     * @return Returns the caller info of the task.
629100ae2f9Sopenharmony_ci     */
630100ae2f9Sopenharmony_ci    inline Caller &GetCaller()
631100ae2f9Sopenharmony_ci    {
632100ae2f9Sopenharmony_ci        return caller_;
633100ae2f9Sopenharmony_ci    }
634100ae2f9Sopenharmony_ci
635100ae2f9Sopenharmony_ci    /**
636100ae2f9Sopenharmony_ci     * Obtains the Runnable task that will be executed when this InnerEvent is handled.
637100ae2f9Sopenharmony_ci     *
638100ae2f9Sopenharmony_ci     * @return Returns the callback of the task.
639100ae2f9Sopenharmony_ci     */
640100ae2f9Sopenharmony_ci    inline const Callback &GetTask() const
641100ae2f9Sopenharmony_ci    {
642100ae2f9Sopenharmony_ci        return GetTaskCallback();
643100ae2f9Sopenharmony_ci    }
644100ae2f9Sopenharmony_ci
645100ae2f9Sopenharmony_ci    /**
646100ae2f9Sopenharmony_ci     * Check whether it takes a task callback in event.
647100ae2f9Sopenharmony_ci     *
648100ae2f9Sopenharmony_ci     * @return Returns true if it takes a task callback.
649100ae2f9Sopenharmony_ci     */
650100ae2f9Sopenharmony_ci    inline bool HasTask() const
651100ae2f9Sopenharmony_ci    {
652100ae2f9Sopenharmony_ci        return static_cast<bool>(taskCallback_);
653100ae2f9Sopenharmony_ci    }
654100ae2f9Sopenharmony_ci
655100ae2f9Sopenharmony_ci    /**
656100ae2f9Sopenharmony_ci     * Convert TimePoint to human readable string.
657100ae2f9Sopenharmony_ci     *
658100ae2f9Sopenharmony_ci     * @param time object represent time
659100ae2f9Sopenharmony_ci     */
660100ae2f9Sopenharmony_ci    static std::string DumpTimeToString(const TimePoint &time);
661100ae2f9Sopenharmony_ci
662100ae2f9Sopenharmony_ci    /**
663100ae2f9Sopenharmony_ci     * Convert std::chrono::system_clock::time_point to human readable string.
664100ae2f9Sopenharmony_ci     *
665100ae2f9Sopenharmony_ci     * @param time object represent time
666100ae2f9Sopenharmony_ci     */
667100ae2f9Sopenharmony_ci    static std::string DumpTimeToString(const std::chrono::system_clock::time_point &time);
668100ae2f9Sopenharmony_ci
669100ae2f9Sopenharmony_ci    /**
670100ae2f9Sopenharmony_ci     * Prints out the internal information about an object in the specified format,
671100ae2f9Sopenharmony_ci     * helping you diagnose internal errors of the object.
672100ae2f9Sopenharmony_ci     *
673100ae2f9Sopenharmony_ci     * @param return The content of the event.
674100ae2f9Sopenharmony_ci     */
675100ae2f9Sopenharmony_ci    std::string Dump();
676100ae2f9Sopenharmony_ci
677100ae2f9Sopenharmony_ci    /**
678100ae2f9Sopenharmony_ci     * Prints out the internal information about an object in the specified format,
679100ae2f9Sopenharmony_ci     * helping you diagnose internal errors of the object.
680100ae2f9Sopenharmony_ci     *
681100ae2f9Sopenharmony_ci     * @param return The content of the event for trace.
682100ae2f9Sopenharmony_ci     */
683100ae2f9Sopenharmony_ci    std::string TraceInfo();
684100ae2f9Sopenharmony_ci
685100ae2f9Sopenharmony_ci    /**
686100ae2f9Sopenharmony_ci     * Set uniqueId in event.
687100ae2f9Sopenharmony_ci     */
688100ae2f9Sopenharmony_ci    void SetEventUniqueId();
689100ae2f9Sopenharmony_ci
690100ae2f9Sopenharmony_ci    /**
691100ae2f9Sopenharmony_ci     * Get uniqueId for event.
692100ae2f9Sopenharmony_ci     *
693100ae2f9Sopenharmony_ci     * @return Returns uniqueId for event.
694100ae2f9Sopenharmony_ci     */
695100ae2f9Sopenharmony_ci    inline std::string GetEventUniqueId()
696100ae2f9Sopenharmony_ci    {
697100ae2f9Sopenharmony_ci        return eventId;
698100ae2f9Sopenharmony_ci    }
699100ae2f9Sopenharmony_ci
700100ae2f9Sopenharmony_ci    /**
701100ae2f9Sopenharmony_ci     * Get event priority.
702100ae2f9Sopenharmony_ci     *
703100ae2f9Sopenharmony_ci     * @return Returns uniqueId for event.
704100ae2f9Sopenharmony_ci     */
705100ae2f9Sopenharmony_ci    inline int32_t GetEventPriority()
706100ae2f9Sopenharmony_ci    {
707100ae2f9Sopenharmony_ci        return priority;
708100ae2f9Sopenharmony_ci    }
709100ae2f9Sopenharmony_ci
710100ae2f9Sopenharmony_ci    /**
711100ae2f9Sopenharmony_ci     * Set event priority.
712100ae2f9Sopenharmony_ci     */
713100ae2f9Sopenharmony_ci    inline void SetEventPriority(int32_t prio)
714100ae2f9Sopenharmony_ci    {
715100ae2f9Sopenharmony_ci        priority = prio;
716100ae2f9Sopenharmony_ci    }
717100ae2f9Sopenharmony_ci
718100ae2f9Sopenharmony_ci    /**
719100ae2f9Sopenharmony_ci     * Set ownerId.
720100ae2f9Sopenharmony_ci     */
721100ae2f9Sopenharmony_ci    inline void SetOwnerId(std::string ownerId)
722100ae2f9Sopenharmony_ci    {
723100ae2f9Sopenharmony_ci        ownerId_ = ownerId;
724100ae2f9Sopenharmony_ci    }
725100ae2f9Sopenharmony_ci
726100ae2f9Sopenharmony_ci    /**
727100ae2f9Sopenharmony_ci     * Get ownerId.
728100ae2f9Sopenharmony_ci     */
729100ae2f9Sopenharmony_ci    inline std::string GetOwnerId()
730100ae2f9Sopenharmony_ci    {
731100ae2f9Sopenharmony_ci        return ownerId_;
732100ae2f9Sopenharmony_ci    }
733100ae2f9Sopenharmony_ci
734100ae2f9Sopenharmony_ci    /**
735100ae2f9Sopenharmony_ci     * Set delayTime.
736100ae2f9Sopenharmony_ci     */
737100ae2f9Sopenharmony_ci    inline void SetDelayTime(int64_t delayTime)
738100ae2f9Sopenharmony_ci    {
739100ae2f9Sopenharmony_ci        delayTime_ = delayTime;
740100ae2f9Sopenharmony_ci    }
741100ae2f9Sopenharmony_ci
742100ae2f9Sopenharmony_ci    /**
743100ae2f9Sopenharmony_ci     * Get delayTime.
744100ae2f9Sopenharmony_ci     */
745100ae2f9Sopenharmony_ci    inline int64_t GetDelayTime()
746100ae2f9Sopenharmony_ci    {
747100ae2f9Sopenharmony_ci        return delayTime_;
748100ae2f9Sopenharmony_ci    }
749100ae2f9Sopenharmony_ci
750100ae2f9Sopenharmony_ciprivate:
751100ae2f9Sopenharmony_ci    using SmartPtrDestructor = void (*)(void *);
752100ae2f9Sopenharmony_ci
753100ae2f9Sopenharmony_ci    InnerEvent() = default;
754100ae2f9Sopenharmony_ci    ~InnerEvent() = default;
755100ae2f9Sopenharmony_ci
756100ae2f9Sopenharmony_ci    void ClearEvent();
757100ae2f9Sopenharmony_ci
758100ae2f9Sopenharmony_ci    static void WarnSmartPtrCastMismatch();
759100ae2f9Sopenharmony_ci
760100ae2f9Sopenharmony_ci    template<typename T>
761100ae2f9Sopenharmony_ci    static void ReleaseSmartPtr(void *smartPtr)
762100ae2f9Sopenharmony_ci    {
763100ae2f9Sopenharmony_ci        if (smartPtr != nullptr) {
764100ae2f9Sopenharmony_ci            delete reinterpret_cast<T *>(smartPtr);
765100ae2f9Sopenharmony_ci        }
766100ae2f9Sopenharmony_ci    }
767100ae2f9Sopenharmony_ci
768100ae2f9Sopenharmony_ci    template<typename T>
769100ae2f9Sopenharmony_ci    inline void SaveSharedPtr(const T &object)
770100ae2f9Sopenharmony_ci    {
771100ae2f9Sopenharmony_ci        smartPtrDtor_ = ReleaseSmartPtr<T>;
772100ae2f9Sopenharmony_ci        smartPtrTypeId_ = CalculateSmartPtrTypeId(object);
773100ae2f9Sopenharmony_ci        smartPtr_ = new (std::nothrow) T(object);
774100ae2f9Sopenharmony_ci        if (smartPtr_ == nullptr) {
775100ae2f9Sopenharmony_ci            return;
776100ae2f9Sopenharmony_ci        }
777100ae2f9Sopenharmony_ci    }
778100ae2f9Sopenharmony_ci
779100ae2f9Sopenharmony_ci    template<typename T>
780100ae2f9Sopenharmony_ci    inline void SaveUniquePtr(T &object)
781100ae2f9Sopenharmony_ci    {
782100ae2f9Sopenharmony_ci        smartPtrDtor_ = ReleaseSmartPtr<T>;
783100ae2f9Sopenharmony_ci        smartPtrTypeId_ = CalculateSmartPtrTypeId(object);
784100ae2f9Sopenharmony_ci        smartPtr_ = new (std::nothrow) T(std::move(object));
785100ae2f9Sopenharmony_ci        if (smartPtr_ == nullptr) {
786100ae2f9Sopenharmony_ci            return;
787100ae2f9Sopenharmony_ci        }
788100ae2f9Sopenharmony_ci    }
789100ae2f9Sopenharmony_ci
790100ae2f9Sopenharmony_ci    /**
791100ae2f9Sopenharmony_ci     * if event has trace id ,return trace id, else create span id,
792100ae2f9Sopenharmony_ci     * store it in event and return.
793100ae2f9Sopenharmony_ci     *
794100ae2f9Sopenharmony_ci     * @return return hiTrace Id.
795100ae2f9Sopenharmony_ci     */
796100ae2f9Sopenharmony_ci    const std::shared_ptr<HiTraceId> GetOrCreateTraceId();
797100ae2f9Sopenharmony_ci
798100ae2f9Sopenharmony_ci    /**
799100ae2f9Sopenharmony_ci     * return trace id.
800100ae2f9Sopenharmony_ci     *
801100ae2f9Sopenharmony_ci     * @return return hiTrace Id.
802100ae2f9Sopenharmony_ci     */
803100ae2f9Sopenharmony_ci    const std::shared_ptr<HiTraceId> GetTraceId();
804100ae2f9Sopenharmony_ci
805100ae2f9Sopenharmony_ci    /*
806100ae2f9Sopenharmony_ci     * Calculate the type id for different smart pointers.
807100ae2f9Sopenharmony_ci     */
808100ae2f9Sopenharmony_ci#ifdef __GXX_RTTI
809100ae2f9Sopenharmony_ci    // If RTTI(Run-Time Type Info) is enabled, use hash code of type info.
810100ae2f9Sopenharmony_ci    template<typename T>
811100ae2f9Sopenharmony_ci    static inline size_t CalculateSmartPtrTypeId(const T &)
812100ae2f9Sopenharmony_ci    {
813100ae2f9Sopenharmony_ci        return typeid(T).hash_code();
814100ae2f9Sopenharmony_ci    }
815100ae2f9Sopenharmony_ci#else   // #ifdef __GXX_RTTI
816100ae2f9Sopenharmony_ci    // Otherwise, generate type id using smart pointer type and the size of the elements they contain.
817100ae2f9Sopenharmony_ci    static const size_t SHARED_PTR_TYPE = 0x10000000;
818100ae2f9Sopenharmony_ci    static const size_t WEAK_PTR_TYPE = 0x20000000;
819100ae2f9Sopenharmony_ci    static const size_t UNIQUE_PTR_TYPE = 0x30000000;
820100ae2f9Sopenharmony_ci    static const size_t UNIQUE_PTR_ARRAY_TYPE = 0x40000000;
821100ae2f9Sopenharmony_ci
822100ae2f9Sopenharmony_ci    template<typename T>
823100ae2f9Sopenharmony_ci    static inline size_t CalculateSmartPtrTypeId(const std::shared_ptr<T> &)
824100ae2f9Sopenharmony_ci    {
825100ae2f9Sopenharmony_ci        return (sizeof(T) | SHARED_PTR_TYPE);
826100ae2f9Sopenharmony_ci    }
827100ae2f9Sopenharmony_ci
828100ae2f9Sopenharmony_ci    template<typename T>
829100ae2f9Sopenharmony_ci    static inline size_t CalculateSmartPtrTypeId(const std::weak_ptr<T> &)
830100ae2f9Sopenharmony_ci    {
831100ae2f9Sopenharmony_ci        return (sizeof(T) | WEAK_PTR_TYPE);
832100ae2f9Sopenharmony_ci    }
833100ae2f9Sopenharmony_ci
834100ae2f9Sopenharmony_ci    template<typename T, typename D>
835100ae2f9Sopenharmony_ci    static inline size_t CalculateSmartPtrTypeId(const std::unique_ptr<T, D> &)
836100ae2f9Sopenharmony_ci    {
837100ae2f9Sopenharmony_ci        return (sizeof(T) | (sizeof(D) - 1) | UNIQUE_PTR_TYPE);
838100ae2f9Sopenharmony_ci    }
839100ae2f9Sopenharmony_ci
840100ae2f9Sopenharmony_ci    template<typename T, typename D>
841100ae2f9Sopenharmony_ci    static inline size_t CalculateSmartPtrTypeId(const std::unique_ptr<T[], D> &)
842100ae2f9Sopenharmony_ci    {
843100ae2f9Sopenharmony_ci        return (sizeof(T) | (sizeof(D) - 1) | UNIQUE_PTR_ARRAY_TYPE);
844100ae2f9Sopenharmony_ci    }
845100ae2f9Sopenharmony_ci#endif  // #ifdef __GXX_RTTI
846100ae2f9Sopenharmony_ci
847100ae2f9Sopenharmony_ci    // Used by event handler to create waiter.
848100ae2f9Sopenharmony_ci    const std::shared_ptr<Waiter> &CreateWaiter();
849100ae2f9Sopenharmony_ci
850100ae2f9Sopenharmony_ci    // Used by event handler to tell whether event has waiter.
851100ae2f9Sopenharmony_ci    bool HasWaiter() const;
852100ae2f9Sopenharmony_ci
853100ae2f9Sopenharmony_ci    // Let event pool to create instance of events.
854100ae2f9Sopenharmony_ci    friend class InnerEventPool;
855100ae2f9Sopenharmony_ci    // Let event handler to access private interface.
856100ae2f9Sopenharmony_ci    friend class EventHandler;
857100ae2f9Sopenharmony_ci
858100ae2f9Sopenharmony_ci    std::weak_ptr<EventHandler> owner_;
859100ae2f9Sopenharmony_ci    TimePoint handleTime_;
860100ae2f9Sopenharmony_ci    TimePoint sendTime_;
861100ae2f9Sopenharmony_ci    uint64_t senderKernelThreadId_{0};
862100ae2f9Sopenharmony_ci
863100ae2f9Sopenharmony_ci    // Event id of the event, if it is not a task object
864100ae2f9Sopenharmony_ci    EventId innerEventId_ = 0u;
865100ae2f9Sopenharmony_ci
866100ae2f9Sopenharmony_ci    // Simple parameter for the event.
867100ae2f9Sopenharmony_ci    int64_t param_{0};
868100ae2f9Sopenharmony_ci
869100ae2f9Sopenharmony_ci    // Using to save smart pointer
870100ae2f9Sopenharmony_ci    size_t smartPtrTypeId_{0};
871100ae2f9Sopenharmony_ci    void *smartPtr_{nullptr};
872100ae2f9Sopenharmony_ci    SmartPtrDestructor smartPtrDtor_{nullptr};
873100ae2f9Sopenharmony_ci
874100ae2f9Sopenharmony_ci    // Task callback and its name.
875100ae2f9Sopenharmony_ci    Callback taskCallback_;
876100ae2f9Sopenharmony_ci    std::string taskName_;
877100ae2f9Sopenharmony_ci
878100ae2f9Sopenharmony_ci    // Task event caller info
879100ae2f9Sopenharmony_ci    Caller caller_;
880100ae2f9Sopenharmony_ci
881100ae2f9Sopenharmony_ci    // Used for synchronized event.
882100ae2f9Sopenharmony_ci    std::shared_ptr<Waiter> waiter_;
883100ae2f9Sopenharmony_ci
884100ae2f9Sopenharmony_ci    // use to store hitrace Id
885100ae2f9Sopenharmony_ci    std::shared_ptr<HiTraceId> hiTraceId_;
886100ae2f9Sopenharmony_ci
887100ae2f9Sopenharmony_ci    // use to store event unique Id
888100ae2f9Sopenharmony_ci    std::string eventId;
889100ae2f9Sopenharmony_ci
890100ae2f9Sopenharmony_ci    int32_t priority = -1;
891100ae2f9Sopenharmony_ci
892100ae2f9Sopenharmony_ci    std::string ownerId_;
893100ae2f9Sopenharmony_ci
894100ae2f9Sopenharmony_ci    int64_t delayTime_ = 0;
895100ae2f9Sopenharmony_ci};
896100ae2f9Sopenharmony_ci}  // namespace AppExecFwk
897100ae2f9Sopenharmony_ci}  // namespace OHOS
898100ae2f9Sopenharmony_ci
899100ae2f9Sopenharmony_ci#endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_INNER_EVENT_H
900