sync with tizen_2.0
[platform/framework/native/appfw.git] / inc / FBaseRtMonitorGuard.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         FBaseRtMonitorGuard.h
20 * @brief        This is the header file for the %MonitorGuard class.
21 *
22 * This header file contains the declarations of the %MonitorGuard class.
23 */
24
25 #ifndef _FBASE_RT_MONITOR_GUARD_H_
26 #define _FBASE_RT_MONITOR_GUARD_H_
27
28 #include <FBaseRtMonitor.h>
29
30 namespace Tizen { namespace Base { namespace Runtime
31 {
32
33 /**
34 * @class        MonitorGuard
35 * @brief        This class is the RAII style class for %Monitor class.
36 *
37 * @since 2.0
38 */
39 class MonitorGuard
40 {
41 public:
42         /**
43         * This constructor enters the monitored block in a blocking way.
44         *
45         * @since 2.0
46         *
47         * @param[in]    m       The %Monitor instance to be manipulated
48         * @remarks              The specific error code can be accessed using the GetLastResult() method.
49         * @see                  Monitor::Enter() for detailed exceptions
50         */
51         MonitorGuard(Monitor& m)
52         : __m(m)
53         , __entered(false)
54         {
55                 SetLastResult(SetEnteredAndReturn(__m.Enter()));
56         }
57
58         /**
59         * This destructor exits the monitored block when going out of a scope
60         *
61         * @since 2.0
62         *
63         * @remarks      The specific error code can be accessed using the GetLastResult() method.
64         * @see          Monitor::Exit() for detailed exceptions
65         */
66         ~MonitorGuard(void)
67         {
68                 result r = E_SUCCESS;
69                 if (__entered)
70                 {
71                         r = __m.Exit();
72                         __entered = false;
73                 }
74                 SetLastResult(r);
75         }
76
77         /**
78         * Returns whether this instance owns the lock on the given monitor at constructor.
79         *
80         * @since 2.0
81         *
82         * @return       true if the lock is owned, @n
83         *                       false otherwise.
84         */
85         bool IsEntered(void) const
86         {
87                 return __entered;
88         }
89
90         /**
91         * Returns whether this instance owns the lock on the given monitor at constructor. @n
92         * Have same effects to calling IsEntered().
93         *
94         * @since 2.0
95         */
96         operator bool() const
97         {
98                 return IsEntered();
99         }
100
101 private:
102         /**
103         * The implementation of this copy constructor is intentionally blank and declared as private 
104         * to prohibit copying of objects.
105         *
106         * @since 2.0
107         */
108         MonitorGuard(const MonitorGuard& rhs);
109
110         /**
111         * The implementation of this copy assignment operator is intentionally blank and declared as private
112         * to prohibit copying of objects.
113         *
114         * @since 2.0
115         */
116         MonitorGuard& operator =(const MonitorGuard& rhs);
117
118         // helper function
119         result SetEnteredAndReturn(result r)
120         {
121                 __entered = (r == E_SUCCESS);
122                 return r;
123         }
124
125 private:
126         Monitor& __m;
127         bool __entered;
128 }; // MonitorGuard
129
130 }}} // Tizen::Base::Runtime
131
132 #endif // _FBASE_RT_MONITOR_GUARD_H_