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 // option pricing functions add in
21 
22 // most parts of this files are technical UNO details which are
23 // all copied from ../datefunc/datefunc.hxx
24 // to avoid having to rename all classes to do with UNO
25 // technicalities we use our own namespace
26 
27 #pragma once
28 
29 
30 #include <vector>
31 #include <memory>
32 #include <com/sun/star/lang/XServiceName.hpp>
33 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include <com/sun/star/sheet/XAddIn.hpp>
35 #include <com/sun/star/sheet/XCompatibilityNames.hpp>
36 #include <com/sun/star/sheet/addin/XPricingFunctions.hpp>
37 #include <cppuhelper/implbase.hxx>
38 
39 namespace com::sun::star::lang { class XMultiServiceFactory; }
40 
41 #define RETURN_FINITE(d)    if( !std::isfinite( d ) ) throw css::lang::IllegalArgumentException(); return d;
42 
43 
44 namespace sca::pricing {
45 
46 enum class ScaCategory
47 {
48     DateTime,
49     Text,
50     Finance,
51     Inf,
52     Math,
53     Tech
54 };
55 
56 struct ScaFuncDataBase
57 {
58     const char*                 pIntName;           // internal name (get***)
59     const char*                 pUINameID;          // resource ID to UI name
60     const char**                pDescrID;           // resource ID to description, parameter names and ~ description
61     // pCompName was originally meant to be able to load Excel documents that for
62     // some time were stored with localized function names.
63     // This is not relevant to this add-in, so we only supply the same
64     // (English) function names again.
65     // see also: GetExcelName() or GetCompNames() or getCompatibilityNames()
66     const char*                 pCompName;
67     sal_uInt16                  nParamCount;        // number of named / described parameters
68     ScaCategory                 eCat;               // function category
69     bool                        bDouble;            // name already exist in Calc
70     bool                        bWithOpt;           // first parameter is internal
71 };
72 
73 class ScaFuncData final
74 {
75 private:
76     OUString                aIntName;           // internal name (get***)
77     const char*             pUINameID;          // resource ID to UI name
78     const char**            pDescrID;           // leads also to parameter descriptions!
79     sal_uInt16              nParamCount;        // num of parameters
80     std::vector<OUString>   aCompList;          // list of all valid names
81     ScaCategory             eCat;               // function category
82     bool                    bDouble;            // name already exist in Calc
83     bool                    bWithOpt;           // first parameter is internal
84 
85 public:
86     ScaFuncData(const ScaFuncDataBase& rBaseData);
87 
GetUINameID() const88     const char*          GetUINameID() const     { return pUINameID; }
GetDescrID() const89     const char**         GetDescrID() const      { return pDescrID; }
GetCategory() const90     ScaCategory          GetCategory() const     { return eCat; }
IsDouble() const91     bool                 IsDouble() const        { return bDouble; }
92 
93     sal_uInt16                  GetStrIndex( sal_uInt16 nParam ) const;
Is(std::u16string_view rCompare) const94     bool                 Is( std::u16string_view rCompare ) const
95                                                     { return aIntName == rCompare; }
96 
GetCompNameList() const97     const std::vector<OUString>& GetCompNameList() const { return aCompList; }
98 };
99 
100 
101 typedef std::vector<ScaFuncData> ScaFuncDataList;
102 
103 void InitScaFuncDataList(ScaFuncDataList& rMap);
104 
105 // Predicate for use with std::find_if
106 struct FindScaFuncData
107 {
108     const OUString& m_rId;
FindScaFuncDatasca::pricing::FindScaFuncData109     explicit FindScaFuncData( const OUString& rId ) : m_rId(rId) {}
operator ()sca::pricing::FindScaFuncData110     bool operator() ( ScaFuncData const & rCandidate ) const { return rCandidate.Is(m_rId); }
111 };
112 
113 } // namespace sca::pricing
114 
115 
116 // AddIn class for pricing functions
117 
118 class ScaPricingAddIn : public ::cppu::WeakImplHelper<
119                                 css::sheet::XAddIn,
120                                 css::sheet::XCompatibilityNames,
121                                 css::sheet::addin::XPricingFunctions,
122                                 css::lang::XServiceName,
123                                 css::lang::XServiceInfo >
124 {
125 private:
126     css::lang::Locale  aFuncLoc;
127     std::unique_ptr<css::lang::Locale[]> pDefLocales;
128     std::locale        aResLocale;
129     std::unique_ptr<sca::pricing::ScaFuncDataList> pFuncDataList;
130 
131 
132     void                        InitDefLocales();
133     const css::lang::Locale& GetLocale( sal_uInt32 nIndex );
134     void                        InitData();
135 
136     /// @throws css::uno::RuntimeException
137     OUString             GetFuncDescrStr(const char** pResId, sal_uInt16 nStrIndex);
138 
139 public:
140                                 ScaPricingAddIn();
141     virtual                     ~ScaPricingAddIn() override;
142 
143     OUString ScaResId(const char* pResId);
144 
145                                 // XAddIn
146     virtual OUString SAL_CALL getProgrammaticFuntionName( const OUString& aDisplayName ) override;
147     virtual OUString SAL_CALL getDisplayFunctionName( const OUString& aProgrammaticName ) override;
148     virtual OUString SAL_CALL getFunctionDescription( const OUString& aProgrammaticName ) override;
149     virtual OUString SAL_CALL getDisplayArgumentName( const OUString& aProgrammaticName, sal_Int32 nArgument ) override;
150     virtual OUString SAL_CALL getArgumentDescription( const OUString& aProgrammaticName, sal_Int32 nArgument ) override;
151     virtual OUString SAL_CALL getProgrammaticCategoryName( const OUString& aProgrammaticName ) override;
152     virtual OUString SAL_CALL getDisplayCategoryName( const OUString& aProgrammaticName ) override;
153 
154                                 // XCompatibilityNames
155     virtual css::uno::Sequence< css::sheet::LocalizedName > SAL_CALL getCompatibilityNames( const OUString& aProgrammaticName ) override;
156 
157                                 // XLocalizable
158     virtual void SAL_CALL       setLocale( const css::lang::Locale& eLocale ) override;
159     virtual css::lang::Locale SAL_CALL getLocale() override;
160 
161                                 // XServiceName
162     virtual OUString SAL_CALL getServiceName() override;
163 
164                                 // XServiceInfo
165     virtual OUString SAL_CALL getImplementationName() override;
166     virtual sal_Bool SAL_CALL   supportsService( const OUString& ServiceName ) override;
167     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
168 
169 
170     //  methods from own interfaces start here
171 
172 
173     virtual double SAL_CALL getOptBarrier( double spot, double vol,
174             double r, double rf, double T, double strike,
175             double barrier_low, double barrier_up, double rebate,
176             const OUString& put_call, const OUString& in_out,
177             const OUString& continuous, const css::uno::Any& greek ) override;
178 
179    virtual double SAL_CALL getOptTouch( double spot, double vol,
180             double r, double rf, double T,
181             double barrier_low, double barrier_up,
182             const OUString& for_dom, const OUString& in_out,
183             const OUString& barriercont, const css::uno::Any& greekstr ) override;
184 
185    virtual double SAL_CALL getOptProbHit( double spot, double vol,
186             double mu, double T,
187             double barrier_low, double barrier_up ) override;
188 
189    virtual double SAL_CALL getOptProbInMoney( double spot, double vol,
190             double mu, double T,
191             double barrier_low, double barrier_up,
192             const css::uno::Any& strikeval, const css::uno::Any& put_call ) override;
193 
194 };
195 
196 
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
198