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