Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_WaitingLoopImpl.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FBaseRt_WaitingLoopImpl.h
19  * @brief       This is the header file for the %_WaitingLoopImpl class.
20  *
21  * This file contains the declarations of the %_WaitingLoopImpl class.
22  */
23
24 #ifndef _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_
25 #define _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_
26
27 #include <glib.h>
28 #include <FBaseObject.h>
29 #include <FBaseRtThread.h>
30 #include <FBaseRtTimer.h>
31 #include <FBaseRtITimerEventListener.h>
32
33 namespace Tizen { namespace Base { namespace Runtime
34 {
35
36 class WaitingLoop;
37 class IWaitingLoopCondition;
38 /*
39  * @class _WaitingLoopImpl
40  * @brief This class allows developers to make the current thread wait for callback events. This class should be used only for test codes and don't use this class for commercial applications
41  * @since 2.0
42  *
43  * @final This class is not intended for extension.
44  */
45 class _OSP_EXPORT_ _WaitingLoopImpl
46         : public Tizen::Base::Object
47         , public Tizen::Base::Runtime::ITimerEventListener
48 {
49 public:
50         /*
51          * This is the destructor for this class.
52          *
53          * @since 2.0
54          */
55         virtual ~_WaitingLoopImpl(void);
56
57         /*
58          * Gets the WaitingLoop instance.
59          *
60          * @since 2.0
61          *
62          * @return      A pointer to the WaitingLoop instance, @n
63          *                              else @c null if it fails   
64          */
65         static WaitingLoop* GetInstanceN(void);
66
67         /*
68          * Starts the waiting loop and Waits until the time is expired.
69          *
70          * @since 2.0
71          *
72          * @param[in] timeout   The timeout period in milliseconds  
73          * @exception E_SUCCESS    The method is successful.
74          * @exception E_TIMEOUT    The time is expired.
75          * @exception E_INVALID_STATE    The waiting loop has already been waiting on a callback event. In order to wait again, you must quit the waiting loop.
76          * @remarks The timeout set to min(timeout, maximum), where the maximum is set using SetMaxTimeoutForWaiting().
77          * @see Notify(), SetMaxTimoutForWaiting()
78          */
79         result Wait(int timeout);
80
81         /*
82          * Starts the waiting loop and Waits until the expiring condition occurs.
83          *
84          * @since 2.0
85          *
86          * @param[in] condition The expiring condition  
87          * @exception E_SUCCESS    The method is successful.
88          * @exception E_INVALID_STATE    The waiting loop has already been waiting on a callback event. In order to wait again, you must quit the waiting loop.
89          * @remarks In order to use this method correctly, you have to implement a class inheriting from %IWaitingLoopCondition. 
90          */
91         result Wait(IWaitingLoopCondition& condition);
92
93         /*
94          * Starts the waiting loop and Waits until either the expiring condition occurs or the time is expired.
95          *
96          * @since 2.0
97          *
98          * @param[in] timeout   The timeout period in milliseconds  
99          * @param[in] condition The expiring condition  
100          * @exception E_TIMEOUT    The time is expired.
101          * @exception E_INVALID_STATE    The waiting loop is waiting on a thread. In order to wait again, you must quit the waiting loop.
102          * @remarks If the timeout is over the maximum, the expired time goes together with the maximum timeout. In order to use this method correctly, you have to implement a class inheriting from %IWaitingLoopCondition interface
103          */
104         result Wait(int timeout, IWaitingLoopCondition& condition);
105
106         /*
107          * Notify the waiting thread that the related callback has completed the job.
108          *
109          * @since 2.0
110          *
111          * @remarks This method is recommended to use when the waiting condition is not explicitly specified with Wait().
112          */
113         void Notify(void);
114
115         /*
116          * Called when the timer is expired.
117          *
118          * @since 2.0
119          *
120          * @param[in] timer The expired timer instance
121          */
122         virtual void OnTimerExpired(Timer& timer);
123
124         /*
125          * This default constructor is intentionally declared as private to implement the %Singleton semantic.
126          *
127          * @since 2.0
128          *
129          * @return A boolean value
130          */
131         bool IsSameThread(void);
132
133         /*
134          * Sets the maximum timeout for waiting.
135          *
136          * @since 2.0
137          *
138          * @param[in] timeout    The timeout period in milliseconds
139          *                              else @c false
140          * @remarks If the timeout is considered as a small number, it will be replaced with the specified number provided by platform.
141          */
142         void SetMaxTimeoutForWaiting(int timeout);
143
144 private:
145         //
146         // This default constructor is intentionally declared as private to implement the %Singleton semantic.
147         //
148         // @since 2.0
149         //
150         _WaitingLoopImpl(void);
151
152         //
153         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
154         //
155         // @since 2.0
156         //
157         _WaitingLoopImpl(const _WaitingLoopImpl& rhs);
158
159         //
160         // The implementation of this copy assignment operator is intentionally blank and delcared as private to prohibit copying of objects.
161         //
162         // @since 2.0
163         //
164         _WaitingLoopImpl& operator =(const _WaitingLoopImpl& rhs);
165
166         //
167         // Constructs the instance of this class.
168         //
169         // @since 2.0
170         //
171         // @return An error code
172         // @exception E_SUCCESS The method is successful.
173         //
174         result Construct(void);
175
176         enum WaitType
177         {
178                 WAIT_DEFAULT = 0,
179                 WAIT_TIMEOUT,
180                 WAIT_CONDITION,
181                 WAIT_TIMEOUT_CONDITION
182         };
183
184         enum WaitStatus
185         {
186                 WAIT_ON = 0,
187                 WAIT_OFF
188         };
189
190         Tizen::Base::Runtime::Timer* __pTimer;
191         bool __timeout; 
192         bool __notifyWait;
193         Tizen::Base::Runtime::ThreadType __threadType;
194         GMainContext* __pGmainContext;
195         WaitType __waitType;
196         WaitStatus __waitStatus;
197         IWaitingLoopCondition* __pWaitingLoopCondition;
198         int __timerCalledCount;
199         int __maxTimeout;
200                 
201 }; // _WaitingLoopImpl
202
203 } } } // Tizen::Base::Runtime
204
205 #endif // _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_