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 : // 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 : #ifndef INCLUDED_SCADDINS_SOURCE_PRICING_PRICING_HXX
28 : #define INCLUDED_SCADDINS_SOURCE_PRICING_PRICING_HXX
29 :
30 :
31 : #include <string.h>
32 : #include <com/sun/star/lang/XServiceName.hpp>
33 : #include <com/sun/star/lang/XServiceInfo.hpp>
34 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 : #include <com/sun/star/sheet/XAddIn.hpp>
36 : #include <com/sun/star/sheet/XCompatibilityNames.hpp>
37 : #include <com/sun/star/sheet/addin/XPricingFunctions.hpp>
38 : #include <cppuhelper/implbase5.hxx>
39 : #include <tools/resid.hxx>
40 : #include <tools/rc.hxx>
41 : #include <tools/resary.hxx>
42 :
43 : #define RETURN_FINITE(d) if( ::rtl::math::isFinite( d ) ) return d; else throw css::lang::IllegalArgumentException()
44 :
45 :
46 :
47 : namespace sca {
48 : namespace pricing {
49 :
50 : class ScaList
51 : {
52 : private:
53 : static const sal_uInt32 nStartSize;
54 : static const sal_uInt32 nIncrSize;
55 :
56 : void** pData; // pointer array
57 : sal_uInt32 nSize; // array size
58 : sal_uInt32 nCount; // next index to be inserted at
59 : sal_uInt32 nCurr; // current pos for iterations
60 :
61 : void _Grow();
62 : inline void Grow();
63 :
64 : public:
65 : ScaList();
66 : virtual ~ScaList();
67 :
68 134 : inline sal_uInt32 Count() const { return nCount; }
69 :
70 1142 : inline const void* GetObject( sal_uInt32 nIndex ) const
71 1142 : { return (nIndex < nCount) ? pData[ nIndex ] : NULL; }
72 :
73 50 : inline void* First() { return nCount ? pData[ nCurr = 0 ] : NULL; }
74 80 : inline void* Next() { return (nCurr + 1 < nCount) ? pData[ ++nCurr ] : NULL; }
75 :
76 : inline void Append( void* pNew );
77 : };
78 :
79 :
80 96 : inline void ScaList::Grow()
81 : {
82 96 : if( nCount >= nSize )
83 0 : _Grow();
84 96 : }
85 :
86 96 : inline void ScaList::Append( void* pNew )
87 : {
88 96 : Grow();
89 96 : pData[ nCount++ ] = pNew;
90 96 : }
91 :
92 :
93 : class ScaStringList : protected ScaList
94 : {
95 : public:
96 48 : inline ScaStringList() : ScaList() {};
97 : virtual ~ScaStringList();
98 :
99 : using ScaList::Count;
100 :
101 : inline const OUString* Get( sal_uInt32 nIndex ) const;
102 :
103 : inline OUString* First();
104 : inline OUString* Next();
105 :
106 : using ScaList::Append;
107 : inline void Append( OUString* pNew );
108 : inline void Append( const OUString& rNew );
109 : };
110 :
111 :
112 4 : inline const OUString* ScaStringList::Get( sal_uInt32 nIndex ) const
113 : {
114 4 : return static_cast< const OUString* >( ScaList::GetObject( nIndex ) );
115 : }
116 :
117 40 : inline OUString* ScaStringList::First()
118 : {
119 40 : return static_cast< OUString* >( ScaList::First() );
120 : }
121 :
122 40 : inline OUString* ScaStringList::Next()
123 : {
124 40 : return static_cast< OUString* >( ScaList::Next() );
125 : }
126 :
127 : inline void ScaStringList::Append( OUString* pNew )
128 : {
129 : ScaList::Append( pNew );
130 : }
131 :
132 48 : inline void ScaStringList::Append( const OUString& rNew )
133 : {
134 48 : ScaList::Append( new OUString( rNew ) );
135 48 : }
136 :
137 :
138 : class ScaResId : public ResId
139 : {
140 : public:
141 : ScaResId( sal_uInt16 nResId, ResMgr& rResMgr );
142 : };
143 :
144 :
145 48 : class ScaResStringLoader : public Resource
146 : {
147 : private:
148 : OUString aStr;
149 :
150 : public:
151 : inline ScaResStringLoader( sal_uInt16 nResId, sal_uInt16 nStrId, ResMgr& rResMgr );
152 :
153 48 : inline const OUString& GetString() const { return aStr; }
154 :
155 : };
156 :
157 :
158 48 : inline ScaResStringLoader::ScaResStringLoader( sal_uInt16 nResId, sal_uInt16 nStrId, ResMgr& rResMgr ) :
159 : Resource( ScaResId( nResId, rResMgr ) ),
160 48 : aStr( ScaResId( nStrId, rResMgr ) )
161 : {
162 48 : FreeResource();
163 48 : }
164 :
165 :
166 48 : class ScaResStringArrLoader : public Resource
167 : {
168 : private:
169 : ResStringArray aStrArray;
170 :
171 : public:
172 : inline ScaResStringArrLoader( sal_uInt16 nResId, sal_uInt16 nArrayId, ResMgr& rResMgr );
173 :
174 48 : inline const ResStringArray& GetStringArray() const { return aStrArray; }
175 : };
176 :
177 48 : inline ScaResStringArrLoader::ScaResStringArrLoader( sal_uInt16 nResId, sal_uInt16 nArrayId, ResMgr& rResMgr ) :
178 : Resource( ScaResId( nResId, rResMgr ) ),
179 48 : aStrArray( ScaResId( nArrayId, rResMgr ) )
180 : {
181 48 : FreeResource();
182 48 : }
183 :
184 :
185 960 : class ScaResPublisher : public Resource
186 : {
187 : public:
188 960 : inline ScaResPublisher( const ScaResId& rResId ) : Resource( rResId ) {}
189 :
190 960 : inline bool IsAvailableRes( const ResId& rResId ) const
191 960 : { return Resource::IsAvailableRes( rResId ); }
192 960 : inline void FreeResource()
193 960 : { Resource::FreeResource(); }
194 : };
195 :
196 :
197 960 : class ScaFuncRes : public Resource
198 : {
199 : public:
200 : ScaFuncRes( ResId& rResId, ResMgr& rResMgr, sal_uInt16 nIndex, OUString& rRet );
201 : };
202 :
203 :
204 : enum ScaCategory
205 : {
206 : ScaCat_AddIn,
207 : ScaCat_DateTime,
208 : ScaCat_Text,
209 : ScaCat_Finance,
210 : ScaCat_Inf,
211 : ScaCat_Math,
212 : ScaCat_Tech
213 : };
214 :
215 : struct ScaFuncDataBase
216 : {
217 : const sal_Char* pIntName; // internal name (get***)
218 : sal_uInt16 nUINameID; // resource ID to UI name
219 : sal_uInt16 nDescrID; // resource ID to description, parameter names and ~ description
220 : sal_uInt16 nCompListID; // resource ID to list of valid names
221 : sal_uInt16 nParamCount; // number of named / described parameters
222 : ScaCategory eCat; // function category
223 : bool bDouble; // name already exist in Calc
224 : bool bWithOpt; // first parameter is internal
225 : };
226 :
227 : class ScaFuncData
228 : {
229 : private:
230 : OUString aIntName; // internal name (get***)
231 : sal_uInt16 nUINameID; // resource ID to UI name
232 : sal_uInt16 nDescrID; // leads also to parameter descriptions!
233 : sal_uInt16 nCompListID; // resource ID to list of valid names
234 : sal_uInt16 nParamCount; // num of parameters
235 : ScaStringList aCompList; // list of all valid names
236 : ScaCategory eCat; // function category
237 : bool bDouble; // name already exist in Calc
238 : bool bWithOpt; // first parameter is internal
239 :
240 : public:
241 : ScaFuncData( const ScaFuncDataBase& rBaseData, ResMgr& rRscMgr );
242 : virtual ~ScaFuncData();
243 :
244 48 : inline sal_uInt16 GetUINameID() const { return nUINameID; }
245 960 : inline sal_uInt16 GetDescrID() const { return nDescrID; }
246 48 : inline ScaCategory GetCategory() const { return eCat; }
247 48 : inline bool IsDouble() const { return bDouble; }
248 : inline bool HasIntParam() const { return bWithOpt; }
249 :
250 : sal_uInt16 GetStrIndex( sal_uInt16 nParam ) const;
251 130 : inline bool Is( const OUString& rCompare ) const
252 130 : { return aIntName == rCompare; }
253 :
254 4 : inline const ScaStringList& GetCompNameList() const { return aCompList; }
255 : };
256 :
257 :
258 : class ScaFuncDataList : private ScaList
259 : {
260 : OUString aLastName;
261 : sal_uInt32 nLast;
262 :
263 : public:
264 : ScaFuncDataList( ResMgr& rResMgr );
265 : virtual ~ScaFuncDataList();
266 :
267 : using ScaList::Count;
268 :
269 : inline const ScaFuncData* Get( sal_uInt32 nIndex ) const;
270 : const ScaFuncData* Get( const OUString& rProgrammaticName ) const;
271 : inline ScaFuncData* First();
272 : inline ScaFuncData* Next();
273 :
274 : using ScaList::Append;
275 48 : inline void Append( ScaFuncData* pNew ) { ScaList::Append( pNew ); }
276 : };
277 :
278 :
279 1138 : inline const ScaFuncData* ScaFuncDataList::Get( sal_uInt32 nIndex ) const
280 : {
281 1138 : return static_cast< const ScaFuncData* >( ScaList::GetObject( nIndex ) );
282 : }
283 :
284 10 : inline ScaFuncData* ScaFuncDataList::First()
285 : {
286 10 : return static_cast< ScaFuncData* >( ScaList::First() );
287 : }
288 :
289 40 : inline ScaFuncData* ScaFuncDataList::Next()
290 : {
291 40 : return static_cast< ScaFuncData* >( ScaList::Next() );
292 : }
293 :
294 : } // namespace pricing
295 : } // namespace sca
296 :
297 :
298 :
299 :
300 : css::uno::Reference< css::uno::XInterface > SAL_CALL PricingFunctionAddIn_CreateInstance(
301 : const css::uno::Reference< css::lang::XMultiServiceFactory >& );
302 :
303 :
304 : // AddIn class for pricing functions
305 :
306 : class ScaPricingAddIn : public ::cppu::WeakImplHelper5<
307 : css::sheet::XAddIn,
308 : css::sheet::XCompatibilityNames,
309 : css::sheet::addin::XPricingFunctions,
310 : css::lang::XServiceName,
311 : css::lang::XServiceInfo >
312 : {
313 : private:
314 : css::lang::Locale aFuncLoc;
315 : css::lang::Locale* pDefLocales;
316 : ResMgr* pResMgr;
317 : sca::pricing::ScaFuncDataList* pFuncDataList;
318 :
319 :
320 : void InitDefLocales();
321 : const css::lang::Locale& GetLocale( sal_uInt32 nIndex );
322 : ResMgr& GetResMgr() throw( css::uno::RuntimeException );
323 : void InitData();
324 :
325 : OUString GetDisplFuncStr( sal_uInt16 nResId ) throw( css::uno::RuntimeException );
326 : OUString GetFuncDescrStr( sal_uInt16 nResId, sal_uInt16 nStrIndex ) throw( css::uno::RuntimeException );
327 :
328 : public:
329 : ScaPricingAddIn();
330 : virtual ~ScaPricingAddIn();
331 :
332 : static OUString getImplementationName_Static();
333 : static css::uno::Sequence< OUString > getSupportedServiceNames_Static();
334 :
335 : // XAddIn
336 : virtual OUString SAL_CALL getProgrammaticFuntionName( const OUString& aDisplayName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
337 : virtual OUString SAL_CALL getDisplayFunctionName( const OUString& aProgrammaticName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
338 : virtual OUString SAL_CALL getFunctionDescription( const OUString& aProgrammaticName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
339 : virtual OUString SAL_CALL getDisplayArgumentName( const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
340 : virtual OUString SAL_CALL getArgumentDescription( const OUString& aProgrammaticName, sal_Int32 nArgument ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
341 : virtual OUString SAL_CALL getProgrammaticCategoryName( const OUString& aProgrammaticName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
342 : virtual OUString SAL_CALL getDisplayCategoryName( const OUString& aProgrammaticName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
343 :
344 : // XCompatibilityNames
345 : virtual css::uno::Sequence< css::sheet::LocalizedName > SAL_CALL getCompatibilityNames( const OUString& aProgrammaticName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
346 :
347 : // XLocalizable
348 : virtual void SAL_CALL setLocale( const css::lang::Locale& eLocale ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
349 : virtual css::lang::Locale SAL_CALL getLocale() throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
350 :
351 : // XServiceName
352 : virtual OUString SAL_CALL getServiceName() throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
353 :
354 : // XServiceInfo
355 : virtual OUString SAL_CALL getImplementationName() throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
356 : virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
357 : virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE;
358 :
359 :
360 : // methods from own interfaces start here
361 :
362 :
363 : virtual double SAL_CALL getOptBarrier( double spot, double vol,
364 : double r, double rf, double T, double strike,
365 : double barrier_low, double barrier_up, double rebate,
366 : const OUString& put_call, const OUString& in_out,
367 : const OUString& continuous, const css::uno::Any& greek ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception ) SAL_OVERRIDE;
368 :
369 : virtual double SAL_CALL getOptTouch( double spot, double vol,
370 : double r, double rf, double T,
371 : double barrier_low, double barrier_up,
372 : const OUString& for_dom, const OUString& in_out,
373 : const OUString& barriercont, const css::uno::Any& greekstr ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception ) SAL_OVERRIDE;
374 :
375 : virtual double SAL_CALL getOptProbHit( double spot, double vol,
376 : double mu, double T,
377 : double barrier_low, double barrier_up ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception ) SAL_OVERRIDE;
378 :
379 : virtual double SAL_CALL getOptProbInMoney( double spot, double vol,
380 : double mu, double T,
381 : double barrier_low, double barrier_up,
382 : const css::uno::Any& strikeval, const css::uno::Any& put_call ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception ) SAL_OVERRIDE;
383 :
384 : };
385 :
386 :
387 : #endif
388 :
389 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|