LCOV - code coverage report
Current view: top level - libreoffice/scaddins/source/pricing - pricing.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 310 0.0 %
Date: 2012-12-17 Functions: 0 56 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             : //------------------------------------------------------------------
      21             : //
      22             : // pricing functions add in
      23             : //
      24             : // all of the UNO add-in technical details have been copied from
      25             : // ../datefunc/datefunc.cxx
      26             : //
      27             : //------------------------------------------------------------------
      28             : 
      29             : #include "pricing.hxx"
      30             : #include "black_scholes.hxx"
      31             : #include "pricing.hrc"
      32             : #include <cppuhelper/factory.hxx>
      33             : #include <osl/diagnose.h>
      34             : #include <rtl/ustrbuf.hxx>
      35             : #include <rtl/math.hxx>
      36             : #include <tools/resmgr.hxx>
      37             : #include <tools/rcid.h>
      38             : 
      39             : #include <iostream>
      40             : 
      41             : using namespace ::com::sun::star;
      42             : using namespace sca::pricing;
      43             : 
      44             : 
      45             : //------------------------------------------------------------------
      46             : 
      47             : #define ADDIN_SERVICE           "com.sun.star.sheet.AddIn"
      48             : #define MY_SERVICE              "com.sun.star.sheet.addin.PricingFunctions"
      49             : #define MY_IMPLNAME             "com.sun.star.sheet.addin.PricingFunctionsImpl"
      50             : 
      51             : //------------------------------------------------------------------
      52             : 
      53             : #define STR_FROM_ANSI( s )      OUString( s, strlen( s ), RTL_TEXTENCODING_MS_1252 )
      54             : 
      55             : //------------------------------------------------------------------
      56             : 
      57             : const sal_uInt32 ScaList::nStartSize = 16;
      58             : const sal_uInt32 ScaList::nIncrSize = 16;
      59             : 
      60           0 : ScaList::ScaList() :
      61           0 :     pData( new void*[ nStartSize ] ),
      62             :     nSize( nStartSize ),
      63             :     nCount( 0 ),
      64           0 :     nCurr( 0 )
      65             : {
      66           0 : }
      67             : 
      68           0 : ScaList::~ScaList()
      69             : {
      70           0 :     delete[] pData;
      71           0 : }
      72             : 
      73           0 : void ScaList::_Grow()
      74             : {
      75           0 :     nSize += nIncrSize;
      76             : 
      77           0 :     void** pNewData = new void*[ nSize ];
      78           0 :     memcpy( pNewData, pData, nCount * sizeof( void* ) );
      79             : 
      80           0 :     delete[] pData;
      81           0 :     pData = pNewData;
      82           0 : }
      83             : 
      84             : //------------------------------------------------------------------
      85             : 
      86           0 : ScaStringList::~ScaStringList()
      87             : {
      88           0 :     for( OUString* pStr = First(); pStr; pStr = Next() )
      89           0 :         delete pStr;
      90           0 : }
      91             : 
      92             : //------------------------------------------------------------------
      93             : 
      94           0 : ScaResId::ScaResId( sal_uInt16 nId, ResMgr& rResMgr ) :
      95           0 :     ResId( nId, rResMgr )
      96             : {
      97           0 : }
      98             : 
      99             : 
     100             : //------------------------------------------------------------------
     101             : 
     102             : #define UNIQUE              sal_False   // function name does not exist in Calc
     103             : #define DOUBLE              sal_True    // function name exists in Calc
     104             : 
     105             : #define STDPAR              sal_False   // all parameters are described
     106             : #define INTPAR              sal_True    // first parameter is internal
     107             : 
     108             : #define FUNCDATA( FuncName, ParamCount, Category, Double, IntPar )  \
     109             :     { "get" #FuncName, PRICING_FUNCNAME_##FuncName, PRICING_FUNCDESC_##FuncName, PRICING_DEFFUNCNAME_##FuncName, ParamCount, Category, Double, IntPar }
     110             : 
     111             : const ScaFuncDataBase pFuncDataArr[] =
     112             : {
     113             :    FUNCDATA( OptBarrier,       13, ScaCat_Finance,    UNIQUE,  STDPAR),
     114             :    FUNCDATA( OptTouch,         11, ScaCat_Finance,    UNIQUE,  STDPAR),
     115             :    FUNCDATA( OptProbHit,        6, ScaCat_Finance,    UNIQUE,  STDPAR),
     116             :    FUNCDATA( OptProbInMoney,    8, ScaCat_Finance,    UNIQUE,  STDPAR)
     117             : };
     118             : 
     119             : #undef FUNCDATA
     120             : 
     121             : 
     122             : //------------------------------------------------------------------
     123             : 
     124           0 : ScaFuncData::ScaFuncData( const ScaFuncDataBase& rBaseData, ResMgr& rResMgr ) :
     125             :     aIntName( OUString::createFromAscii( rBaseData.pIntName ) ),
     126             :     nUINameID( rBaseData.nUINameID ),
     127             :     nDescrID( rBaseData.nDescrID ),
     128             :     nCompListID( rBaseData.nCompListID ),
     129             :     nParamCount( rBaseData.nParamCount ),
     130             :     eCat( rBaseData.eCat ),
     131             :     bDouble( rBaseData.bDouble ),
     132           0 :     bWithOpt( rBaseData.bWithOpt )
     133             : {
     134           0 :     ScaResStringArrLoader aArrLoader( RID_PRICING_DEFFUNCTION_NAMES, nCompListID, rResMgr );
     135           0 :     const ResStringArray& rArr = aArrLoader.GetStringArray();
     136             : 
     137           0 :     for( sal_uInt16 nIndex = 0; nIndex < rArr.Count(); nIndex++ )
     138           0 :         aCompList.Append( rArr.GetString( nIndex ) );
     139           0 : }
     140             : 
     141           0 : ScaFuncData::~ScaFuncData()
     142             : {
     143           0 : }
     144             : 
     145           0 : sal_uInt16 ScaFuncData::GetStrIndex( sal_uInt16 nParam ) const
     146             : {
     147           0 :     if( !bWithOpt )
     148           0 :         nParam++;
     149           0 :     return (nParam > nParamCount) ? (nParamCount * 2) : (nParam * 2);
     150             : }
     151             : 
     152             : 
     153             : //------------------------------------------------------------------
     154             : 
     155           0 : ScaFuncDataList::ScaFuncDataList( ResMgr& rResMgr ) :
     156           0 :     nLast( 0xFFFFFFFF )
     157             : {
     158           0 :     for( sal_uInt16 nIndex = 0; nIndex < SAL_N_ELEMENTS(pFuncDataArr); nIndex++ )
     159           0 :         Append( new ScaFuncData( pFuncDataArr[ nIndex ], rResMgr ) );
     160           0 : }
     161             : 
     162           0 : ScaFuncDataList::~ScaFuncDataList()
     163             : {
     164           0 :     for( ScaFuncData* pFData = First(); pFData; pFData = Next() )
     165           0 :         delete pFData;
     166           0 : }
     167             : 
     168           0 : const ScaFuncData* ScaFuncDataList::Get( const OUString& rProgrammaticName ) const
     169             : {
     170           0 :     OUString aTestName;
     171             : 
     172           0 :     if( aLastName == rProgrammaticName ){
     173           0 :         return Get( nLast );
     174             :     }
     175             : 
     176           0 :     for( sal_uInt32 nIndex = 0; nIndex < Count(); nIndex++ )
     177             :     {
     178           0 :         const ScaFuncData* pCurr = Get( nIndex );
     179           0 :         if( pCurr->Is( rProgrammaticName ) )
     180             :         {
     181           0 :             const_cast< ScaFuncDataList* >( this )->aLastName = rProgrammaticName;
     182           0 :             const_cast< ScaFuncDataList* >( this )->nLast = nIndex;
     183           0 :             return pCurr;
     184             :         }
     185             :     }
     186           0 :     return NULL;
     187             : }
     188             : 
     189             : 
     190             : //------------------------------------------------------------------
     191             : 
     192           0 : ScaFuncRes::ScaFuncRes( ResId& rResId, ResMgr& rResMgr, sal_uInt16 nIndex, OUString& rRet ) :
     193           0 :     Resource( rResId )
     194             : {
     195           0 :     rRet = String( ScaResId( nIndex, rResMgr ) );
     196           0 :     FreeResource();
     197           0 : }
     198             : 
     199             : 
     200             : //------------------------------------------------------------------
     201             : //
     202             : //  entry points for service registration / instantiation
     203             : //
     204             : //------------------------------------------------------------------
     205             : 
     206           0 : uno::Reference< uno::XInterface > SAL_CALL ScaPricingAddIn_CreateInstance(
     207             :         const uno::Reference< lang::XMultiServiceFactory >& )
     208             : {
     209           0 :     static uno::Reference< uno::XInterface > xInst = (cppu::OWeakObject*) new ScaPricingAddIn();
     210           0 :     return xInst;
     211             : }
     212             : 
     213             : 
     214             : //------------------------------------------------------------------------
     215             : 
     216             : extern "C" {
     217             : 
     218           0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL pricing_component_getFactory(
     219             :     const sal_Char * pImplName, void * pServiceManager, void * /*pRegistryKey*/ )
     220             : {
     221           0 :     void* pRet = 0;
     222             : 
     223           0 :     if ( pServiceManager &&
     224           0 :             OUString::createFromAscii( pImplName ) == ScaPricingAddIn::getImplementationName_Static() )
     225             :     {
     226             :         uno::Reference< lang::XSingleServiceFactory > xFactory( cppu::createOneInstanceFactory(
     227             :                 reinterpret_cast< lang::XMultiServiceFactory* >( pServiceManager ),
     228             :                 ScaPricingAddIn::getImplementationName_Static(),
     229             :                 ScaPricingAddIn_CreateInstance,
     230           0 :                 ScaPricingAddIn::getSupportedServiceNames_Static() ) );
     231             : 
     232           0 :         if (xFactory.is())
     233             :         {
     234           0 :             xFactory->acquire();
     235           0 :             pRet = xFactory.get();
     236           0 :         }
     237             :     }
     238             : 
     239           0 :     return pRet;
     240             : }
     241             : 
     242             : }   // extern C
     243             : 
     244             : //------------------------------------------------------------------------
     245             : //
     246             : //  "normal" service implementation
     247             : //
     248             : //------------------------------------------------------------------------
     249             : 
     250           0 : ScaPricingAddIn::ScaPricingAddIn() :
     251             :     pDefLocales( NULL ),
     252             :     pResMgr( NULL ),
     253           0 :     pFuncDataList( NULL )
     254             : {
     255           0 : }
     256             : 
     257           0 : ScaPricingAddIn::~ScaPricingAddIn()
     258             : {
     259           0 :     if( pFuncDataList )
     260           0 :         delete pFuncDataList;
     261           0 :     if( pDefLocales )
     262           0 :         delete[] pDefLocales;
     263             : 
     264             :     // pResMgr already deleted (_all_ resource managers are deleted _before_ this dtor is called)
     265           0 : }
     266             : 
     267             : static const sal_Char*  pLang[] = { "de", "en" };
     268             : static const sal_Char*  pCoun[] = { "DE", "US" };
     269             : static const sal_uInt32 nNumOfLoc = SAL_N_ELEMENTS( pLang );
     270             : 
     271           0 : void ScaPricingAddIn::InitDefLocales()
     272             : {
     273           0 :     pDefLocales = new lang::Locale[ nNumOfLoc ];
     274             : 
     275           0 :     for( sal_uInt32 nIndex = 0; nIndex < nNumOfLoc; nIndex++ )
     276             :     {
     277           0 :         pDefLocales[ nIndex ].Language = OUString::createFromAscii( pLang[ nIndex ] );
     278           0 :         pDefLocales[ nIndex ].Country = OUString::createFromAscii( pCoun[ nIndex ] );
     279             :     }
     280           0 : }
     281             : 
     282           0 : const lang::Locale& ScaPricingAddIn::GetLocale( sal_uInt32 nIndex )
     283             : {
     284           0 :     if( !pDefLocales )
     285           0 :         InitDefLocales();
     286             : 
     287           0 :     return (nIndex < sizeof( pLang )) ? pDefLocales[ nIndex ] : aFuncLoc;
     288             : }
     289             : 
     290           0 : ResMgr& ScaPricingAddIn::GetResMgr() throw( uno::RuntimeException )
     291             : {
     292           0 :     if( !pResMgr )
     293             :     {
     294           0 :         InitData();     // try to get resource manager
     295           0 :         if( !pResMgr )
     296           0 :             throw uno::RuntimeException();
     297             :     }
     298           0 :     return *pResMgr;
     299             : }
     300             : 
     301           0 : void ScaPricingAddIn::InitData()
     302             : {
     303             : 
     304           0 :     if( pResMgr )
     305           0 :         delete pResMgr;
     306             : 
     307           0 :     OString aModName( "pricing" );
     308           0 :     pResMgr = ResMgr::CreateResMgr( aModName.getStr(), aFuncLoc );
     309             : 
     310           0 :     if( pFuncDataList )
     311           0 :         delete pFuncDataList;
     312             : 
     313           0 :     pFuncDataList = pResMgr ? new ScaFuncDataList( *pResMgr ) : NULL;
     314             : 
     315           0 :     if( pDefLocales )
     316             :     {
     317           0 :         delete pDefLocales;
     318           0 :         pDefLocales = NULL;
     319           0 :     }
     320           0 : }
     321             : 
     322           0 : OUString ScaPricingAddIn::GetDisplFuncStr( sal_uInt16 nResId ) throw( uno::RuntimeException )
     323             : {
     324           0 :     return ScaResStringLoader( RID_PRICING_FUNCTION_NAMES, nResId, GetResMgr() ).GetString();
     325             : }
     326             : 
     327           0 : OUString ScaPricingAddIn::GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) throw( uno::RuntimeException )
     328             : {
     329           0 :     OUString aRet;
     330             : 
     331           0 :     ScaResPublisher aResPubl( ScaResId( RID_PRICING_FUNCTION_DESCRIPTIONS, GetResMgr() ) );
     332           0 :     ScaResId aResId( nResId, GetResMgr() );
     333           0 :     aResId.SetRT( RSC_RESOURCE );
     334             : 
     335           0 :     if( aResPubl.IsAvailableRes( aResId ) )
     336           0 :         ScaFuncRes aSubRes( aResId, GetResMgr(), nStrIndex, aRet );
     337             : 
     338           0 :     aResPubl.FreeResource();
     339           0 :     return aRet;
     340             : }
     341             : 
     342             : 
     343             : //------------------------------------------------------------------------
     344             : 
     345           0 : OUString ScaPricingAddIn::getImplementationName_Static()
     346             : {
     347           0 :     return OUString(RTL_CONSTASCII_USTRINGPARAM( MY_IMPLNAME ));
     348             : }
     349             : 
     350           0 : uno::Sequence< OUString > ScaPricingAddIn::getSupportedServiceNames_Static()
     351             : {
     352           0 :     uno::Sequence< OUString > aRet( 2 );
     353           0 :     OUString* pArray = aRet.getArray();
     354           0 :     pArray[0] = OUString(RTL_CONSTASCII_USTRINGPARAM( ADDIN_SERVICE ));
     355           0 :     pArray[1] = OUString(RTL_CONSTASCII_USTRINGPARAM( MY_SERVICE ));
     356           0 :     return aRet;
     357             : }
     358             : 
     359             : // XServiceName
     360             : 
     361           0 : OUString SAL_CALL ScaPricingAddIn::getServiceName() throw( uno::RuntimeException )
     362             : {
     363             :     // name of specific AddIn service
     364           0 :     return OUString(RTL_CONSTASCII_USTRINGPARAM( MY_SERVICE ));
     365             : }
     366             : 
     367             : // XServiceInfo
     368             : 
     369           0 : OUString SAL_CALL ScaPricingAddIn::getImplementationName() throw( uno::RuntimeException )
     370             : {
     371           0 :     return getImplementationName_Static();
     372             : }
     373             : 
     374           0 : sal_Bool SAL_CALL ScaPricingAddIn::supportsService( const OUString& aServiceName ) throw( uno::RuntimeException )
     375             : {
     376           0 :     return aServiceName == ADDIN_SERVICE || aServiceName == MY_SERVICE;
     377             : }
     378             : 
     379           0 : uno::Sequence< OUString > SAL_CALL ScaPricingAddIn::getSupportedServiceNames() throw( uno::RuntimeException )
     380             : {
     381           0 :     return getSupportedServiceNames_Static();
     382             : }
     383             : 
     384             : // XLocalizable
     385             : 
     386           0 : void SAL_CALL ScaPricingAddIn::setLocale( const lang::Locale& eLocale ) throw( uno::RuntimeException )
     387             : {
     388           0 :     aFuncLoc = eLocale;
     389           0 :     InitData();     // change of locale invalidates resources!
     390           0 : }
     391             : 
     392           0 : lang::Locale SAL_CALL ScaPricingAddIn::getLocale() throw( uno::RuntimeException )
     393             : {
     394           0 :     return aFuncLoc;
     395             : }
     396             : 
     397             : //------------------------------------------------------------------
     398             : //
     399             : //  function descriptions start here
     400             : //
     401             : //------------------------------------------------------------------
     402             : 
     403             : // XAddIn
     404           0 : OUString SAL_CALL ScaPricingAddIn::getProgrammaticFuntionName( const OUString& ) throw( uno::RuntimeException )
     405             : {
     406             :     //  not used by calc
     407             :     //  (but should be implemented for other uses of the AddIn service)
     408           0 :     return OUString();
     409             : }
     410             : 
     411           0 : OUString SAL_CALL ScaPricingAddIn::getDisplayFunctionName( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
     412             : {
     413           0 :     OUString aRet;
     414             : 
     415           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     416           0 :     if( pFData )
     417             :     {
     418           0 :         aRet = GetDisplFuncStr( pFData->GetUINameID() );
     419           0 :         if( pFData->IsDouble() )
     420           0 :             aRet += STR_FROM_ANSI( "_ADD" );
     421             :     }
     422             :     else
     423             :     {
     424           0 :         aRet = STR_FROM_ANSI( "UNKNOWNFUNC_" );
     425           0 :         aRet += aProgrammaticName;
     426             :     }
     427             : 
     428           0 :     return aRet;
     429             : }
     430             : 
     431           0 : OUString SAL_CALL ScaPricingAddIn::getFunctionDescription( const OUString& aProgrammaticName ) throw( uno::RuntimeException )
     432             : {
     433           0 :     OUString aRet;
     434             : 
     435           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     436           0 :     if( pFData )
     437           0 :         aRet = GetFuncDescrStr( pFData->GetDescrID(), 1 );
     438             : 
     439           0 :     return aRet;
     440             : }
     441             : 
     442           0 : OUString SAL_CALL ScaPricingAddIn::getDisplayArgumentName(
     443             :         const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( uno::RuntimeException )
     444             : {
     445           0 :     OUString aRet;
     446             : 
     447           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     448           0 :     if( pFData && (nArgument <= 0xFFFF) )
     449             :     {
     450           0 :         sal_uInt16 nStr = pFData->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
     451           0 :         if( nStr )
     452           0 :             aRet = GetFuncDescrStr( pFData->GetDescrID(), nStr );
     453             :         else
     454           0 :             aRet = STR_FROM_ANSI( "internal" );
     455             :     }
     456             : 
     457           0 :     return aRet;
     458             : }
     459             : 
     460           0 : OUString SAL_CALL ScaPricingAddIn::getArgumentDescription(
     461             :         const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( uno::RuntimeException )
     462             : {
     463           0 :     OUString aRet;
     464             : 
     465           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     466           0 :     if( pFData && (nArgument <= 0xFFFF) )
     467             :     {
     468           0 :         sal_uInt16 nStr = pFData->GetStrIndex( static_cast< sal_uInt16 >( nArgument ) );
     469           0 :         if( nStr )
     470           0 :             aRet = GetFuncDescrStr( pFData->GetDescrID(), nStr + 1 );
     471             :         else
     472           0 :             aRet = STR_FROM_ANSI( "for internal use only" );
     473             :     }
     474             : 
     475           0 :     return aRet;
     476             : }
     477             : 
     478           0 : OUString SAL_CALL ScaPricingAddIn::getProgrammaticCategoryName(
     479             :         const OUString& aProgrammaticName ) throw( uno::RuntimeException )
     480             : {
     481           0 :     OUString aRet;
     482             : 
     483           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     484           0 :     if( pFData )
     485             :     {
     486           0 :         switch( pFData->GetCategory() )
     487             :         {
     488           0 :             case ScaCat_DateTime:   aRet = STR_FROM_ANSI( "Date&Time" );    break;
     489           0 :             case ScaCat_Text:       aRet = STR_FROM_ANSI( "Text" );         break;
     490           0 :             case ScaCat_Finance:    aRet = STR_FROM_ANSI( "Financial" );    break;
     491           0 :             case ScaCat_Inf:        aRet = STR_FROM_ANSI( "Information" );  break;
     492           0 :             case ScaCat_Math:       aRet = STR_FROM_ANSI( "Mathematical" ); break;
     493           0 :             case ScaCat_Tech:       aRet = STR_FROM_ANSI( "Technical" );    break;
     494             :             default:    // to prevent compiler warnings
     495           0 :                 break;
     496             :         }
     497             :     }
     498             : 
     499           0 :     if( aRet.isEmpty() )
     500           0 :         aRet = STR_FROM_ANSI( "Add-In" );
     501           0 :     return aRet;
     502             : }
     503             : 
     504           0 : OUString SAL_CALL ScaPricingAddIn::getDisplayCategoryName(
     505             :         const OUString& aProgrammaticName ) throw( uno::RuntimeException )
     506             : {
     507           0 :     return getProgrammaticCategoryName( aProgrammaticName );
     508             : }
     509             : 
     510             : 
     511             : // XCompatibilityNames
     512             : 
     513           0 : uno::Sequence< sheet::LocalizedName > SAL_CALL ScaPricingAddIn::getCompatibilityNames(
     514             :         const OUString& aProgrammaticName ) throw( uno::RuntimeException )
     515             : {
     516           0 :     const ScaFuncData* pFData = pFuncDataList->Get( aProgrammaticName );
     517           0 :     if( !pFData )
     518           0 :         return uno::Sequence< sheet::LocalizedName >( 0 );
     519             : 
     520           0 :     const ScaStringList& rStrList = pFData->GetCompNameList();
     521           0 :     sal_uInt32 nCount = rStrList.Count();
     522             : 
     523           0 :     uno::Sequence< sheet::LocalizedName > aRet( nCount );
     524           0 :     sheet::LocalizedName* pArray = aRet.getArray();
     525             : 
     526           0 :     for( sal_uInt32 nIndex = 0; nIndex < nCount; nIndex++ )
     527           0 :         pArray[ nIndex ] = sheet::LocalizedName( GetLocale( nIndex ), *rStrList.Get( nIndex ) );
     528             : 
     529           0 :     return aRet;
     530             : }
     531             : 
     532             : 
     533             : 
     534             : // ---------------------------------------------------------------------
     535             : // ---------------------------------------------------------------------
     536             : // actual function implementation starts here
     537             : //
     538             : 
     539             : // auxillary input handling functions
     540             : namespace {
     541             : 
     542           0 : bool getinput_putcall(bs::types::PutCall& pc, const STRING& str) {
     543           0 :     if(str.compareToAscii("c",1)==0) {
     544           0 :         pc=bs::types::Call;
     545           0 :     } else if(str.compareToAscii("p",1)==0) {
     546           0 :         pc=bs::types::Put;
     547             :     } else {
     548           0 :         return false;
     549             :     }
     550           0 :     return true;
     551             : }
     552             : 
     553           0 : bool getinput_putcall(bs::types::PutCall& pc, const ANY& anyval) {
     554           0 :     STRING str;
     555           0 :     if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_STRING) {
     556           0 :         anyval >>= str;
     557           0 :     } else if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_VOID) {
     558           0 :         str="c";        // call as default
     559             :     } else {
     560           0 :         return false;
     561             :     }
     562           0 :     return getinput_putcall(pc, str);
     563             : }
     564             : 
     565           0 : bool getinput_strike(double& strike, const ANY& anyval) {
     566           0 :     if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_DOUBLE) {
     567           0 :         anyval >>= strike;
     568           0 :     } else if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_VOID) {
     569           0 :         strike=-1.0;        // -1 as default (means not set)
     570             :     } else {
     571           0 :         return false;
     572             :     }
     573           0 :     return true;
     574             : }
     575             : 
     576           0 : bool getinput_inout(bs::types::BarrierKIO& kio, const STRING& str) {
     577           0 :     if(str.compareToAscii("i",1)==0) {
     578           0 :         kio=bs::types::KnockIn;
     579           0 :     } else if(str.compareToAscii("o",1)==0) {
     580           0 :         kio=bs::types::KnockOut;
     581             :     } else {
     582           0 :         return false;
     583             :     }
     584           0 :     return true;
     585             : }
     586             : 
     587           0 : bool getinput_barrier(bs::types::BarrierActive& cont, const STRING& str) {
     588           0 :     if(str.compareToAscii("c",1)==0) {
     589           0 :         cont=bs::types::Continuous;
     590           0 :     } else if(str.compareToAscii("e",1)==0) {
     591           0 :         cont=bs::types::Maturity;
     592             :     } else {
     593           0 :         return false;
     594             :     }
     595           0 :     return true;
     596             : }
     597             : 
     598           0 : bool getinput_fordom(bs::types::ForDom& fd, const STRING& str) {
     599           0 :     if(str.compareToAscii("f",1)==0) {
     600           0 :         fd=bs::types::Foreign;
     601           0 :     } else if(str.compareToAscii("d",1)==0) {
     602           0 :         fd=bs::types::Domestic;
     603             :     } else {
     604           0 :         return false;
     605             :     }
     606           0 :     return true;
     607             : }
     608             : 
     609           0 : bool getinput_greek(bs::types::Greeks& greek, const ANY& anyval) {
     610           0 :     STRING str;
     611           0 :     if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_STRING) {
     612           0 :         anyval >>= str;
     613           0 :     } else if(anyval.getValueTypeClass() == ::com::sun::star::uno::TypeClass_VOID) {
     614           0 :         str="value";
     615             :     } else {
     616           0 :         return false;
     617             :     }
     618             : 
     619           0 :     if(str.compareToAscii("value")==0 || str.compareToAscii("price")==0 ||
     620           0 :        str.compareToAscii("v")==0 || str.compareToAscii("p")==0 ) {
     621           0 :         greek=bs::types::Value;
     622           0 :     } else if(str.compareToAscii("delta")==0||str.compareToAscii("d")==0) {
     623           0 :         greek=bs::types::Delta;
     624           0 :     } else if(str.compareToAscii("gamma")==0||str.compareToAscii("g")==0) {
     625           0 :         greek=bs::types::Gamma;
     626           0 :     } else if(str.compareToAscii("theta")==0||str.compareToAscii("t")==0) {
     627           0 :         greek=bs::types::Theta;
     628           0 :     } else if(str.compareToAscii("vega")==0||str.compareToAscii("e")==0) {
     629           0 :         greek=bs::types::Vega;
     630           0 :     } else if(str.compareToAscii("volga")==0||str.compareToAscii("o")==0) {
     631           0 :         greek=bs::types::Volga;
     632           0 :     } else if(str.compareToAscii("vanna")==0||str.compareToAscii("a")==0) {
     633           0 :         greek=bs::types::Vanna;
     634           0 :     } else if(str.compareToAscii("rho")==0||str.compareToAscii("r")==0) {
     635           0 :         greek=bs::types::Rho_d;
     636           0 :     } else if(str.compareToAscii("rhof")==0||str.compareToAscii("f")==0) {
     637           0 :         greek=bs::types::Rho_f;
     638             :     } else {
     639           0 :         return false;
     640             :     }
     641           0 :     return true;
     642             : }
     643             : 
     644             : } // namespace for auxillary functions
     645             : 
     646             : 
     647             : // OPT_BARRIER(...)
     648           0 : double SAL_CALL ScaPricingAddIn::getOptBarrier( double spot, double vol,
     649             :             double r, double rf, double T, double strike,
     650             :             double barrier_low, double barrier_up, double rebate,
     651             :             const STRING& put_call, const STRING& in_out,
     652             :             const STRING& barriercont, const ANY& greekstr ) THROWDEF_RTE_IAE
     653             : {
     654             :     bs::types::PutCall pc;
     655             :     bs::types::BarrierKIO kio;
     656             :     bs::types::BarrierActive bcont;
     657             :     bs::types::Greeks greek;
     658             :     // read and check input values
     659           0 :     if( spot<=0.0 || vol<=0.0 || T<0.0 || strike<0.0 ||
     660           0 :                 !getinput_putcall(pc,put_call) ||
     661           0 :                 !getinput_inout(kio,in_out) ||
     662           0 :                 !getinput_barrier(bcont,barriercont) ||
     663           0 :                 !getinput_greek(greek,greekstr) ){
     664           0 :         THROW_IAE;
     665             :     }
     666             : 
     667             :     double fRet=bs::barrier(spot,vol,r,rf,T,strike, barrier_low,barrier_up,
     668           0 :                             rebate,pc,kio,bcont,greek);
     669             : 
     670           0 :     RETURN_FINITE( fRet );
     671             : }
     672             : 
     673             : // OPT_TOUCH(...)
     674           0 : double SAL_CALL ScaPricingAddIn::getOptTouch( double spot, double vol,
     675             :             double r, double rf, double T,
     676             :             double barrier_low, double barrier_up,
     677             :             const STRING& for_dom, const STRING& in_out,
     678             :             const STRING& barriercont, const ANY& greekstr ) THROWDEF_RTE_IAE
     679             : {
     680             :     bs::types::ForDom fd;
     681             :     bs::types::BarrierKIO kio;
     682             :     bs::types::BarrierActive bcont;
     683             :     bs::types::Greeks greek;
     684             :     // read and check input values
     685           0 :     if( spot<=0.0 || vol<=0.0 || T<0.0 ||
     686           0 :                 !getinput_fordom(fd,for_dom) ||
     687           0 :                 !getinput_inout(kio,in_out) ||
     688           0 :                 !getinput_barrier(bcont,barriercont) ||
     689           0 :                 !getinput_greek(greek,greekstr) ){
     690           0 :         THROW_IAE;
     691             :     }
     692             : 
     693             :     double fRet=bs::touch(spot,vol,r,rf,T,barrier_low,barrier_up,
     694           0 :                             fd,kio,bcont,greek);
     695             : 
     696           0 :     RETURN_FINITE( fRet );
     697             : }
     698             : 
     699             : // OPT_PRB_HIT(...)
     700           0 : double SAL_CALL ScaPricingAddIn::getOptProbHit( double spot, double vol,
     701             :             double mu, double T,
     702             :             double barrier_low, double barrier_up ) THROWDEF_RTE_IAE
     703             : {
     704             :     // read and check input values
     705           0 :     if( spot<=0.0 || vol<=0.0 || T<0.0 ) {
     706           0 :         THROW_IAE;
     707             :     }
     708             : 
     709           0 :     double fRet=bs::prob_hit(spot,vol,mu,T,barrier_low,barrier_up);
     710             : 
     711           0 :     RETURN_FINITE( fRet );
     712             : }
     713             : 
     714             : // OPT_PROB_INMONEY(...)
     715           0 : double SAL_CALL ScaPricingAddIn::getOptProbInMoney( double spot, double vol,
     716             :             double mu, double T,
     717             :             double barrier_low, double barrier_up,
     718             :             const ANY& strikeval, const ANY& put_call ) THROWDEF_RTE_IAE
     719             : {
     720           0 :     bs::types::PutCall pc=bs::types::Call;
     721             :     double  K;
     722             : 
     723             :     // read and check input values
     724           0 :     if( spot<=0.0 || vol<=0.0 || T<0.0 ||
     725           0 :             !getinput_putcall(pc,put_call) ||
     726           0 :             !getinput_strike(K,strikeval) ) {
     727           0 :         THROW_IAE;
     728             :     }
     729             : 
     730           0 :     double fRet=bs::prob_in_money(spot,vol,mu,T,K,barrier_low,barrier_up,pc);
     731             : 
     732           0 :     RETURN_FINITE( fRet );
     733           0 : }
     734             : 
     735             : 
     736             : 
     737             : //------------------------------------------------------------------
     738             : 
     739             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10