Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / uno / environment.hxx
blob31eca6f04fad8ba93a241f4ca54a7bdde3953416
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_UNO_ENVIRONMENT_HXX
20 #define INCLUDED_UNO_ENVIRONMENT_HXX
22 #include <sal/config.h>
24 #include <cstddef>
26 #include <rtl/alloc.h>
27 #include <rtl/ustring.hxx>
28 #include <uno/environment.h>
30 #include <uno/lbnames.h>
32 /** */ //for docpp
33 namespace com
35 /** */ //for docpp
36 namespace sun
38 /** */ //for docpp
39 namespace star
41 /** */ //for docpp
42 namespace uno
45 /** C++ wrapper for binary C uno_Environment.
47 @see uno_Environment
49 class Environment
51 /** binary C uno_Environment
53 uno_Environment * _pEnv;
55 public:
56 /** Returns the current Environment.
58 @param typeName 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
62 inline static Environment getCurrent(rtl::OUString const & typeName = rtl::OUString(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
64 /// @cond INTERNAL
65 // these are here to force memory de/allocation to sal lib.
66 static void * SAL_CALL operator new ( size_t nSize )
67 { return ::rtl_allocateMemory( nSize ); }
68 static void SAL_CALL operator delete ( void * pMem )
69 { ::rtl_freeMemory( pMem ); }
70 static void * SAL_CALL operator new ( size_t, void * pMem )
71 { return pMem; }
72 static void SAL_CALL operator delete ( void *, void * )
74 /// @endcond
76 /** Constructor: acquires given environment
78 @param pEnv environment
80 inline Environment( uno_Environment * pEnv = NULL );
82 /** Gets a specific environment. If the specified environment does not exist, then a default one
83 is created and registered.
85 @param envDcp descriptor of the environment
86 @param pContext context pointer
88 inline explicit Environment( rtl::OUString const & envDcp, void * pContext = NULL );
91 /** Copy constructor: acquires given environment
93 @param rEnv another environment
95 inline Environment( const Environment & rEnv );
97 #if defined LIBO_INTERNAL_ONLY
98 Environment(Environment && other): _pEnv(other._pEnv)
99 { other._pEnv = nullptr; }
100 #endif
102 /** Destructor: releases a set environment.
104 inline ~Environment();
106 /** Sets a given environment, i.e. acquires given one and releases a set one.
108 @param pEnv another environment
109 @return this environment
111 inline Environment & SAL_CALL operator = ( uno_Environment * pEnv );
112 /** Sets a given environment, i.e. acquires given one and releases a set one.
114 @param rEnv another environment
115 @return this environment
117 Environment & SAL_CALL operator = ( const Environment & rEnv )
118 { return operator = ( rEnv._pEnv ); }
120 #if defined LIBO_INTERNAL_ONLY
121 Environment & operator =(Environment && other) {
122 if (_pEnv != nullptr) {
123 (*_pEnv->release)(_pEnv);
125 _pEnv = other._pEnv;
126 other._pEnv = nullptr;
127 return *this;
129 #endif
131 /** Provides UNacquired pointer to the set C environment.
133 @return UNacquired pointer to the C environment struct
135 uno_Environment * SAL_CALL get() const
136 { return _pEnv; }
138 /** Gets type name of set environment.
140 @return type name of set environment
142 ::rtl::OUString SAL_CALL getTypeName() const
143 { return _pEnv->pTypeName; }
145 /** Gets free context pointer of set environment.
147 @return free context pointer of set environment
149 void * SAL_CALL getContext() const
150 { return _pEnv->pContext; }
152 /** Tests if a environment is set.
154 @return true, if a environment is set, false otherwise
156 bool SAL_CALL is() const
157 { return (_pEnv != NULL); }
159 /** Releases a set environment.
161 inline void SAL_CALL clear();
163 /** Invoke the passed function in this environment.
165 @param pCallee the function to call
166 @param pParam the parameter pointer to be passed to the function
167 @since UDK 3.2.7
169 inline void SAL_CALL invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const;
171 /** Invoke the passed function in this environment.
173 @param pCallee the function to call
174 @param ... the parameters to be passed to the function
175 @since UDK 3.2.7
177 inline void SAL_CALL invoke(uno_EnvCallee * pCallee, ...) const;
179 /** Enter this environment explicitly.
181 @since UDK 3.2.7
183 inline void SAL_CALL enter() const;
185 /** Checks, if it is valid to currently call objects
186 belonging to this environment.
188 @since UDK 3.2.7
190 inline int SAL_CALL isValid(rtl::OUString * pReason) const;
193 inline Environment::Environment( uno_Environment * pEnv )
194 : _pEnv( pEnv )
196 if (_pEnv)
197 (*_pEnv->acquire)( _pEnv );
200 inline Environment::Environment( rtl::OUString const & rEnvDcp, void * pContext )
201 : _pEnv(NULL)
203 uno_getEnvironment(&_pEnv, rEnvDcp.pData, pContext);
206 inline Environment::Environment( const Environment & rEnv )
207 : _pEnv( rEnv._pEnv )
209 if (_pEnv)
210 (*_pEnv->acquire)( _pEnv );
213 inline Environment::~Environment()
215 if (_pEnv)
216 (*_pEnv->release)( _pEnv );
219 inline void Environment::clear()
221 if (_pEnv)
223 (*_pEnv->release)( _pEnv );
224 _pEnv = NULL;
228 inline Environment & Environment::operator = ( uno_Environment * pEnv )
230 if (pEnv != _pEnv)
232 if (pEnv)
233 (*pEnv->acquire)( pEnv );
234 if (_pEnv)
235 (*_pEnv->release)( _pEnv );
236 _pEnv = pEnv;
238 return *this;
241 inline void SAL_CALL Environment::invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const
243 if (_pEnv)
244 uno_Environment_invoke_v(_pEnv, pCallee, pParam);
247 inline void SAL_CALL Environment::invoke(uno_EnvCallee * pCallee, ...) const
249 if (_pEnv)
251 va_list param;
253 va_start(param, pCallee);
254 uno_Environment_invoke_v(_pEnv, pCallee, &param);
255 va_end(param);
260 inline void SAL_CALL Environment::enter() const
262 uno_Environment_enter(_pEnv);
265 inline int SAL_CALL Environment::isValid(rtl::OUString * pReason) const
267 return uno_Environment_isValid(_pEnv, &pReason->pData);
270 inline Environment Environment::getCurrent(rtl::OUString const & typeName)
272 Environment environment;
274 uno_Environment * pEnv = NULL;
275 uno_getCurrentEnvironment(&pEnv, typeName.pData);
276 environment = pEnv;
277 if (pEnv)
278 pEnv->release(pEnv);
280 return environment;
288 #endif
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */