LCOV - code coverage report
Current view: top level - libreoffice/bridges/source/jni_uno - jni_base.h (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 67 0.0 %
Date: 2012-12-27 Functions: 0 22 0.0 %
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             : #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
      45             : 
      46             : 
      47             : namespace jni_uno
      48             : {
      49             : 
      50             : class JNI_info;
      51             : 
      52             : //==============================================================================
      53           0 : struct BridgeRuntimeError
      54             : {
      55             :     ::rtl::OUString m_message;
      56             : 
      57           0 :     inline BridgeRuntimeError( ::rtl::OUString const & message )
      58           0 :         : m_message( message )
      59           0 :         {}
      60             : };
      61             : 
      62             : 
      63             : //==============================================================================
      64             : class JNI_context
      65             : {
      66             :     JNI_info const * m_jni_info;
      67             :     JNIEnv *         m_env;
      68             :     jobject          m_class_loader;
      69             : 
      70             :     JNI_context( JNI_context & ); // not impl
      71             :     void operator = ( JNI_context ); // not impl
      72             : 
      73             :     void java_exc_occurred() const;
      74             : public:
      75           0 :     inline explicit JNI_context(
      76             :         JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
      77             :         : m_jni_info( jni_info ),
      78             :           m_env( env ),
      79           0 :           m_class_loader( class_loader )
      80           0 :         {}
      81             : 
      82           0 :     inline JNI_info const * get_info() const
      83           0 :         { return m_jni_info; }
      84             : 
      85           0 :     inline JNIEnv * operator -> () const
      86           0 :         { return m_env; }
      87           0 :     inline JNIEnv * get_jni_env() const
      88           0 :         { return m_env; }
      89             : 
      90             :     // does not handle exceptions, *classClass will be null if exception
      91             :     // occurred:
      92             :     void getClassForName(jclass * classClass, jmethodID * methodForName) const;
      93             : 
      94             :     // if inException, does not handle exceptions, in which case returned value
      95             :     // will be null if exception occurred:
      96             :     jclass findClass(
      97             :         char const * name, jclass classClass, jmethodID methodForName,
      98             :         bool inException) const;
      99             : 
     100             :     inline void ensure_no_exception() const; // throws BridgeRuntimeError
     101             :     inline bool assert_no_exception() const; // asserts and clears exception
     102             : 
     103             :     ::rtl::OUString get_stack_trace( jobject jo_exc = 0 ) const;
     104             : };
     105             : 
     106             : //______________________________________________________________________________
     107           0 : inline void JNI_context::ensure_no_exception() const
     108             : {
     109           0 :     if (JNI_FALSE != m_env->ExceptionCheck())
     110             :     {
     111           0 :         java_exc_occurred();
     112             :     }
     113           0 : }
     114             : 
     115             : //______________________________________________________________________________
     116           0 : inline bool JNI_context::assert_no_exception() const
     117             : {
     118           0 :     if (JNI_FALSE != m_env->ExceptionCheck())
     119             :     {
     120           0 :         m_env->ExceptionClear();
     121             :         OSL_FAIL( "unexpected java exception occurred!" );
     122           0 :         return false;
     123             :     }
     124           0 :     return true;
     125             : }
     126             : 
     127             : 
     128             : //==============================================================================
     129           0 : class JNI_guarded_context
     130             :     : private ::jvmaccess::VirtualMachine::AttachGuard,
     131             :       public JNI_context
     132             : {
     133             :     JNI_guarded_context( JNI_guarded_context & ); // not impl
     134             :     void operator = ( JNI_guarded_context ); // not impl
     135             : 
     136             : public:
     137           0 :     inline explicit JNI_guarded_context(
     138             :         JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
     139             :         : AttachGuard( vm_access->getVirtualMachine() ),
     140             :           JNI_context(
     141             :               jni_info, AttachGuard::getEnvironment(),
     142           0 :               static_cast< jobject >(vm_access->getClassLoader()) )
     143           0 :         {}
     144             : };
     145             : 
     146             : 
     147             : //==============================================================================
     148             : class JLocalAutoRef
     149             : {
     150             :     JNI_context const & m_jni;
     151             :     jobject m_jo;
     152             : 
     153             : public:
     154           0 :     inline JLocalAutoRef( JNI_context const & jni )
     155             :         : m_jni( jni ),
     156           0 :           m_jo( 0 )
     157           0 :         {}
     158           0 :     inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
     159             :         : m_jni( jni ),
     160           0 :           m_jo( jo )
     161           0 :         {}
     162             :     inline JLocalAutoRef( JLocalAutoRef & auto_ref );
     163             :     inline ~JLocalAutoRef() SAL_THROW(());
     164             : 
     165           0 :     inline jobject get() const
     166           0 :         { return m_jo; }
     167           0 :     inline bool is() const
     168           0 :         { return (0 != m_jo); }
     169             :     inline jobject release();
     170             :     inline void reset();
     171             :     inline void reset( jobject jo );
     172             :     inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
     173             : };
     174             : 
     175             : //______________________________________________________________________________
     176           0 : inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW(())
     177             : {
     178           0 :     if (0 != m_jo)
     179           0 :         m_jni->DeleteLocalRef( m_jo );
     180           0 : }
     181             : 
     182             : //______________________________________________________________________________
     183             : inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
     184             :     : m_jni( auto_ref.m_jni ),
     185             :       m_jo( auto_ref.m_jo )
     186             : {
     187             :     auto_ref.m_jo = 0;
     188             : }
     189             : 
     190             : //______________________________________________________________________________
     191           0 : inline jobject JLocalAutoRef::release()
     192             : {
     193           0 :     jobject jo = m_jo;
     194           0 :     m_jo = 0;
     195           0 :     return jo;
     196             : }
     197             : 
     198             : //______________________________________________________________________________
     199             : inline void JLocalAutoRef::reset()
     200             : {
     201             :     if (0 != m_jo)
     202             :         m_jni->DeleteLocalRef( m_jo );
     203             :     m_jo = 0;
     204             : }
     205             : 
     206             : //______________________________________________________________________________
     207           0 : inline void JLocalAutoRef::reset( jobject jo )
     208             : {
     209           0 :     if (jo != m_jo)
     210             :     {
     211           0 :         if (0 != m_jo)
     212           0 :             m_jni->DeleteLocalRef( m_jo );
     213           0 :         m_jo = jo;
     214             :     }
     215           0 : }
     216             : 
     217             : //______________________________________________________________________________
     218             : inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
     219             : {
     220             :     OSL_ASSERT( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
     221             :     reset( auto_ref.m_jo );
     222             :     auto_ref.m_jo = 0;
     223             :     return *this;
     224             : }
     225             : 
     226             : 
     227             : //==============================================================================
     228             : struct rtl_mem
     229             : {
     230             :     inline static void * operator new ( size_t nSize )
     231             :         { return rtl_allocateMemory( nSize ); }
     232           0 :     inline static void operator delete ( void * mem )
     233           0 :         { if (mem) rtl_freeMemory( mem ); }
     234             :     inline static void * operator new ( size_t, void * mem )
     235             :         { return mem; }
     236             :     inline static void operator delete ( void *, void * )
     237             :         {}
     238             : 
     239             :     static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
     240             : };
     241             : 
     242             : //______________________________________________________________________________
     243           0 : inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
     244             : {
     245           0 :     void * p = rtl_allocateMemory( bytes );
     246           0 :     if (0 == p)
     247           0 :         throw BridgeRuntimeError( OUSTR("out of memory!") );
     248           0 :     return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
     249             : }
     250             : 
     251             : 
     252             : //==============================================================================
     253             : class TypeDescr
     254             : {
     255             :     typelib_TypeDescription * m_td;
     256             : 
     257             :     TypeDescr( TypeDescr & ); // not impl
     258             :     void operator = ( TypeDescr ); // not impl
     259             : 
     260             : public:
     261             :     inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
     262           0 :     inline ~TypeDescr() SAL_THROW(())
     263           0 :         { TYPELIB_DANGER_RELEASE( m_td ); }
     264             : 
     265           0 :     inline typelib_TypeDescription * get() const
     266           0 :         { return m_td; }
     267             : };
     268             : 
     269             : //______________________________________________________________________________
     270           0 : inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
     271           0 :     : m_td( 0 )
     272             : {
     273           0 :     TYPELIB_DANGER_GET( &m_td, td_ref );
     274           0 :     if (0 == m_td)
     275             :     {
     276             :         throw BridgeRuntimeError(
     277             :             OUSTR("cannot get comprehensive type description for ") +
     278           0 :             ::rtl::OUString::unacquired( &td_ref->pTypeName ) );
     279             :     }
     280           0 : }
     281             : 
     282             : }
     283             : 
     284             : #endif
     285             : 
     286             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10