1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: environment.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _UNO_ENVIRONMENT_HXX_
31 #define _UNO_ENVIRONMENT_HXX_
33 #include <rtl/alloc.h>
34 #include <rtl/ustring.hxx>
35 #include <uno/environment.h>
37 #include "uno/lbnames.h"
52 /** C++ wrapper for binary C uno_Environment.
58 /** binary C uno_Environment
60 uno_Environment
* _pEnv
;
63 /** Returns the current Environment.
65 @param env_type the optional type of the Environment, falls back to "uno" in case being empty,
66 respectively to current C++ Environment.
69 inline static Environment
getCurrent(rtl::OUString
const & typeName
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV
)))) SAL_THROW( () );
71 // these are here to force memory de/allocation to sal lib.
73 inline static void * SAL_CALL
operator new ( size_t nSize
) SAL_THROW( () )
74 { return ::rtl_allocateMemory( nSize
); }
76 inline static void SAL_CALL
operator delete ( void * pMem
) SAL_THROW( () )
77 { ::rtl_freeMemory( pMem
); }
79 inline static void * SAL_CALL
operator new ( size_t, void * pMem
) SAL_THROW( () )
82 inline static void SAL_CALL
operator delete ( void *, void * ) SAL_THROW( () )
85 /** Constructor: acquires given environment
87 @param pEnv environment
89 inline Environment( uno_Environment
* pEnv
= 0 ) SAL_THROW( () );
91 /** Gets a specific environment. If the specified environment does not exist, then a default one
92 is created and registered.
94 @param envDcp descriptor of the environment
95 @param pContext context pointer
97 inline explicit Environment( rtl::OUString
const & envDcp
, void * pContext
= NULL
) SAL_THROW( () );
100 /** Copy constructor: acquires given environment
102 @param rEnv another environment
104 inline Environment( const Environment
& rEnv
) SAL_THROW( () );
106 /** Destructor: releases a set environment.
108 inline ~Environment() SAL_THROW( () );
110 /** Sets a given environment, i.e. acquires given one and releases a set one.
112 @param pEnv another environment
113 @return this environment
115 inline Environment
& SAL_CALL
operator = ( uno_Environment
* pEnv
) SAL_THROW( () );
116 /** Sets a given environment, i.e. acquires given one and releases a set one.
118 @param rEnv another environment
119 @return this environment
121 inline Environment
& SAL_CALL
operator = ( const Environment
& rEnv
) SAL_THROW( () )
122 { return operator = ( rEnv
._pEnv
); }
124 /** Provides UNacquired pointer to the set C environment.
126 @return UNacquired pointer to the C environment struct
128 inline uno_Environment
* SAL_CALL
get() const SAL_THROW( () )
131 /** Gets type name of set environment.
133 @return type name of set environment
135 inline ::rtl::OUString SAL_CALL
getTypeName() const SAL_THROW( () )
136 { return _pEnv
->pTypeName
; }
138 /** Gets free context pointer of set environment.
140 @return free context pointer of set environment
142 inline void * SAL_CALL
getContext() const SAL_THROW( () )
143 { return _pEnv
->pContext
; }
145 /** Tests if a environment is set.
147 @return true, if a environment is set, false otherwise
149 inline sal_Bool SAL_CALL
is() const SAL_THROW( () )
150 { return (_pEnv
!= 0); }
152 /** Releases a set environment.
154 inline void SAL_CALL
clear() SAL_THROW( () );
156 /** Invoke the passed function in this environment.
158 @param pCallee the function to call
159 @param pParam the parameter pointer to be passed to the function
162 inline void SAL_CALL
invoke_v(uno_EnvCallee
* pCallee
, va_list * pParam
) const SAL_THROW( () );
164 /** Invoke the passed function in this environment.
166 @param pCallee the function to call
167 @param ... the parameters to be passed to the function
170 inline void SAL_CALL
invoke(uno_EnvCallee
* pCallee
, ...) const SAL_THROW( () );
172 /** Enter this environment explicitly.
176 inline void SAL_CALL
enter() const SAL_THROW( () );
178 /** Checks, if it is valid to currently call objects
179 belonging to this environment.
183 inline int SAL_CALL
isValid(rtl::OUString
* pReason
) const SAL_THROW( () );
185 //__________________________________________________________________________________________________
186 inline Environment::Environment( uno_Environment
* pEnv
) SAL_THROW( () )
190 (*_pEnv
->acquire
)( _pEnv
);
192 //__________________________________________________________________________________________________
193 inline Environment::Environment( rtl::OUString
const & rEnvDcp
, void * pContext
) SAL_THROW( () )
196 uno_getEnvironment(&_pEnv
, rEnvDcp
.pData
, pContext
);
198 //__________________________________________________________________________________________________
199 inline Environment::Environment( const Environment
& rEnv
) SAL_THROW( () )
200 : _pEnv( rEnv
._pEnv
)
203 (*_pEnv
->acquire
)( _pEnv
);
205 //__________________________________________________________________________________________________
206 inline Environment::~Environment() SAL_THROW( () )
209 (*_pEnv
->release
)( _pEnv
);
211 //__________________________________________________________________________________________________
212 inline void Environment::clear() SAL_THROW( () )
216 (*_pEnv
->release
)( _pEnv
);
220 //__________________________________________________________________________________________________
221 inline Environment
& Environment::operator = ( uno_Environment
* pEnv
) SAL_THROW( () )
226 (*pEnv
->acquire
)( pEnv
);
228 (*_pEnv
->release
)( _pEnv
);
233 //__________________________________________________________________________________________________
234 inline void SAL_CALL
Environment::invoke_v(uno_EnvCallee
* pCallee
, va_list * pParam
) const SAL_THROW( () )
237 uno_Environment_invoke_v(_pEnv
, pCallee
, pParam
);
239 //__________________________________________________________________________________________________
240 inline void SAL_CALL
Environment::invoke(uno_EnvCallee
* pCallee
, ...) const SAL_THROW( () )
246 va_start(param
, pCallee
);
247 uno_Environment_invoke_v(_pEnv
, pCallee
, ¶m
);
252 //__________________________________________________________________________________________________
253 inline void SAL_CALL
Environment::enter() const SAL_THROW( () )
255 uno_Environment_enter(_pEnv
);
257 //__________________________________________________________________________________________________
258 inline int SAL_CALL
Environment::isValid(rtl::OUString
* pReason
) const SAL_THROW( () )
260 return uno_Environment_isValid(_pEnv
, (rtl_uString
**)pReason
);
262 //__________________________________________________________________________________________________
263 inline Environment
Environment::getCurrent(rtl::OUString
const & typeName
) SAL_THROW( () )
265 Environment environment
;
267 uno_Environment
* pEnv
= NULL
;
268 uno_getCurrentEnvironment(&pEnv
, typeName
.pData
);