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 #ifndef _UNO_ENVIRONMENT_HXX_ 24 #define _UNO_ENVIRONMENT_HXX_ 25 26 #include <rtl/alloc.h> 27 #include <rtl/ustring.hxx> 28 #include <uno/environment.h> 29 30 #include "uno/lbnames.h" 31 32 /** */ //for docpp 33 namespace com 34 { 35 /** */ //for docpp 36 namespace sun 37 { 38 /** */ //for docpp 39 namespace star 40 { 41 /** */ //for docpp 42 namespace uno 43 { 44 45 /** C++ wrapper for binary C uno_Environment. 46 47 @see uno_Environment 48 */ 49 class Environment 50 { 51 /** binary C uno_Environment 52 */ 53 uno_Environment * _pEnv; 54 55 public: 56 /** Returns the current Environment. 57 58 @param env_type the optional type of the Environment, falls back to "uno" in case being empty, 59 respectively to current C++ Environment. 60 @since UDK 3.2.7 61 */ 62 inline static Environment getCurrent(rtl::OUString const & typeName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(CPPU_STRINGIFY(CPPU_ENV)))) SAL_THROW( () ); 63 64 // these are here to force memory de/allocation to sal lib. 65 /** @internal */ 66 inline static void * SAL_CALL operator new ( size_t nSize ) SAL_THROW( () ) 67 { return ::rtl_allocateMemory( nSize ); } 68 /** @internal */ 69 inline static void SAL_CALL operator delete ( void * pMem ) SAL_THROW( () ) 70 { ::rtl_freeMemory( pMem ); } 71 /** @internal */ 72 inline static void * SAL_CALL operator new ( size_t, void * pMem ) SAL_THROW( () ) 73 { return pMem; } 74 /** @internal */ 75 inline static void SAL_CALL operator delete ( void *, void * ) SAL_THROW( () ) 76 {} 77 78 /** Constructor: acquires given environment 79 80 @param pEnv environment 81 */ 82 inline Environment( uno_Environment * pEnv = 0 ) SAL_THROW( () ); 83 84 /** Gets a specific environment. If the specified environment does not exist, then a default one 85 is created and registered. 86 87 @param envDcp descriptor of the environment 88 @param pContext context pointer 89 */ 90 inline explicit Environment( rtl::OUString const & envDcp, void * pContext = NULL ) SAL_THROW( () ); 91 92 93 /** Copy constructor: acquires given environment 94 95 @param rEnv another environment 96 */ 97 inline Environment( const Environment & rEnv ) SAL_THROW( () ); 98 99 /** Destructor: releases a set environment. 100 */ 101 inline ~Environment() SAL_THROW( () ); 102 103 /** Sets a given environment, i.e. acquires given one and releases a set one. 104 105 @param pEnv another environment 106 @return this environment 107 */ 108 inline Environment & SAL_CALL operator = ( uno_Environment * pEnv ) SAL_THROW( () ); 109 /** Sets a given environment, i.e. acquires given one and releases a set one. 110 111 @param rEnv another environment 112 @return this environment 113 */ 114 inline Environment & SAL_CALL operator = ( const Environment & rEnv ) SAL_THROW( () ) 115 { return operator = ( rEnv._pEnv ); } 116 117 /** Provides UNacquired pointer to the set C environment. 118 119 @return UNacquired pointer to the C environment struct 120 */ 121 inline uno_Environment * SAL_CALL get() const SAL_THROW( () ) 122 { return _pEnv; } 123 124 /** Gets type name of set environment. 125 126 @return type name of set environment 127 */ 128 inline ::rtl::OUString SAL_CALL getTypeName() const SAL_THROW( () ) 129 { return _pEnv->pTypeName; } 130 131 /** Gets free context pointer of set environment. 132 133 @return free context pointer of set environment 134 */ 135 inline void * SAL_CALL getContext() const SAL_THROW( () ) 136 { return _pEnv->pContext; } 137 138 /** Tests if a environment is set. 139 140 @return true, if a environment is set, false otherwise 141 */ 142 inline sal_Bool SAL_CALL is() const SAL_THROW( () ) 143 { return (_pEnv != 0); } 144 145 /** Releases a set environment. 146 */ 147 inline void SAL_CALL clear() SAL_THROW( () ); 148 149 /** Invoke the passed function in this environment. 150 151 @param pCallee the function to call 152 @param pParam the parameter pointer to be passed to the function 153 @since UDK 3.2.7 154 */ 155 inline void SAL_CALL invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const SAL_THROW( () ); 156 157 /** Invoke the passed function in this environment. 158 159 @param pCallee the function to call 160 @param ... the parameters to be passed to the function 161 @since UDK 3.2.7 162 */ 163 inline void SAL_CALL invoke(uno_EnvCallee * pCallee, ...) const SAL_THROW( () ); 164 165 /** Enter this environment explicitly. 166 167 @since UDK 3.2.7 168 */ 169 inline void SAL_CALL enter() const SAL_THROW( () ); 170 171 /** Checks, if it is valid to currently call objects 172 belonging to this environment. 173 174 @since UDK 3.2.7 175 */ 176 inline int SAL_CALL isValid(rtl::OUString * pReason) const SAL_THROW( () ); 177 }; 178 //__________________________________________________________________________________________________ 179 inline Environment::Environment( uno_Environment * pEnv ) SAL_THROW( () ) 180 : _pEnv( pEnv ) 181 { 182 if (_pEnv) 183 (*_pEnv->acquire)( _pEnv ); 184 } 185 //__________________________________________________________________________________________________ 186 inline Environment::Environment( rtl::OUString const & rEnvDcp, void * pContext ) SAL_THROW( () ) 187 : _pEnv(NULL) 188 { 189 uno_getEnvironment(&_pEnv, rEnvDcp.pData, pContext); 190 } 191 //__________________________________________________________________________________________________ 192 inline Environment::Environment( const Environment & rEnv ) SAL_THROW( () ) 193 : _pEnv( rEnv._pEnv ) 194 { 195 if (_pEnv) 196 (*_pEnv->acquire)( _pEnv ); 197 } 198 //__________________________________________________________________________________________________ 199 inline Environment::~Environment() SAL_THROW( () ) 200 { 201 if (_pEnv) 202 (*_pEnv->release)( _pEnv ); 203 } 204 //__________________________________________________________________________________________________ 205 inline void Environment::clear() SAL_THROW( () ) 206 { 207 if (_pEnv) 208 { 209 (*_pEnv->release)( _pEnv ); 210 _pEnv = 0; 211 } 212 } 213 //__________________________________________________________________________________________________ 214 inline Environment & Environment::operator = ( uno_Environment * pEnv ) SAL_THROW( () ) 215 { 216 if (pEnv != _pEnv) 217 { 218 if (pEnv) 219 (*pEnv->acquire)( pEnv ); 220 if (_pEnv) 221 (*_pEnv->release)( _pEnv ); 222 _pEnv = pEnv; 223 } 224 return *this; 225 } 226 //__________________________________________________________________________________________________ 227 inline void SAL_CALL Environment::invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const SAL_THROW( () ) 228 { 229 if (_pEnv) 230 uno_Environment_invoke_v(_pEnv, pCallee, pParam); 231 } 232 //__________________________________________________________________________________________________ 233 inline void SAL_CALL Environment::invoke(uno_EnvCallee * pCallee, ...) const SAL_THROW( () ) 234 { 235 if (_pEnv) 236 { 237 va_list param; 238 239 va_start(param, pCallee); 240 uno_Environment_invoke_v(_pEnv, pCallee, ¶m); 241 va_end(param); 242 } 243 244 } 245 //__________________________________________________________________________________________________ 246 inline void SAL_CALL Environment::enter() const SAL_THROW( () ) 247 { 248 uno_Environment_enter(_pEnv); 249 } 250 //__________________________________________________________________________________________________ 251 inline int SAL_CALL Environment::isValid(rtl::OUString * pReason) const SAL_THROW( () ) 252 { 253 return uno_Environment_isValid(_pEnv, (rtl_uString **)pReason); 254 } 255 //__________________________________________________________________________________________________ 256 inline Environment Environment::getCurrent(rtl::OUString const & typeName) SAL_THROW( () ) 257 { 258 Environment environment; 259 260 uno_Environment * pEnv = NULL; 261 uno_getCurrentEnvironment(&pEnv, typeName.pData); 262 environment = pEnv; 263 if (pEnv) 264 pEnv->release(pEnv); 265 266 return environment; 267 } 268 269 } 270 } 271 } 272 } 273 274 #endif 275