merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / jni_uno / jni_base.h
blobfaf3a20f15a26302310e0bb350090df92254d3bb
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_base.h,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #if ! defined INCLUDED_JNI_BASE_H
32 #define INCLUDED_JNI_BASE_H
34 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
35 // workaround solaris include trouble on jumbo
36 #include <stdarg.h>
37 namespace std
39 typedef __va_list va_list;
41 #endif
42 #include <memory>
44 #include "jvmaccess/unovirtualmachine.hxx"
45 #include "jvmaccess/virtualmachine.hxx"
47 #include "osl/diagnose.h"
49 #include "rtl/alloc.h"
50 #include "rtl/ustring.hxx"
52 #include "uno/environment.h"
53 #include "typelib/typedescription.h"
55 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
58 namespace jni_uno
61 class JNI_info;
63 //==============================================================================
64 struct BridgeRuntimeError
66 ::rtl::OUString m_message;
68 inline BridgeRuntimeError( ::rtl::OUString const & message )
69 : m_message( message )
74 //==============================================================================
75 class JNI_context
77 JNI_info const * m_jni_info;
78 JNIEnv * m_env;
79 jobject m_class_loader;
81 JNI_context( JNI_context & ); // not impl
82 void operator = ( JNI_context ); // not impl
84 void java_exc_occured() const;
85 public:
86 inline explicit JNI_context(
87 JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
88 : m_jni_info( jni_info ),
89 m_env( env ),
90 m_class_loader( class_loader )
93 inline JNI_info const * get_info() const
94 { return m_jni_info; }
96 inline JNIEnv * operator -> () const
97 { return m_env; }
98 inline JNIEnv * get_jni_env() const
99 { return m_env; }
101 // does not handle exceptions, *classClass will be null if exception
102 // occurred:
103 void getClassForName(jclass * classClass, jmethodID * methodForName) const;
105 // if inException, does not handle exceptions, in which case returned value
106 // will be null if exception occurred:
107 jclass findClass(
108 char const * name, jclass classClass, jmethodID methodForName,
109 bool inException) const;
111 inline void ensure_no_exception() const; // throws BridgeRuntimeError
112 inline bool assert_no_exception() const; // asserts and clears exception
114 ::rtl::OUString get_stack_trace( jobject jo_exc = 0 ) const;
117 //______________________________________________________________________________
118 inline void JNI_context::ensure_no_exception() const
120 if (JNI_FALSE != m_env->ExceptionCheck())
122 java_exc_occured();
126 //______________________________________________________________________________
127 inline bool JNI_context::assert_no_exception() const
129 if (JNI_FALSE != m_env->ExceptionCheck())
131 m_env->ExceptionClear();
132 OSL_ENSURE( 0, "unexpected java exception occured!" );
133 return false;
135 return true;
139 //==============================================================================
140 class JNI_guarded_context
141 : private ::jvmaccess::VirtualMachine::AttachGuard,
142 public JNI_context
144 JNI_guarded_context( JNI_guarded_context & ); // not impl
145 void operator = ( JNI_guarded_context ); // not impl
147 public:
148 inline explicit JNI_guarded_context(
149 JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
150 : AttachGuard( vm_access->getVirtualMachine() ),
151 JNI_context(
152 jni_info, AttachGuard::getEnvironment(),
153 static_cast< jobject >(vm_access->getClassLoader()) )
158 //==============================================================================
159 class JLocalAutoRef
161 JNI_context const & m_jni;
162 jobject m_jo;
164 public:
165 inline JLocalAutoRef( JNI_context const & jni )
166 : m_jni( jni ),
167 m_jo( 0 )
169 inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
170 : m_jni( jni ),
171 m_jo( jo )
173 inline JLocalAutoRef( JLocalAutoRef & auto_ref );
174 inline ~JLocalAutoRef() SAL_THROW( () );
176 inline jobject get() const
177 { return m_jo; }
178 inline bool is() const
179 { return (0 != m_jo); }
180 inline jobject release();
181 inline void reset();
182 inline void reset( jobject jo );
183 inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
186 //______________________________________________________________________________
187 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW( () )
189 if (0 != m_jo)
190 m_jni->DeleteLocalRef( m_jo );
193 //______________________________________________________________________________
194 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
195 : m_jni( auto_ref.m_jni ),
196 m_jo( auto_ref.m_jo )
198 auto_ref.m_jo = 0;
201 //______________________________________________________________________________
202 inline jobject JLocalAutoRef::release()
204 jobject jo = m_jo;
205 m_jo = 0;
206 return jo;
209 //______________________________________________________________________________
210 inline void JLocalAutoRef::reset()
212 if (0 != m_jo)
213 m_jni->DeleteLocalRef( m_jo );
214 m_jo = 0;
217 //______________________________________________________________________________
218 inline void JLocalAutoRef::reset( jobject jo )
220 if (jo != m_jo)
222 if (0 != m_jo)
223 m_jni->DeleteLocalRef( m_jo );
224 m_jo = jo;
228 //______________________________________________________________________________
229 inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
231 OSL_ASSERT( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
232 reset( auto_ref.m_jo );
233 auto_ref.m_jo = 0;
234 return *this;
238 //==============================================================================
239 struct rtl_mem
241 inline static void * operator new ( size_t nSize )
242 { return rtl_allocateMemory( nSize ); }
243 inline static void operator delete ( void * mem )
244 { if (mem) rtl_freeMemory( mem ); }
245 inline static void * operator new ( size_t, void * mem )
246 { return mem; }
247 inline static void operator delete ( void *, void * )
250 static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
253 //______________________________________________________________________________
254 inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
256 void * p = rtl_allocateMemory( bytes );
257 if (0 == p)
258 throw BridgeRuntimeError( OUSTR("out of memory!") );
259 return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
263 //==============================================================================
264 class TypeDescr
266 typelib_TypeDescription * m_td;
268 TypeDescr( TypeDescr & ); // not impl
269 void operator = ( TypeDescr ); // not impl
271 public:
272 inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
273 inline ~TypeDescr() SAL_THROW( () )
274 { TYPELIB_DANGER_RELEASE( m_td ); }
276 inline typelib_TypeDescription * get() const
277 { return m_td; }
280 //______________________________________________________________________________
281 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
282 : m_td( 0 )
284 TYPELIB_DANGER_GET( &m_td, td_ref );
285 if (0 == m_td)
287 throw BridgeRuntimeError(
288 OUSTR("cannot get comprehensive type description for ") +
289 ::rtl::OUString::unacquired( &td_ref->pTypeName ) );
295 #endif