e52fb823c1c93ad58b1d4fc2973d95d61a182398
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRtTimer.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FBaseRtTimer.cpp
20  * @brief       This is the implementation file for the Timer class.
21  *
22  */
23
24 #include <new>
25 #include <FBaseRtTimer.h>
26 #include <FBaseSysLog.h>
27 #include "FBaseRt_TimerImpl.h"
28
29
30 namespace Tizen { namespace Base { namespace Runtime
31 {
32
33 Timer::Timer(void)
34         : __pTimerImpl(null)
35 {
36
37 }
38
39 Timer::~Timer(void)
40 {
41         delete __pTimerImpl;
42         __pTimerImpl = null;
43 }
44
45 result
46 Timer::Construct(ITimerEventListener& listener)
47 {
48         // Object is not allowed to construct twice
49         SysAssertf(__pTimerImpl == null,
50                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
51
52         __pTimerImpl = new (std::nothrow) _TimerImpl;
53         SysTryReturnResult(NID_BASE_RT, __pTimerImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
54
55         result r = __pTimerImpl->Construct(*this, listener);
56         if (IsFailed(r))
57         {
58                 delete __pTimerImpl;
59                 __pTimerImpl = null;
60         }
61
62         return r;
63 }
64
65 result
66 Timer::Start(int timeout)
67 {
68         SysAssertf(__pTimerImpl != null, "Not yet constructed! Construct() should be called before use.");
69
70         result r = __pTimerImpl->Start(timeout);
71         SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to start a timer.", GetErrorMessage(r));
72
73         return E_SUCCESS;
74 }
75
76 result
77 Timer::StartAsRepeatable(int interval)
78 {
79         SysAssertf(__pTimerImpl != null, "Not yet constructed! Construct() should be called before use.");
80
81         result r = __pTimerImpl->StartAsRepeatable(interval);
82         SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to start a timer.", GetErrorMessage(r));
83
84         return E_SUCCESS;
85 }
86
87 result
88 Timer::Cancel(void)
89 {
90         SysAssertf(__pTimerImpl != null, "Not yet constructed! Construct() should be called before use.");
91
92         result r = __pTimerImpl->Cancel();
93         SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to cancel a timer.", GetErrorMessage(r));
94
95         return E_SUCCESS;
96 }
97
98 } } } // Tizen::Runtime