LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_type.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 67 138 48.6 %
Date: 2014-04-11 Functions: 12 15 80.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             : #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          10 : const char *typeClassToString( TypeClass t )
      38             : {
      39          10 :     const char * ret = 0;
      40          10 :     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          10 :         ret = "INTERFACE"; break;
      82             :     case com::sun::star::uno::TypeClass_TYPEDEF:
      83           0 :         ret = "TYPEDEF"; break;
      84             :     case com::sun::star::uno::TypeClass_SERVICE:
      85           0 :         ret = "SERVICE"; break;
      86             :     case com::sun::star::uno::TypeClass_MODULE:
      87           0 :         ret = "MODULE"; break;
      88             :     case com::sun::star::uno::TypeClass_INTERFACE_METHOD:
      89           0 :         ret = "INTERFACE_METHOD"; break;
      90             :     case com::sun::star::uno::TypeClass_INTERFACE_ATTRIBUTE:
      91           0 :         ret = "INTERFACE_ATTRIBUTE"; break;
      92             :     default:
      93           0 :         ret = "UNKNOWN"; break;
      94             :     }
      95          10 :     return ret;
      96             : }
      97             : 
      98         223 : static PyRef getClass( const Runtime & r , const char * name)
      99             : {
     100         223 :     return PyRef( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*) name ) );
     101             : }
     102             : 
     103          75 : PyRef getTypeClass( const Runtime & r )
     104             : {
     105          75 :     return getClass( r , "Type" );
     106             : }
     107             : 
     108          65 : PyRef getEnumClass( const Runtime & r )
     109             : {
     110          65 :     return getClass( r , "Enum" );
     111             : }
     112             : 
     113           4 : PyRef getCharClass( const Runtime & r )
     114             : {
     115           4 :     return getClass( r , "Char" );
     116             : }
     117             : 
     118          75 : PyRef getByteSequenceClass( const Runtime & r )
     119             : {
     120          75 :     return getClass( r , "ByteSequence" );
     121             : }
     122             : 
     123           4 : PyRef getAnyClass( const Runtime & r )
     124             : {
     125           4 :     return getClass( r , "Any" );
     126             : }
     127             : 
     128             : 
     129           0 : sal_Unicode PyChar2Unicode( PyObject *obj ) throw ( RuntimeException )
     130             : {
     131           0 :     PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE );
     132           0 :     if( ! PyUnicode_Check( value.get() ) )
     133             :     {
     134             :         throw RuntimeException(
     135             :             "attribute value of uno.Char is not a unicode string",
     136           0 :             Reference< XInterface > () );
     137             :     }
     138             : 
     139           0 :     if( PyUnicode_GetSize( value.get() ) < 1 )
     140             :     {
     141             :         throw RuntimeException(
     142             :             "uno.Char contains an empty unicode string",
     143           0 :             Reference< XInterface > () );
     144             :     }
     145             : 
     146           0 :     sal_Unicode c = (sal_Unicode)PyUnicode_AsUnicode( value.get() )[0];
     147           0 :     return c;
     148             : }
     149             : 
     150          73 : Any PyEnum2Enum( PyObject *obj ) throw ( RuntimeException )
     151             : {
     152          73 :     Any ret;
     153         146 :     PyRef typeName( PyObject_GetAttrString( obj,"typeName" ), SAL_NO_ACQUIRE);
     154         146 :     PyRef value( PyObject_GetAttrString( obj, "value" ), SAL_NO_ACQUIRE);
     155          73 :     if( !PyStr_Check( typeName.get() ) || ! PyStr_Check( value.get() ) )
     156             :     {
     157             :         throw RuntimeException(
     158             :             "attributes typeName and/or value of uno.Enum are not strings",
     159           0 :             Reference< XInterface > () );
     160             :     }
     161             : 
     162         146 :     OUString strTypeName( OUString::createFromAscii( PyStr_AsString( typeName.get() ) ) );
     163          73 :     char *stringValue = PyStr_AsString( value.get() );
     164             : 
     165         146 :     TypeDescription desc( strTypeName );
     166          73 :     if( desc.is() )
     167             :     {
     168          44 :         if(desc.get()->eTypeClass != typelib_TypeClass_ENUM )
     169             :         {
     170           0 :             OUStringBuffer buf;
     171           0 :             buf.appendAscii( "pyuno.checkEnum: " ).append(strTypeName).appendAscii( "is a " );
     172             :             buf.appendAscii(
     173           0 :                 typeClassToString( (com::sun::star::uno::TypeClass) desc.get()->eTypeClass));
     174           0 :             buf.appendAscii( ", expected ENUM" );
     175           0 :             throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface>  () );
     176             :         }
     177             : 
     178          44 :         desc.makeComplete();
     179             : 
     180          44 :         typelib_EnumTypeDescription *pEnumDesc = (typelib_EnumTypeDescription*) desc.get();
     181          44 :         int i = 0;
     182         713 :         for( i = 0; i < pEnumDesc->nEnumValues ; i ++ )
     183             :         {
     184         713 :             if( (*((OUString *)&pEnumDesc->ppEnumNames[i])).equalsAscii( stringValue ) )
     185             :             {
     186          44 :                 break;
     187             :             }
     188             :         }
     189          44 :         if( i == pEnumDesc->nEnumValues )
     190             :         {
     191           0 :             OUStringBuffer buf;
     192           0 :             buf.appendAscii( "value " ).appendAscii( stringValue ).appendAscii( "is unknown in enum " );
     193           0 :             buf.appendAscii( PyStr_AsString( typeName.get() ) );
     194           0 :             throw RuntimeException( buf.makeStringAndClear(), Reference<XInterface> () );
     195             :         }
     196          44 :         ret = Any( &pEnumDesc->pEnumValues[i], desc.get()->pWeakRef );
     197             :     }
     198             :     else
     199             :     {
     200          29 :         OUStringBuffer buf;
     201          29 :         buf.appendAscii( "enum " ).appendAscii( PyStr_AsString(typeName.get()) ).appendAscii( " is unknown" );
     202          29 :         throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface>  () );
     203             :     }
     204          88 :     return ret;
     205             : }
     206             : 
     207             : 
     208          20 : Type PyType2Type( PyObject * o ) throw(RuntimeException )
     209             : {
     210          20 :     PyRef pyName( PyObject_GetAttrString( o, "typeName" ), SAL_NO_ACQUIRE);
     211          20 :     if( !PyStr_Check( pyName.get() ) )
     212             :     {
     213             :         throw RuntimeException(
     214             :             "type object does not have typeName property",
     215           0 :             Reference< XInterface > () );
     216             :     }
     217             : 
     218          40 :     PyRef pyTC( PyObject_GetAttrString( o, "typeClass" ), SAL_NO_ACQUIRE );
     219          40 :     Any enumValue = PyEnum2Enum( pyTC.get() );
     220             : 
     221          40 :     OUString name( OUString::createFromAscii( PyStr_AsString( pyName.get() ) ) );
     222          40 :     TypeDescription desc( name );
     223          20 :     if( ! desc.is() )
     224             :     {
     225           0 :         OUStringBuffer buf;
     226           0 :         buf.appendAscii( "type " ).append(name).appendAscii( " is unknown" );
     227             :         throw RuntimeException(
     228           0 :             buf.makeStringAndClear(), Reference< XInterface > () );
     229             :     }
     230          20 :     if( desc.get()->eTypeClass != (typelib_TypeClass) *(sal_Int32*)enumValue.getValue() )
     231             :     {
     232           0 :         OUStringBuffer buf;
     233           0 :         buf.appendAscii( "pyuno.checkType: " ).append(name).appendAscii( " is a " );
     234           0 :         buf.appendAscii( typeClassToString( (TypeClass) desc.get()->eTypeClass) );
     235           0 :         buf.appendAscii( ", but type got construct with typeclass " );
     236           0 :         buf.appendAscii( typeClassToString( (TypeClass) *(sal_Int32*)enumValue.getValue() ) );
     237             :         throw RuntimeException(
     238           0 :             buf.makeStringAndClear(), Reference< XInterface > () );
     239             :     }
     240          40 :     return desc.get()->pWeakRef;
     241             : }
     242             : 
     243          25 : static PyObject* callCtor( const Runtime &r , const char * clazz, const PyRef & args )
     244             : {
     245          25 :     PyRef code( PyDict_GetItemString( r.getImpl()->cargo->getUnoModule().get(), (char*)clazz ) );
     246          25 :     if( ! code.is() )
     247             :     {
     248           0 :         OStringBuffer buf;
     249           0 :         buf.append( "couldn't access uno." );
     250           0 :         buf.append( clazz );
     251           0 :         PyErr_SetString( PyExc_RuntimeError, buf.getStr() );
     252           0 :         return NULL;
     253             :     }
     254          50 :     PyRef instance( PyObject_CallObject( code.get(), args.get()  ), SAL_NO_ACQUIRE);
     255          25 :     Py_XINCREF( instance.get() );
     256          50 :     return instance.get();
     257             : 
     258             : }
     259             : 
     260             : 
     261          15 : PyObject *PyUNO_Enum_new( const char *enumBase, const char *enumValue, const Runtime &r )
     262             : {
     263          15 :     PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
     264          15 :     PyTuple_SetItem( args.get() , 0 , PyStr_FromString( enumBase ) );
     265          15 :     PyTuple_SetItem( args.get() , 1 , PyStr_FromString( enumValue ) );
     266             : 
     267          15 :     return callCtor( r, "Enum" , args );
     268             : }
     269             : 
     270             : 
     271          10 : PyObject* PyUNO_Type_new (const char *typeName , TypeClass t , const Runtime &r )
     272             : {
     273             :     // retrieve type object
     274          10 :     PyRef args( PyTuple_New( 2 ), SAL_NO_ACQUIRE );
     275             : 
     276          10 :     PyTuple_SetItem( args.get() , 0 , PyStr_FromString( typeName ) );
     277          10 :     PyObject *typeClass = PyUNO_Enum_new( "com.sun.star.uno.TypeClass" , typeClassToString(t), r );
     278          10 :     if( ! typeClass )
     279           0 :         return NULL;
     280          10 :     PyTuple_SetItem( args.get() , 1 , typeClass);
     281             : 
     282          10 :     return callCtor( r, "Type" , args );
     283             : }
     284             : 
     285           0 : PyObject* PyUNO_char_new ( sal_Unicode val , const Runtime &r )
     286             : {
     287             :     // retrieve type object
     288           0 :     PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     289             : 
     290             :     Py_UNICODE u[2];
     291           0 :     u[0] = val;
     292           0 :     u[1] = 0;
     293           0 :     PyTuple_SetItem( args.get() , 0 , PyUnicode_FromUnicode( u ,1) );
     294             : 
     295           0 :     return callCtor( r, "Char" , args );
     296             : }
     297             : 
     298           0 : PyObject *PyUNO_ByteSequence_new(
     299             :     const com::sun::star::uno::Sequence< sal_Int8 > &byteSequence, const Runtime &r )
     300             : {
     301             :     PyRef str(
     302           0 :         PyStrBytes_FromStringAndSize( (char*)byteSequence.getConstArray(), byteSequence.getLength()),
     303           0 :         SAL_NO_ACQUIRE );
     304           0 :     PyRef args( PyTuple_New( 1 ), SAL_NO_ACQUIRE );
     305           0 :     PyTuple_SetItem( args.get() , 0 , str.getAcquired() );
     306           0 :     return callCtor( r, "ByteSequence" , args );
     307             : 
     308             : }
     309             : }
     310             : 
     311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10