Bump for 3.6-28
[LibreOffice.git] / bridges / source / jni_uno / jni_base.h
blobe9345a32aacf095fad0fbfc5e1e94e874e37331a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #if ! defined INCLUDED_JNI_BASE_H
30 #define INCLUDED_JNI_BASE_H
32 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
33 // workaround solaris include trouble on jumbo
34 #include <stdarg.h>
35 namespace std
37 typedef __va_list va_list;
39 #endif
40 #include <memory>
42 #include "jvmaccess/unovirtualmachine.hxx"
43 #include "jvmaccess/virtualmachine.hxx"
45 #include "osl/diagnose.h"
47 #include "rtl/alloc.h"
48 #include "rtl/ustring.hxx"
50 #include "uno/environment.h"
51 #include "typelib/typedescription.h"
53 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
56 namespace jni_uno
59 class JNI_info;
61 //==============================================================================
62 struct BridgeRuntimeError
64 ::rtl::OUString m_message;
66 inline BridgeRuntimeError( ::rtl::OUString const & message )
67 : m_message( message )
72 //==============================================================================
73 class JNI_context
75 JNI_info const * m_jni_info;
76 JNIEnv * m_env;
77 jobject m_class_loader;
79 JNI_context( JNI_context & ); // not impl
80 void operator = ( JNI_context ); // not impl
82 void java_exc_occurred() const;
83 public:
84 inline explicit JNI_context(
85 JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
86 : m_jni_info( jni_info ),
87 m_env( env ),
88 m_class_loader( class_loader )
91 inline JNI_info const * get_info() const
92 { return m_jni_info; }
94 inline JNIEnv * operator -> () const
95 { return m_env; }
96 inline JNIEnv * get_jni_env() const
97 { return m_env; }
99 // does not handle exceptions, *classClass will be null if exception
100 // occurred:
101 void getClassForName(jclass * classClass, jmethodID * methodForName) const;
103 // if inException, does not handle exceptions, in which case returned value
104 // will be null if exception occurred:
105 jclass findClass(
106 char const * name, jclass classClass, jmethodID methodForName,
107 bool inException) const;
109 inline void ensure_no_exception() const; // throws BridgeRuntimeError
110 inline bool assert_no_exception() const; // asserts and clears exception
112 ::rtl::OUString get_stack_trace( jobject jo_exc = 0 ) const;
115 //______________________________________________________________________________
116 inline void JNI_context::ensure_no_exception() const
118 if (JNI_FALSE != m_env->ExceptionCheck())
120 java_exc_occurred();
124 //______________________________________________________________________________
125 inline bool JNI_context::assert_no_exception() const
127 if (JNI_FALSE != m_env->ExceptionCheck())
129 m_env->ExceptionClear();
130 OSL_FAIL( "unexpected java exception occurred!" );
131 return false;
133 return true;
137 //==============================================================================
138 class JNI_guarded_context
139 : private ::jvmaccess::VirtualMachine::AttachGuard,
140 public JNI_context
142 JNI_guarded_context( JNI_guarded_context & ); // not impl
143 void operator = ( JNI_guarded_context ); // not impl
145 public:
146 inline explicit JNI_guarded_context(
147 JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
148 : AttachGuard( vm_access->getVirtualMachine() ),
149 JNI_context(
150 jni_info, AttachGuard::getEnvironment(),
151 static_cast< jobject >(vm_access->getClassLoader()) )
156 //==============================================================================
157 class JLocalAutoRef
159 JNI_context const & m_jni;
160 jobject m_jo;
162 public:
163 inline JLocalAutoRef( JNI_context const & jni )
164 : m_jni( jni ),
165 m_jo( 0 )
167 inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
168 : m_jni( jni ),
169 m_jo( jo )
171 inline JLocalAutoRef( JLocalAutoRef & auto_ref );
172 inline ~JLocalAutoRef() SAL_THROW(());
174 inline jobject get() const
175 { return m_jo; }
176 inline bool is() const
177 { return (0 != m_jo); }
178 inline jobject release();
179 inline void reset();
180 inline void reset( jobject jo );
181 inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
184 //______________________________________________________________________________
185 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
187 if (0 != m_jo)
188 m_jni->DeleteLocalRef( m_jo );
191 //______________________________________________________________________________
192 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
193 : m_jni( auto_ref.m_jni ),
194 m_jo( auto_ref.m_jo )
196 auto_ref.m_jo = 0;
199 //______________________________________________________________________________
200 inline jobject JLocalAutoRef::release()
202 jobject jo = m_jo;
203 m_jo = 0;
204 return jo;
207 //______________________________________________________________________________
208 inline void JLocalAutoRef::reset()
210 if (0 != m_jo)
211 m_jni->DeleteLocalRef( m_jo );
212 m_jo = 0;
215 //______________________________________________________________________________
216 inline void JLocalAutoRef::reset( jobject jo )
218 if (jo != m_jo)
220 if (0 != m_jo)
221 m_jni->DeleteLocalRef( m_jo );
222 m_jo = jo;
226 //______________________________________________________________________________
227 inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
229 OSL_ASSERT( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
230 reset( auto_ref.m_jo );
231 auto_ref.m_jo = 0;
232 return *this;
236 //==============================================================================
237 struct rtl_mem
239 inline static void * operator new ( size_t nSize )
240 { return rtl_allocateMemory( nSize ); }
241 inline static void operator delete ( void * mem )
242 { if (mem) rtl_freeMemory( mem ); }
243 inline static void * operator new ( size_t, void * mem )
244 { return mem; }
245 inline static void operator delete ( void *, void * )
248 static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
251 //______________________________________________________________________________
252 inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
254 void * p = rtl_allocateMemory( bytes );
255 if (0 == p)
256 throw BridgeRuntimeError( OUSTR("out of memory!") );
257 return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
261 //==============================================================================
262 class TypeDescr
264 typelib_TypeDescription * m_td;
266 TypeDescr( TypeDescr & ); // not impl
267 void operator = ( TypeDescr ); // not impl
269 public:
270 inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
271 inline ~TypeDescr() SAL_THROW(())
272 { TYPELIB_DANGER_RELEASE( m_td ); }
274 inline typelib_TypeDescription * get() const
275 { return m_td; }
278 //______________________________________________________________________________
279 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
280 : m_td( 0 )
282 TYPELIB_DANGER_GET( &m_td, td_ref );
283 if (0 == m_td)
285 throw BridgeRuntimeError(
286 OUSTR("cannot get comprehensive type description for ") +
287 ::rtl::OUString::unacquired( &td_ref->pTypeName ) );
293 #endif
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */