LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/bridges/source/jni_uno - jni_base.h (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 53 68 77.9 %
Date: 2013-07-09 Functions: 18 22 81.8 %
Legend: Lines: hit not hit

          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             : 
      20             : #ifndef INCLUDED_JNI_BASE_H
      21             : #define INCLUDED_JNI_BASE_H
      22             : 
      23             : #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
      24             : // workaround solaris include trouble on jumbo
      25             : #include <stdarg.h>
      26             : namespace std
      27             : {
      28             : typedef __va_list va_list;
      29             : }
      30             : #endif
      31             : #include <memory>
      32             : 
      33             : #include "jvmaccess/unovirtualmachine.hxx"
      34             : #include "jvmaccess/virtualmachine.hxx"
      35             : 
      36             : #include "osl/diagnose.h"
      37             : 
      38             : #include "rtl/alloc.h"
      39             : #include "rtl/ustring.hxx"
      40             : 
      41             : #include "uno/environment.h"
      42             : #include "typelib/typedescription.h"
      43             : 
      44             : 
      45             : namespace jni_uno
      46             : {
      47             : 
      48             : class JNI_info;
      49             : 
      50             : //==============================================================================
      51           0 : struct BridgeRuntimeError
      52             : {
      53             :     OUString m_message;
      54             : 
      55           0 :     inline BridgeRuntimeError( OUString const & message )
      56           0 :         : m_message( message )
      57           0 :         {}
      58             : };
      59             : 
      60             : 
      61             : //==============================================================================
      62             : class JNI_context
      63             : {
      64             :     JNI_info const * m_jni_info;
      65             :     JNIEnv *         m_env;
      66             :     jobject          m_class_loader;
      67             : 
      68             :     JNI_context( JNI_context & ); // not impl
      69             :     void operator = ( JNI_context ); // not impl
      70             : 
      71             :     void java_exc_occurred() const;
      72             : public:
      73         108 :     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         108 :           m_class_loader( class_loader )
      78         108 :         {}
      79             : 
      80         153 :     inline JNI_info const * get_info() const
      81         153 :         { return m_jni_info; }
      82             : 
      83        1391 :     inline JNIEnv * operator -> () const
      84        1391 :         { return m_env; }
      85           0 :     inline JNIEnv * get_jni_env() const
      86           0 :         { return m_env; }
      87             : 
      88             :     // does not handle exceptions, *classClass will be null if exception
      89             :     // occurred:
      90             :     void getClassForName(jclass * classClass, jmethodID * methodForName) const;
      91             : 
      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;
      97             : 
      98             :     inline void ensure_no_exception() const; // throws BridgeRuntimeError
      99             :     inline bool assert_no_exception() const; // asserts and clears exception
     100             : 
     101             :     OUString get_stack_trace( jobject jo_exc = 0 ) const;
     102             : };
     103             : 
     104             : //______________________________________________________________________________
     105         555 : inline void JNI_context::ensure_no_exception() const
     106             : {
     107         555 :     if (JNI_FALSE != m_env->ExceptionCheck())
     108             :     {
     109           0 :         java_exc_occurred();
     110             :     }
     111         555 : }
     112             : 
     113             : //______________________________________________________________________________
     114           0 : inline bool JNI_context::assert_no_exception() const
     115             : {
     116           0 :     if (JNI_FALSE != m_env->ExceptionCheck())
     117             :     {
     118           0 :         m_env->ExceptionClear();
     119             :         OSL_FAIL( "unexpected java exception occurred!" );
     120           0 :         return false;
     121             :     }
     122           0 :     return true;
     123             : }
     124             : 
     125             : 
     126             : //==============================================================================
     127          69 : class JNI_guarded_context
     128             :     : private ::jvmaccess::VirtualMachine::AttachGuard,
     129             :       public JNI_context
     130             : {
     131             :     JNI_guarded_context( JNI_guarded_context & ); // not impl
     132             :     void operator = ( JNI_guarded_context ); // not impl
     133             : 
     134             : public:
     135          69 :     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          69 :               static_cast< jobject >(vm_access->getClassLoader()) )
     141          69 :         {}
     142             : };
     143             : 
     144             : 
     145             : //==============================================================================
     146             : class JLocalAutoRef
     147             : {
     148             :     JNI_context const & m_jni;
     149             :     jobject m_jo;
     150             : 
     151             : public:
     152          95 :     inline JLocalAutoRef( JNI_context const & jni )
     153             :         : m_jni( jni ),
     154          95 :           m_jo( 0 )
     155          95 :         {}
     156         351 :     inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
     157             :         : m_jni( jni ),
     158         351 :           m_jo( jo )
     159         351 :         {}
     160             :     inline JLocalAutoRef( JLocalAutoRef & auto_ref );
     161             :     inline ~JLocalAutoRef() SAL_THROW(());
     162             : 
     163         672 :     inline jobject get() const
     164         672 :         { return m_jo; }
     165         101 :     inline bool is() const
     166         101 :         { 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 );
     171             : };
     172             : 
     173             : //______________________________________________________________________________
     174         446 : inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
     175             : {
     176         446 :     if (0 != m_jo)
     177         368 :         m_jni->DeleteLocalRef( m_jo );
     178         446 : }
     179             : 
     180             : //______________________________________________________________________________
     181             : inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
     182             :     : m_jni( auto_ref.m_jni ),
     183             :       m_jo( auto_ref.m_jo )
     184             : {
     185             :     auto_ref.m_jo = 0;
     186             : }
     187             : 
     188             : //______________________________________________________________________________
     189          26 : inline jobject JLocalAutoRef::release()
     190             : {
     191          26 :     jobject jo = m_jo;
     192          26 :     m_jo = 0;
     193          26 :     return jo;
     194             : }
     195             : 
     196             : //______________________________________________________________________________
     197             : inline void JLocalAutoRef::reset()
     198             : {
     199             :     if (0 != m_jo)
     200             :         m_jni->DeleteLocalRef( m_jo );
     201             :     m_jo = 0;
     202             : }
     203             : 
     204             : //______________________________________________________________________________
     205          49 : inline void JLocalAutoRef::reset( jobject jo )
     206             : {
     207          49 :     if (jo != m_jo)
     208             :     {
     209          49 :         if (0 != m_jo)
     210           3 :             m_jni->DeleteLocalRef( m_jo );
     211          49 :         m_jo = jo;
     212             :     }
     213          49 : }
     214             : 
     215             : //______________________________________________________________________________
     216             : inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
     217             : {
     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;
     222             : }
     223             : 
     224             : 
     225             : //==============================================================================
     226             : struct rtl_mem
     227             : {
     228             :     inline static void * operator new ( size_t nSize )
     229             :         { return rtl_allocateMemory( nSize ); }
     230         202 :     inline static void operator delete ( void * mem )
     231         202 :         { 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 * )
     235             :         {}
     236             : 
     237             :     static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
     238             : };
     239             : 
     240             : //______________________________________________________________________________
     241          64 : inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
     242             : {
     243          64 :     void * p = rtl_allocateMemory( bytes );
     244          64 :     if (0 == p)
     245           0 :         throw BridgeRuntimeError( "out of memory!" );
     246          64 :     return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
     247             : }
     248             : 
     249             : 
     250             : //==============================================================================
     251             : class TypeDescr
     252             : {
     253             :     typelib_TypeDescription * m_td;
     254             : 
     255             :     TypeDescr( TypeDescr & ); // not impl
     256             :     void operator = ( TypeDescr ); // not impl
     257             : 
     258             : public:
     259             :     inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
     260         186 :     inline ~TypeDescr() SAL_THROW(())
     261         186 :         { TYPELIB_DANGER_RELEASE( m_td ); }
     262             : 
     263         356 :     inline typelib_TypeDescription * get() const
     264         356 :         { return m_td; }
     265             : };
     266             : 
     267             : //______________________________________________________________________________
     268         186 : inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
     269         186 :     : m_td( 0 )
     270             : {
     271         186 :     TYPELIB_DANGER_GET( &m_td, td_ref );
     272         186 :     if (0 == m_td)
     273             :     {
     274             :         throw BridgeRuntimeError(
     275           0 :             "cannot get comprehensive type description for " +
     276           0 :             OUString::unacquired( &td_ref->pTypeName ) );
     277             :     }
     278         186 : }
     279             : 
     280             : }
     281             : 
     282             : #endif
     283             : 
     284             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10