Merge "Apply millisecond of DateTime for Calendar" into tizen_2.1
[platform/framework/native/appfw.git] / inc / FBaseRtSemaphoreGuard.h
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         FBaseRtSemaphoreGuard.h
20 * @brief        This is the header file for the %SemaphoreGuard class.
21 *
22 * This header file contains the declarations of the %SemaphoreGuard class.
23 */
24
25 #ifndef _FBASE_RT_SEMAPHORE_GUARD_H_
26 #define _FBASE_RT_SEMAPHORE_GUARD_H_
27
28 #include <FBaseRtSemaphore.h>
29 #include <FBaseRtTypes.h>
30
31 namespace Tizen { namespace Base { namespace Runtime
32 {
33
34 /**
35 * @class        SemaphoreGuard
36 * @brief        This class is the RAII style class for %Semaphore class.
37 *
38 * @since 2.0
39 */
40 class SemaphoreGuard
41 {
42 public:
43         /**
44         * This constructor acquires a semaphore count in a blocking way.
45         *
46         * @since 2.0
47         *
48         * @param[in]    s       The %Semaphore instance to be manipulated
49         * @remarks              The specific error code can be accessed using the GetLastResult() method.
50         * @see                  Semaphore::Acquire() for detailed exceptions
51         */
52         SemaphoreGuard(Semaphore& s)
53                 : __s(s)
54                 , __acquired(false)
55         {
56                 SetLastResult(Acquire());
57         }
58
59         /**
60         * This constructor acquires a semaphore count in a non-blocking way.
61         *
62         * @since 2.0
63         *
64         * @param[in]    s       The %Semaphore instance to be manipulated
65         * @remarks              The specific error code can be accessed using the GetLastResult() method.
66         * @see                  Semaphore::TryToAcquire() for detailed exceptions
67         */
68         SemaphoreGuard(Semaphore& s, TryTag)
69                 : __s(s)
70                 , __acquired(false)
71         {
72                 SetLastResult(TryToAcquire());
73         }
74
75         /**
76         * This destructor releases the acquired semaphore count when going out of a scope
77         *
78         * @since 2.0
79         *
80         * @remarks      The specific error code can be accessed using the GetLastResult() method.
81         * @see          Semaphore::Release() for detailed exceptions
82         */
83         ~SemaphoreGuard(void)
84         {
85                 SetLastResult(Release());
86         }
87
88         /**
89         * Returns whether this instance owns a semaphore count on the given semaphore at constructor.
90         *
91         * @since 2.0
92         *
93         * @return       true if a semaphore is owned, @n
94         *                       false otherwise.
95         */
96         bool IsAcquired(void) const
97         {
98                 return __acquired;
99         }
100
101         /**
102         * Returns whether this instance owns a semaphore count on the given semaphore at constructor. @n
103         * Have same effects to calling IsAcquired().
104         *
105         * @since 2.0
106         */
107         operator bool() const
108         {
109                 return IsAcquired();
110         }
111
112         /**
113         * Acquires a semaphore count manually on the given semaphore at constructor in a blocking way
114         *
115         * @since 2.0
116         *
117         * @return       An error code.
118         * @see          Semaphore::Acquire() for detailed exceptions
119         */
120         result Acquire(void)
121         {
122                 return SetAcquiredAndReturn(__s.Acquire());
123         }
124
125         /**
126         * Acquires a semaphore count manually on the given semaphore at constructor in a non-blocking way
127         *
128         * @since 2.0
129         *
130         * @return       An error code.
131         * @see          Semaphore::TryToAcquire() for detailed exceptions
132         */
133         result TryToAcquire(void)
134         {
135                 return SetAcquiredAndReturn(__s.TryToAcquire());
136         }
137
138         /**
139         * Releases the acquired semaphore count manually
140         *
141         * @since 2.0
142         *
143         * @return       An error code.
144         * @see          Semaphore::Release() for detailed exceptions
145         */
146         result Release(void)
147         {
148                 result r = E_SUCCESS;
149                 if (__acquired)
150                 {
151                         r = __s.Release();
152                         __acquired = false;
153                 }
154                 return r;
155         }
156
157 private:
158         /**
159         * The implementation of this copy constructor is intentionally blank and declared as private 
160         * to prohibit copying of objects.
161         *
162         * @since 2.0
163         */
164         SemaphoreGuard(const SemaphoreGuard& rhs);
165
166         /**
167         * The implementation of this copy assignment operator is intentionally blank and declared as private
168         * to prohibit copying of objects.
169         *
170         * @since 2.0
171         */
172         SemaphoreGuard& operator =(const SemaphoreGuard& rhs);
173
174         // helper function
175         result SetAcquiredAndReturn(result r)
176         {
177                 __acquired = (r == E_SUCCESS);
178                 return r;
179         }
180
181 private:
182         Semaphore& __s;
183         bool __acquired;
184 }; // SemaphoreGuard
185
186 }}} // Tizen::Base::Runtime
187
188 #endif // _FBASE_RT_SEMAPHORE_GUARD_H_