1 /*
2  * Copyright (c) 2020 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 
17 #ifndef OHOS_ACELITE_JS_TIMER_LIST_H
18 #define OHOS_ACELITE_JS_TIMER_LIST_H
19 
20 #include "acelite_config.h"
21 
22 #if (defined(FEATURE_TIMER_MODULE) && (FEATURE_TIMER_MODULE == 1))
23 #include <jerryscript.h>
24 #include <cstdint>
25 #include "memory_heap.h"
26 #include "nativeapi_timer_task.h"
27 #include "non_copyable.h"
28 
29 namespace OHOS {
30 namespace ACELite {
31 class TimerList final : public MemoryHeap {
32 public:
33     ACE_DISALLOW_COPY_AND_MOVE(TimerList);
34     /**
35      * @fn TimerList::TimerList()
36      * @brief Constructor
37      */
TimerList()38     TimerList() : timerListHead_(nullptr), index_(0) {}
39 
40     /**
41      * @fn TimerList::~TimerList()
42      *
43      */
44     ~TimerList() = default;
45 
46     void ClearTimerList();
47 
48     // the struct of timer task arguments
49     struct Arguments : public MemoryHeap {
50         uint8_t index;
51         bool repeated;
52         jerry_length_t argsNum = 0;
53         jerry_value_t func;
54         jerry_value_t context;
55         jerry_value_t *args = nullptr;
56     };
57 
58     // the struct of timer
59     struct TimerNode : public MemoryHeap {
60         uint8_t index;
61         timerHandle_t timerId;
62         Arguments *arguments;
63         struct TimerNode *next;
64     };
65 
66     jerry_value_t AddTimer(timerHandle_t timerId, Arguments *&arg);
67 
68     TimerNode* GetTimer(uint8_t id);
69 
70     void DeleteTimer(uint8_t timer);
71 
72     void ReleaseArguments(Arguments *&argument);
73 
GetIndex()74     uint8_t GetIndex()
75     {
76         return ++index_;
77     }
78 
79 private:
80     void ReleaseTimer(TimerNode *&current);
81 
82     TimerNode *timerListHead_;
83     uint8_t index_;
84 };
85 } // namespace ACELite
86 } //  namespace OHOS
87 #endif // FEATURE_TIMER_MODULE
88 #endif // OHOS_ACELITE_JS_TIMER_LIST_H
89