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