Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/uno/environment.hxx
Line
Count
Source
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
20
/*
21
 * This file is part of LibreOffice published API.
22
 */
23
#ifndef INCLUDED_UNO_ENVIRONMENT_HXX
24
#define INCLUDED_UNO_ENVIRONMENT_HXX
25
26
#include "sal/config.h"
27
28
#include <cstddef>
29
30
#include "rtl/alloc.h"
31
#include "rtl/ustring.hxx"
32
#include "uno/environment.h"
33
34
#include "uno/lbnames.h"
35
36
namespace com
37
{
38
namespace sun
39
{
40
namespace star
41
{
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 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
61
    */
62
    inline static Environment getCurrent(rtl::OUString const & typeName = rtl::OUString(CPPU_CURRENT_LANGUAGE_BINDING_NAME));
63
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
0
        { return ::rtl_allocateMemory( nSize ); }
68
    static void SAL_CALL operator delete ( void * pMem )
69
0
        { ::rtl_freeMemory( pMem ); }
70
    static void * SAL_CALL operator new ( size_t, void * pMem )
71
0
        { return pMem; }
72
    static void SAL_CALL operator delete ( void *, void * )
73
0
        {}
74
    /// @endcond
75
76
    /** Constructor: acquires given environment
77
78
        @param pEnv environment
79
    */
80
    inline Environment( uno_Environment * pEnv = NULL );
81
82
    /** Gets a specific environment. If the specified environment does not exist, then a default one
83
        is created and registered.
84
85
        @param envDcp           descriptor of the environment
86
        @param pContext         context pointer
87
    */
88
    inline explicit Environment( rtl::OUString const & envDcp, void * pContext = NULL );
89
90
91
    /** Copy constructor: acquires given environment
92
93
        @param rEnv another environment
94
    */
95
    inline Environment( const Environment & rEnv );
96
97
#if defined LIBO_INTERNAL_ONLY
98
318
    Environment(Environment && other) noexcept : _pEnv(other._pEnv)
99
318
    { other._pEnv = nullptr; }
100
#endif
101
102
    /** Destructor: releases a set environment.
103
    */
104
    inline ~Environment();
105
106
    /** Sets a given environment, i.e. acquires given one and releases a set one.
107
108
        @param pEnv another environment
109
        @return this environment
110
    */
111
    inline Environment & SAL_CALL operator = ( uno_Environment * pEnv );
112
    /** Sets a given environment, i.e. acquires given one and releases a set one.
113
114
        @param rEnv another environment
115
        @return this environment
116
    */
117
    Environment & SAL_CALL operator = ( const Environment & rEnv )
118
0
        { return operator = ( rEnv._pEnv ); }
119
120
#if defined LIBO_INTERNAL_ONLY
121
0
    Environment & operator =(Environment && other) {
122
0
        if (_pEnv != nullptr) {
123
0
            (*_pEnv->release)(_pEnv);
124
0
        }
125
0
        _pEnv = other._pEnv;
126
0
        other._pEnv = nullptr;
127
0
        return *this;
128
0
    }
129
#endif
130
131
    /** Provides UNacquired pointer to the set C environment.
132
133
        @return UNacquired pointer to the C environment struct
134
    */
135
    uno_Environment * SAL_CALL get() const
136
1.13M
        { return _pEnv; }
137
138
    /** Gets type name of set environment.
139
140
        @return type name of set environment
141
    */
142
    ::rtl::OUString SAL_CALL getTypeName() const
143
977k
        { return _pEnv->pTypeName; }
144
145
    /** Gets free context pointer of set environment.
146
147
        @return free context pointer of set environment
148
    */
149
    void * SAL_CALL getContext() const
150
0
        { return _pEnv->pContext; }
151
152
    /** Tests if an environment is set.
153
154
        @return true, if an environment is set, false otherwise
155
    */
156
    bool SAL_CALL is() const
157
275k
        { return (_pEnv != NULL); }
158
159
    /** Releases a set environment.
160
    */
161
    inline void SAL_CALL clear();
162
163
    /** Invoke the passed function in this environment.
164
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
168
    */
169
    inline void SAL_CALL invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const;
170
171
    /** Invoke the passed function in this environment.
172
173
        @param pCallee  the function to call
174
        @param ...      the parameters to be passed to the function
175
        @since UDK 3.2.7
176
    */
177
    inline void SAL_CALL invoke(uno_EnvCallee * pCallee, ...) const;
178
179
    /** Enter this environment explicitly.
180
181
        @since UDK 3.2.7
182
    */
183
    inline void SAL_CALL enter() const;
184
185
    /** Checks, if it is valid to currently call objects
186
        belonging to this environment.
187
188
        @since UDK 3.2.7
189
    */
190
    inline int  SAL_CALL isValid(rtl::OUString * pReason) const;
191
};
192
193
inline Environment::Environment( uno_Environment * pEnv )
194
707k
    : _pEnv( pEnv )
195
707k
{
196
707k
    if (_pEnv)
197
564k
        (*_pEnv->acquire)( _pEnv );
198
707k
}
199
200
inline Environment::Environment( rtl::OUString const & rEnvDcp, void * pContext )
201
143k
    : _pEnv(NULL)
202
143k
{
203
143k
    uno_getEnvironment(&_pEnv, rEnvDcp.pData, pContext);
204
143k
}
205
206
inline Environment::Environment( const Environment & rEnv )
207
318
    : _pEnv( rEnv._pEnv )
208
318
{
209
318
    if (_pEnv)
210
318
        (*_pEnv->acquire)( _pEnv );
211
318
}
212
213
inline Environment::~Environment()
214
851k
{
215
851k
    if (_pEnv)
216
851k
        (*_pEnv->release)( _pEnv );
217
851k
}
218
219
inline void Environment::clear()
220
0
{
221
0
    if (_pEnv)
222
0
    {
223
0
        (*_pEnv->release)( _pEnv );
224
0
        _pEnv = NULL;
225
0
    }
226
0
}
227
228
inline Environment & Environment::operator = ( uno_Environment * pEnv )
229
143k
{
230
143k
    if (pEnv != _pEnv)
231
143k
    {
232
143k
        if (pEnv)
233
143k
            (*pEnv->acquire)( pEnv );
234
143k
        if (_pEnv)
235
0
            (*_pEnv->release)( _pEnv );
236
143k
        _pEnv = pEnv;
237
143k
    }
238
143k
    return *this;
239
143k
}
240
241
inline void SAL_CALL Environment::invoke_v(uno_EnvCallee * pCallee, va_list * pParam) const
242
0
{
243
0
    if (_pEnv)
244
0
        uno_Environment_invoke_v(_pEnv, pCallee, pParam);
245
0
}
246
247
inline void SAL_CALL Environment::invoke(uno_EnvCallee * pCallee, ...) const
248
106
{
249
106
    if (_pEnv)
250
106
    {
251
106
        va_list param;
252
253
106
        va_start(param, pCallee);
254
106
        uno_Environment_invoke_v(_pEnv, pCallee, &param);
255
106
        va_end(param);
256
106
    }
257
258
106
}
259
260
inline void SAL_CALL Environment::enter() const
261
0
{
262
0
    uno_Environment_enter(_pEnv);
263
0
}
264
265
inline int  SAL_CALL Environment::isValid(rtl::OUString * pReason) const
266
0
{
267
0
    return uno_Environment_isValid(_pEnv, &pReason->pData);
268
0
}
269
270
inline Environment Environment::getCurrent(rtl::OUString const & typeName)
271
143k
{
272
143k
    Environment environment;
273
274
143k
    uno_Environment * pEnv = NULL;
275
143k
    uno_getCurrentEnvironment(&pEnv, typeName.pData);
276
143k
    environment = pEnv;
277
143k
    if (pEnv)
278
143k
        pEnv->release(pEnv);
279
280
143k
    return environment;
281
143k
}
282
283
}
284
}
285
}
286
}
287
288
#endif
289
290
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */