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