LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/pyuno/source/module - pyuno_type.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 73 140 52.1 %
Date: 2013-07-09 Functions: 13 15 86.7 %
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             : #include "pyuno_impl.hxx"
      20             : 
      21             : #include <rtl/ustrbuf.hxx>
      22             : #include <rtl/strbuf.hxx>
      23             : 
      24             : #include <typelib/typedescription.hxx>
      25             : 
      26             : 
      27             : using com::sun::star::uno::TypeClass;
      28             : using com::sun::star::uno::Type;
      29             : using com::sun::star::uno::RuntimeException;
      30             : using com::sun::star::uno::Any;
      31             : using com::sun::star::uno::XInterface;
      32             : using com::sun::star::uno::Reference;
      33             : using com::sun::star::uno::TypeDescription;
      34             : 
      35             : namespace pyuno
      36             : {
      37           2 : const char *typeClassToString( TypeClass t )
      38             : {
      39           2 :     const char * ret = 0;
      40           2 :     switch (t)
      41             :     {
      42             :     case com::sun::star::uno::TypeClass_VOID:
      43           0 :         ret = "VOID"; break;
      44             :     case com::sun::star::uno::TypeClass_CHAR:
      45           0 :         ret = "CHAR"; break;
      46             :     case com::sun::star::uno::TypeClass_BOOLEAN:
      47           0 :         ret = "BOOLEAN"; break;
      48             :     case com::sun::star::uno::TypeClass_BYTE:
      49           0 :         ret = "BYTE"; break;
      50             :     case com::sun::star::uno::TypeClass_SHORT:
      51           0 :         ret = "SHORT"; break;
      52             :     case com::sun::star::uno::TypeClass_UNSIGNED_SHORT:
      53           0 :         ret = "UNSIGNED_SHORT"; break;
      54             :     case com::sun::star::uno::TypeClass_LONG:
      55           0 :         ret = "LONG"; break;
      56             :     case com::sun::star::uno::TypeClass_UNSIGNED_LONG:
      57           0 :         ret = "UNSIGNED_LONG"; break;
      58             :     case com::sun::star::uno::TypeClass_HYPER:
      59           0 :         ret = "HYPER"; break;
      60             :     case com::sun::star::uno::TypeClass_UNSIGNED_HYPER:
      61           0 :         ret = "UNSIGNED_HYPER"; break;
      62             :     case com::sun::star::uno::TypeClass_FLOAT:
      63           0 :         ret = "FLOAT"; break;
      64             :     case com::sun::star::uno::TypeClass_DOUBLE:
      65           0 :         ret = "DOUBLE"; break;
      66             :     case com::sun::star::uno::TypeClass_STRING:
      67           0 :         ret = "STRING"; break;
      68             :     case com::sun::star::uno::TypeClass_TYPE:
      69           0 :         ret = "TYPE"; break;
      70             :     case com::sun::star::uno::TypeClass_ANY:
      71           0 :         ret = "ANY";break;
      72             :     case com::sun::star::uno::TypeClass_ENUM:
      73           0 :         ret = "ENUM";break;
      74             :     case com::sun::star::uno::TypeClass_STRUCT:
      75           0 :         ret = "STRUCT"; break;
      76             :     case com::sun::star::uno::TypeClass_EXCEPTION:
      77           0 :         ret = "EXCEPTION"; break;
      78             :     case com::sun::star::uno::TypeClass_SEQUENCE:
      79           0 :         ret = "SEQUENCE"; break;
      80             :     case com::sun::star::uno::TypeClass_INTERFACE:
      81           2 :         ret = "INTERFACE"; break;
      82             :     case com::sun::star::uno::TypeClass_TYPEDEF:
      83           0 :         ret = "TYPEDEF"; break;
      84             :     case com::sun::star::uno::TypeClass_UNION:
      85           0 :         ret = "UNION"; break;
      86             :     case com::sun::star::uno::TypeClass_ARRAY:
      87           0 :         ret = "ARRAY"; break;
      88             :     case com::sun::star::uno::TypeClass_SERVICE:
      89           0 :         ret = "SERVICE"; break;
      90             :     case com::sun::star::uno::TypeClass_MODULE:
      91           0 :         ret = "MODULE"; break;
      92             :     case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
      93           0 :         ret = "INTERFACE_METHOD"; break;
      94             :     case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
      95           0 :         ret = "INTERFACE_ATTRIBUTE"; break;
      96             :     default:
      97           0 :         ret = "UNKNOWN"; break;
      98             :     }
      99           2 :     return ret;
     100             : }
     101             : 
     102         132 : static PyRef getClass( const Runtime & r , const char * name)
     103             : {
     104         132 :     return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
     105             : }
     106             : 
     107          44 : PyRef getTypeClass( const Runtime & r )
     108             : {
     109          44 :     return getClass( r , "Type" );
     110             : }
     111             : 
     112          42 : PyRef getEnumClass( const Runtime & r )
     113             : {
     114          42 :     return getClass( r , "Enum" );
     115             : }
     116             : 
     117           1 : PyRef getCharClass( const Runtime & r )
     118             : {
     119           1 :     return getClass( r , "Char" );
     120             : }
     121             : 
     122          44 : PyRef getByteSequenceClass( const Runtime & r )
     123             : {
     124          44 :     return getClass( r , "ByteSequence" );
     125             : }
     126             : 
     127           1 : PyRef getAnyClass( const Runtime & r )
     128             : {
     129           1 :     return getClass( r , "Any" );
     130             : }
     131             : 
     132             : 
     133           0 : sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
     134             : {
     135           0 :     PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
     136           0 :     if( ! PyUnicode_Check( value.get() ) )
     137             :     {
     138             :         throw RuntimeException(
     139             :             "attribute value of uno.Char is not a unicode string",
     140           0 :             Reference< XInterface > () );
     141             :     }
     142             : 
     143           0 :     if( PyUnicode_GetSize( value.get() ) < 1 )
     144             :     {
     145             :         throw RuntimeException(
     146             :             "uno.Char contains an empty unicode string",
     147           0 :             Reference< XInterface > () );
     148             :     }
     149             : 
     150           0 :     sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
     151           0 :     return c;
     152             : }
     153             : 
     154          21 : Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
     155             : {
     156          21 :     Any ret;
     157          42 :     PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
     158          42 :     PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
     159          21 :     if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
     160             :     {
     161             :         throw RuntimeException(
     162             :             "attributes typeName and/or value of uno.Enum are not strings",
     163           0 :             Reference< XInterface > () );
     164             :     }
     165             : 
     166          42 :     OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
     167          21 :     char *stringValue = PyStr_AsString( value.get() );
     168             : 
     169          42 :     TypeDescription desc( strTypeName );
     170          21 :     if( desc.is() )
     171             :     {
     172           9 :         if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
     173             :         {
     174           0 :             OUStringBuffer buf;
     175           0 :             buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
     176             :             buf.appendAscii(
     177           0 :                 typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
     178           0 :             buf.appendAscii( ", expected ENUM" );
     179           0 :             throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface>  () );
     180             :         }
     181             : 
     182           9 :         desc.makeComplete();
     183             : 
     184           9 :         typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
     185           9 :         int i = 0;
     186         144 :         for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
     187             :         {
     188         144 :             if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).compareToAscii( stringValue ) == 0 )
     189             :             {
     190           9 :                 break;
     191             :             }
     192             :         }
     193           9 :         if( i == pEnumDesc->nEnumValues )
     194             :         {
     195           0 :             OUStringBuffer buf;
     196           0 :             buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
     197           0 :             buf.appendAscii( PyStr_AsString( typeName.get() ) );
     198           0 :             throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
     199             :         }
     200           9 :         ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
     201             :     }
     202             :     else
     203             :     {
     204          12 :         OUStringBuffer buf;
     205          12 :         buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
     206          12 :         throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface>  () );
     207             :     }
     208          18 :     return ret;
     209             : }
     210             : 
     211             : 
     212           4 : Type PyType2Type( PyObject * o ) throw(RuntimeException )
     213             : {
     214           4 :     PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
     215           4 :     if( !PyStr_Check( pyName.get() ) )
     216             :     {
     217             :         throw RuntimeException(
     218             :             "type object does not have typeName property",
     219           0 :             Reference< XInterface > () );
     220             :     }
     221             : 
     222           8 :     PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
     223           8 :     Any enumValue = PyEnum2Enum( pyTC.get() );
     224             : 
     225           8 :     OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
     226           8 :     TypeDescription desc( name );
     227           4 :     if( ! desc.is() )
     228             :     {
     229           0 :         OUStringBuffer buf;
     230           0 :         buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
     231             :         throw RuntimeException(
     232           0 :             buf.makeStringAndClear(), Reference< XInterface > () );
     233             :     }
     234           4 :     if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
     235             :     {
     236           0 :         OUStringBuffer buf;
     237           0 :         buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
     238           0 :         buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
     239           0 :         buf.appendAscii( ", but type got construct with typeclass " );
     240           0 :         buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
     241             :         throw RuntimeException(
     242           0 :             buf.makeStringAndClear(), Reference< XInterface > () );
     243             :     }
     244           8 :     return desc.get()->pWeakRef;
     245             : }
     246             : 
     247           5 : static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
     248             : {
     249           5 :     PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
     250           5 :     if( ! code.is() )
     251             :     {
     252           0 :         OStringBuffer buf;
     253           0 :         buf.append( "couldn't access uno." );
     254           0 :         buf.append( clazz );
     255           0 :         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     256           0 :         return NULL;
     257             :     }
     258          10 :     PyRef instance( PyObject_CallObject( code.get(), args.get()  ), SAL_NO_ACQUIRE);
     259           5 :     Py_XINCREF( instance.get() );
     260          10 :     return instance.get();
     261             : 
     262             : }
     263             : 
     264             : 
     265           2 : PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
     266             : {
     267           2 :     PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
     268           2 :     PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
     269           2 :     PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
     270             : 
     271           2 :     return callCtor( r, "Enum" , args );
     272             : }
     273             : 
     274             : 
     275           2 : PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
     276             : {
     277             :     // retrieve type object
     278           2 :     PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
     279             : 
     280           2 :     PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
     281           2 :     PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
     282           2 :     if( ! typeClass )
     283           0 :         return NULL;
     284           2 :     PyTuple_SetItem( args.get() , 1 , typeClass);
     285             : 
     286           2 :     return callCtor( r, "Type" , args );
     287             : }
     288             : 
     289           0 : PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
     290             : {
     291             :     // retrieve type object
     292           0 :     PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     293             : 
     294             :     Py_UNICODE u[2];
     295           0 :     u[0] = val;
     296           0 :     u[1] = 0;
     297           0 :     PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
     298             : 
     299           0 :     return callCtor( r, "Char" , args );
     300             : }
     301             : 
     302           1 : PyObject *PyUNO_ByteSequence_new(
     303             :     const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
     304             : {
     305             :     PyRef str(
     306           1 :         PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
     307           1 :         SAL_NO_ACQUIRE );
     308           2 :     PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     309           1 :     PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
     310           2 :     return callCtor( r, "ByteSequence" , args );
     311             : 
     312             : }
     313             : }
     314             : 
     315             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10