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