RDK Documentation (Open Sourced RDK Components)
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Timer.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup qtapp
25 * @{
26 **/
27 
28 
29 #include <QObject>
30 #include <QTimer>
31 #include <QTime>
32 
33 #include "trm/Timer.h"
34 #include "QtTimer.h"
35 
36 namespace TRM {
37 
38 #define PVOID(p) ((void *)(p))
39 #define PQTIMER_(p) ((QtTimer *)(p))
40 #define PQTIMER() (PQTIMER_(data))
41 #define QTIMER() (*(PQTIMER()))
42 
43 Timer::Timer(const std::string &token)
44 : data(PVOID(new TRM::QtTimer())), token(token)
45 {
46 };
47 
48 void Timer::schedule(TimerTask &task, int64_t milliSecs, bool absolute)
49 {
50  int64_t future = 0;
51  qint64 now = QDateTime::currentMSecsSinceEpoch();
52  if (!absolute) {
53  future = now + milliSecs;
54  }
55  else {
56  future = milliSecs;
57  milliSecs = (milliSecs - now);
58  if (milliSecs < 0) milliSecs = 0;
59  }
60  std::cout << "Scheduling Timer " << getToken() << std::endl;
61  QTIMER().schedule(task, future, milliSecs);
62 
63 }
64 
65 void Timer::cancel()
66 {
67  QTIMER().cancel();
68 }
69 
70 Timer::~Timer(void)
71 {
72  delete PQTIMER();
73 }
74 
75 }
76 
77 
78 /** @} */
79 /** @} */