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