xref: /trunk/main/cppu/inc/uno/environment.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _UNO_ENVIRONMENT_HXX_
28 #define _UNO_ENVIRONMENT_HXX_
29 
30 #include <rtl/alloc.h>
31 #include <rtl/ustring.hxx>
32 #include <uno/environment.h>
33 
34 #include "uno/lbnames.h"
35 
36 /** */ //for docpp
37 namespace com
38 {
39 /** */ //for docpp
40 namespace sun
41 {
42 /** */ //for docpp
43 namespace star
44 {
45 /** */ //for docpp
46 namespace uno
47 {
48 
49 /** C++ wrapper for binary C uno_Environment.
50 
51     @see uno_Environment
52 */
53 class Environment
54 {
55 	/** binary C uno_Environment
56 	*/
57 	uno_Environment * _pEnv;
58 
59 public:
60     /** Returns the current Environment.
61 
62         @param env_type   the optional type of the Environment, falls back to "uno" in case being empty,
63 	                      respectively to current C++ Environment.
64         @since UDK 3.2.7
65     */
66     inline static Environment getCurrent(rtl::OUString const & typeName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV)))) SAL_THROW( () );
67 
68 	// these are here to force memory de/allocation to sal lib.
69     /** @internal */
70 	inline static void * SAL_CALL operator new ( size_t nSize ) SAL_THROW( () )
71 		{ return ::rtl_allocateMemory( nSize ); }
72     /** @internal */
73 	inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW( () )
74 		{ ::rtl_freeMemory( pMem ); }
75     /** @internal */
76 	inline static void * SAL_CALL operator new ( size_t, void * pMem ) SAL_THROW( () )
77 		{ return pMem; }
78     /** @internal */
79 	inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW( () )
80 		{}
81 
82 	/** Constructor: acquires given environment
83 
84 		@param pEnv environment
85 	*/
86 	inline Environment( uno_Environment * pEnv = 0 ) SAL_THROW( () );
87 
88 	/** Gets a specific environment. If the specified environment does not exist, then a default one
89 		is created and registered.
90 
91 		@param envDcp           descriptor of the environment
92 		@param pContext         context pointer
93 	*/
94 	inline explicit Environment( rtl::OUString const & envDcp, void * pContext = NULL ) SAL_THROW( () );
95 
96 
97 	/** Copy constructor: acquires given environment
98 
99 		@param rEnv another environment
100 	*/
101 	inline Environment( const Environment & rEnv ) SAL_THROW( () );
102 
103 	/** Destructor: releases a set environment.
104 	*/
105 	inline ~Environment() SAL_THROW( () );
106 
107 	/** Sets a given environment, i.e. acquires given one and releases a set one.
108 
109 		@param pEnv another environment
110 		@return this environment
111 	*/
112 	inline Environment & SAL_CALL operator = ( uno_Environment * pEnv ) SAL_THROW( () );
113 	/** Sets a given environment, i.e. acquires given one and releases a set one.
114 
115 		@param rEnv another environment
116 		@return this environment
117 	*/
118 	inline Environment & SAL_CALL operator = ( const Environment & rEnv ) SAL_THROW( () )
119 		{ return operator = ( rEnv._pEnv ); }
120 
121 	/** Provides UNacquired pointer to the set C environment.
122 
123 		@return UNacquired pointer to the C environment struct
124 	*/
125 	inline uno_Environment * SAL_CALL get() const SAL_THROW( () )
126 		{ return _pEnv; }
127 
128 	/** Gets type name of set environment.
129 
130 		@return type name of set environment
131 	*/
132 	inline ::rtl::OUString SAL_CALL getTypeName() const SAL_THROW( () )
133 		{ return _pEnv->pTypeName; }
134 
135 	/** Gets free context pointer of set environment.
136 
137 		@return free context pointer of set environment
138 	*/
139 	inline void * SAL_CALL getContext() const SAL_THROW( () )
140 		{ return _pEnv->pContext; }
141 
142 	/** Tests if a environment is set.
143 
144 		@return true, if a environment is set, false otherwise
145 	*/
146 	inline sal_Bool SAL_CALL is() const SAL_THROW( () )
147 		{ return (_pEnv != 0); }
148 
149 	/** Releases a set environment.
150 	*/
151 	inline void SAL_CALL clear() SAL_THROW( () );
152 
153 	/** Invoke the passed function in this environment.
154 
155 		@param pCallee  the function to call
156 		@param pParam   the parameter pointer to be passed to the function
157         @since UDK 3.2.7
158 	*/
159 	inline void SAL_CALL invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const SAL_THROW( () );
160 
161 	/** Invoke the passed function in this environment.
162 
163         @param pCallee  the function to call
164         @param ...      the parameters to be passed to the function
165         @since UDK 3.2.7
166 	*/
167 	inline void SAL_CALL invoke(uno_EnvCallee * pCallee, ...) const SAL_THROW( () );
168 
169     /** Enter this environment explicitly.
170 
171         @since UDK 3.2.7
172 	*/
173 	inline void SAL_CALL enter() const SAL_THROW( () );
174 
175     /** Checks, if it is valid to currently call objects
176         belonging to this environment.
177 
178         @since UDK 3.2.7
179     */
180 	inline int  SAL_CALL isValid(rtl::OUString * pReason) const SAL_THROW( () );
181 };
182 //__________________________________________________________________________________________________
183 inline Environment::Environment( uno_Environment * pEnv ) SAL_THROW( () )
184 	: _pEnv( pEnv )
185 {
186 	if (_pEnv)
187 		(*_pEnv->acquire)( _pEnv );
188 }
189 //__________________________________________________________________________________________________
190 inline Environment::Environment( rtl::OUString const & rEnvDcp, void * pContext ) SAL_THROW( () )
191 	: _pEnv(NULL)
192 {
193 	uno_getEnvironment(&_pEnv, rEnvDcp.pData, pContext);
194 }
195 //__________________________________________________________________________________________________
196 inline Environment::Environment( const Environment & rEnv ) SAL_THROW( () )
197 	: _pEnv( rEnv._pEnv )
198 {
199 	if (_pEnv)
200 		(*_pEnv->acquire)( _pEnv );
201 }
202 //__________________________________________________________________________________________________
203 inline Environment::~Environment() SAL_THROW( () )
204 {
205 	if (_pEnv)
206 		(*_pEnv->release)( _pEnv );
207 }
208 //__________________________________________________________________________________________________
209 inline void Environment::clear() SAL_THROW( () )
210 {
211 	if (_pEnv)
212 	{
213 		(*_pEnv->release)( _pEnv );
214 		_pEnv = 0;
215 	}
216 }
217 //__________________________________________________________________________________________________
218 inline Environment & Environment::operator = ( uno_Environment * pEnv ) SAL_THROW( () )
219 {
220 	if (pEnv != _pEnv)
221 	{
222 		if (pEnv)
223 			(*pEnv->acquire)( pEnv );
224 		if (_pEnv)
225 			(*_pEnv->release)( _pEnv );
226 		_pEnv = pEnv;
227 	}
228 	return *this;
229 }
230 //__________________________________________________________________________________________________
231 inline void SAL_CALL Environment::invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const SAL_THROW( () )
232 {
233 	if (_pEnv)
234 		uno_Environment_invoke_v(_pEnv, pCallee, pParam);
235 }
236 //__________________________________________________________________________________________________
237 inline void SAL_CALL Environment::invoke(uno_EnvCallee * pCallee, ...) const SAL_THROW( () )
238 {
239 	if (_pEnv)
240 	{
241 		va_list param;
242 
243 		va_start(param, pCallee);
244 		uno_Environment_invoke_v(_pEnv, pCallee, &param);
245 		va_end(param);
246 	}
247 
248 }
249 //__________________________________________________________________________________________________
250 inline void SAL_CALL Environment::enter() const SAL_THROW( () )
251 {
252 	uno_Environment_enter(_pEnv);
253 }
254 //__________________________________________________________________________________________________
255 inline int  SAL_CALL Environment::isValid(rtl::OUString * pReason) const SAL_THROW( () )
256 {
257 	return uno_Environment_isValid(_pEnv, (rtl_uString **)pReason);
258 }
259 //__________________________________________________________________________________________________
260 inline Environment Environment::getCurrent(rtl::OUString const & typeName) SAL_THROW( () )
261 {
262 	Environment environment;
263 
264 	uno_Environment * pEnv = NULL;
265 	uno_getCurrentEnvironment(&pEnv, typeName.pData);
266 	environment = pEnv;
267 	if (pEnv)
268 		pEnv->release(pEnv);
269 
270 	return environment;
271 }
272 
273 }
274 }
275 }
276 }
277 
278 #endif
279