Bump version to 4.1-6
[LibreOffice.git] / bridges / source / jni_uno / jni_base.h
blob61fc2847958598f080bb7ec51f2b4acacb14377f
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 .
20 #ifndef INCLUDED_JNI_BASE_H
21 #define INCLUDED_JNI_BASE_H
23 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
24 // workaround solaris include trouble on jumbo
25 #include <stdarg.h>
26 namespace std
28 typedef __va_list va_list;
30 #endif
31 #include <memory>
33 #include "jvmaccess/unovirtualmachine.hxx"
34 #include "jvmaccess/virtualmachine.hxx"
36 #include "osl/diagnose.h"
38 #include "rtl/alloc.h"
39 #include "rtl/ustring.hxx"
41 #include "uno/environment.h"
42 #include "typelib/typedescription.h"
45 namespace jni_uno
48 class JNI_info;
50 //==============================================================================
51 struct BridgeRuntimeError
53 OUString m_message;
55 inline BridgeRuntimeError( OUString const & message )
56 : m_message( message )
61 //==============================================================================
62 class JNI_context
64 JNI_info const * m_jni_info;
65 JNIEnv * m_env;
66 jobject m_class_loader;
68 JNI_context( JNI_context & ); // not impl
69 void operator = ( JNI_context ); // not impl
71 void java_exc_occurred() const;
72 public:
73 inline explicit JNI_context(
74 JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
75 : m_jni_info( jni_info ),
76 m_env( env ),
77 m_class_loader( class_loader )
80 inline JNI_info const * get_info() const
81 { return m_jni_info; }
83 inline JNIEnv * operator -> () const
84 { return m_env; }
85 inline JNIEnv * get_jni_env() const
86 { return m_env; }
88 // does not handle exceptions, *classClass will be null if exception
89 // occurred:
90 void getClassForName(jclass * classClass, jmethodID * methodForName) const;
92 // if inException, does not handle exceptions, in which case returned value
93 // will be null if exception occurred:
94 jclass findClass(
95 char const * name, jclass classClass, jmethodID methodForName,
96 bool inException) const;
98 inline void ensure_no_exception() const; // throws BridgeRuntimeError
99 inline bool assert_no_exception() const; // asserts and clears exception
101 OUString get_stack_trace( jobject jo_exc = 0 ) const;
104 //______________________________________________________________________________
105 inline void JNI_context::ensure_no_exception() const
107 if (JNI_FALSE != m_env->ExceptionCheck())
109 java_exc_occurred();
113 //______________________________________________________________________________
114 inline bool JNI_context::assert_no_exception() const
116 if (JNI_FALSE != m_env->ExceptionCheck())
118 m_env->ExceptionClear();
119 OSL_FAIL( "unexpected java exception occurred!" );
120 return false;
122 return true;
126 //==============================================================================
127 class JNI_guarded_context
128 : private ::jvmaccess::VirtualMachine::AttachGuard,
129 public JNI_context
131 JNI_guarded_context( JNI_guarded_context & ); // not impl
132 void operator = ( JNI_guarded_context ); // not impl
134 public:
135 inline explicit JNI_guarded_context(
136 JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
137 : AttachGuard( vm_access->getVirtualMachine() ),
138 JNI_context(
139 jni_info, AttachGuard::getEnvironment(),
140 static_cast< jobject >(vm_access->getClassLoader()) )
145 //==============================================================================
146 class JLocalAutoRef
148 JNI_context const & m_jni;
149 jobject m_jo;
151 public:
152 inline JLocalAutoRef( JNI_context const & jni )
153 : m_jni( jni ),
154 m_jo( 0 )
156 inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
157 : m_jni( jni ),
158 m_jo( jo )
160 inline JLocalAutoRef( JLocalAutoRef & auto_ref );
161 inline ~JLocalAutoRef() SAL_THROW(());
163 inline jobject get() const
164 { return m_jo; }
165 inline bool is() const
166 { return (0 != m_jo); }
167 inline jobject release();
168 inline void reset();
169 inline void reset( jobject jo );
170 inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
173 //______________________________________________________________________________
174 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
176 if (0 != m_jo)
177 m_jni->DeleteLocalRef( m_jo );
180 //______________________________________________________________________________
181 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
182 : m_jni( auto_ref.m_jni ),
183 m_jo( auto_ref.m_jo )
185 auto_ref.m_jo = 0;
188 //______________________________________________________________________________
189 inline jobject JLocalAutoRef::release()
191 jobject jo = m_jo;
192 m_jo = 0;
193 return jo;
196 //______________________________________________________________________________
197 inline void JLocalAutoRef::reset()
199 if (0 != m_jo)
200 m_jni->DeleteLocalRef( m_jo );
201 m_jo = 0;
204 //______________________________________________________________________________
205 inline void JLocalAutoRef::reset( jobject jo )
207 if (jo != m_jo)
209 if (0 != m_jo)
210 m_jni->DeleteLocalRef( m_jo );
211 m_jo = jo;
215 //______________________________________________________________________________
216 inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
218 OSL_ASSERT( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
219 reset( auto_ref.m_jo );
220 auto_ref.m_jo = 0;
221 return *this;
225 //==============================================================================
226 struct rtl_mem
228 inline static void * operator new ( size_t nSize )
229 { return rtl_allocateMemory( nSize ); }
230 inline static void operator delete ( void * mem )
231 { if (mem) rtl_freeMemory( mem ); }
232 inline static void * operator new ( size_t, void * mem )
233 { return mem; }
234 inline static void operator delete ( void *, void * )
237 static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
240 //______________________________________________________________________________
241 inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
243 void * p = rtl_allocateMemory( bytes );
244 if (0 == p)
245 throw BridgeRuntimeError( "out of memory!" );
246 return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
250 //==============================================================================
251 class TypeDescr
253 typelib_TypeDescription * m_td;
255 TypeDescr( TypeDescr & ); // not impl
256 void operator = ( TypeDescr ); // not impl
258 public:
259 inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
260 inline ~TypeDescr() SAL_THROW(())
261 { TYPELIB_DANGER_RELEASE( m_td ); }
263 inline typelib_TypeDescription * get() const
264 { return m_td; }
267 //______________________________________________________________________________
268 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
269 : m_td( 0 )
271 TYPELIB_DANGER_GET( &m_td, td_ref );
272 if (0 == m_td)
274 throw BridgeRuntimeError(
275 "cannot get comprehensive type description for " +
276 OUString::unacquired( &td_ref->pTypeName ) );
282 #endif
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */