xref: /trunk/main/cppu/inc/cppu/EnvGuards.hxx (revision c059a6bf)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_cppu_EnvGuards_hxx
25 #define INCLUDED_cppu_EnvGuards_hxx
26 
27 #include "uno/environment.hxx"
28 #include "uno/mapping.hxx"
29 
30 
31 namespace cssuno = com::sun::star::uno;
32 
33 
34 namespace cppu
35 {
36     /** Environment Guard
37         The provided Environment becomes entered in the constructor and left
38         in the destructor.
39         (https://wiki.openoffice.org/wiki/Uno/Cpp/Spec/Environment_Guard)
40 
41         @since UDK 3.2.7
42     */
43     class EnvGuard
44     {
45 		cssuno::Environment m_env;
46 
47     public:
EnvGuard(cssuno::Environment const & env)48         explicit EnvGuard(cssuno::Environment const & env)
49         {
50             if (env.is())
51 			{
52 				m_env = cssuno::Environment::getCurrent();
53 				env.enter();
54 			}
55         }
56 
~EnvGuard()57         ~EnvGuard()
58 		{
59 			m_env.enter();
60 		}
61 
62         /** Checks if the associated environment is non empty.
63 
64 	        @return  0 == empty, 1 == non empty
65         */
is() const66         sal_Bool SAL_CALL is() const SAL_THROW( () )
67 		{
68             return m_env.is();
69         }
70 
71         /** Leaves the associated environment and clears
72             the reference.
73         */
clear()74         void clear()
75         {
76 			if (m_env.is())
77 			{
78 				m_env.enter();
79 				m_env.clear();
80 			}
81         }
82     };
83 
84     /** Environment Anti-Guard
85         Any entered Environment becomes left in the constructor and re-entered
86         in the destructor.
87         (https://wiki.openoffice.org/wiki/Uno/Cpp/Spec/Environment_AntiGuard)
88 
89         @since UDK 3.2.7
90     */
91     class AntiEnvGuard
92     {
93 		cssuno::Environment m_env;
94 
95     public:
AntiEnvGuard()96         explicit AntiEnvGuard()
97 			: m_env(cssuno::Environment::getCurrent())
98         {
99 			uno_Environment_enter(NULL);
100         }
101 
~AntiEnvGuard()102         ~AntiEnvGuard()
103         {
104 			m_env.enter();
105         }
106     };
107 }
108 
109 #endif
110