Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / scaddins / source / analysis / analysishelper.hxx
blob5ef162dd17e926f710ad0cfde3ee441e1dc8dafd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SCADDINS_SOURCE_ANALYSIS_ANALYSISHELPER_HXX
20 #define INCLUDED_SCADDINS_SOURCE_ANALYSIS_ANALYSISHELPER_HXX
23 #include <com/sun/star/lang/XServiceName.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/util/Date.hpp>
28 #include <com/sun/star/util/XNumberFormatter2.hpp>
29 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
30 #include <com/sun/star/sheet/XAddIn.hpp>
31 #include <com/sun/star/sheet/addin/XAnalysis.hpp>
33 #include <math.h>
35 #include <tools/resid.hxx>
36 #include <tools/rc.hxx>
38 #include "analysisdefs.hxx"
41 class ResMgr;
43 namespace sca { namespace analysis {
45 class SortedIndividualInt32List;
46 class ScaAnyConverter;
49 #define PI 3.1415926535897932
50 #define EOL ( ( const sal_Char* ) 1 )
51 #define EOE ( ( const sal_Char* ) 2 )
54 inline bool IsLeapYear( sal_uInt16 nYear );
56 #ifdef DISABLE_DYNLOADING
58 // Avoid clash with the functions with same name in
59 // scaddins/source/datefunc/datefunc.cxx. I am not sure if each pair
60 // have identical semantics, but if yes, one copy should be enough,
61 // but what would be a suitable library where such functions could go?
62 // Or can the analysis library depend on the date library or the other
63 // way around?
65 #define DaysInMonth analysishelper_DaysInMonth
66 #define DateToDays analysishelper_DateToDays
67 #define DaysToDate analysishelper_DaysToDate
68 #define GetNullDate analysishelper_GetNullDate
70 #endif
72 sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
73 sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
74 void DaysToDate( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear ) throw( css::lang::IllegalArgumentException );
75 sal_Int32 GetNullDate( const css::uno::Reference< css::beans::XPropertySet >& xOptions ) throw( css::uno::RuntimeException );
76 sal_Int32 GetDiffDate360(
77 sal_uInt16 nDay1, sal_uInt16 nMonth1, sal_uInt16 nYear1, bool bLeapYear1,
78 sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
79 bool bUSAMethod );
80 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod );
81 sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod );
83 sal_Int32 GetDaysInYears( sal_uInt16 nYear1, sal_uInt16 nYear2 );
84 inline sal_Int16 GetDayOfWeek( sal_Int32 nDate );
85 sal_Int32 GetDiffDate( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
86 sal_Int32* pOptDaysIn1stYear = nullptr ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
87 double GetYearDiff( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
88 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
89 sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
90 double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
91 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
92 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
93 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
95 double BinomialCoefficient( double n, double k );
96 double GetGcd( double f1, double f2 );
97 double ConvertToDec( const OUString& rFromNum, sal_uInt16 nBaseFrom, sal_uInt16 nCharLim ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
98 OUString ConvertFromDec(
99 double fNum, double fMin, double fMax, sal_uInt16 nBase,
100 sal_Int32 nPlaces, sal_Int32 nMaxPlaces, bool bUsePlaces ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
101 double Erf( double fX );
102 double Erfc( double fX );
103 bool ParseDouble( const sal_Unicode*& rpDoubleAsString, double& rReturn );
104 OUString GetString( double fNumber, bool bLeadingSign = false, sal_uInt16 nMaxNumOfDigits = 15 );
106 double GetAmordegrc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
107 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
108 double GetAmorlinc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
109 double fRestVal, double fPer, double fRate, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
110 double GetDuration( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup,
111 double fYield, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
112 double GetYieldmat( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
113 double fRate, double fPrice, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
114 double GetOddfprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
115 sal_Int32 nFirstCoup, double fRate, double fYield, double fRedemp,
116 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
117 double getYield_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice,
118 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
119 double getPrice_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield,
120 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
121 double GetOddfyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
122 sal_Int32 nFirstCoup, double fRate, double fPrice, double fRedemp,
123 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
124 double GetOddlprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
125 double fRate, double fYield, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
126 double GetOddlyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
127 double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
128 double GetRmz( double fZins, double fZzr, double fBw, double fZw, sal_Int32 nF );
129 double GetZw( double fZins, double fZzr, double fRmz, double fBw, sal_Int32 nF );
131 double GetCouppcd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
132 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
133 double GetCoupncd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
134 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
135 double GetCoupdaybs( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
136 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
137 double GetCoupdaysnc( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
138 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
140 double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat,
141 sal_Int32 nFreq, sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
142 double GetCoupdays( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
143 sal_Int32 nBase ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
146 enum FDCategory
148 FDCat_AddIn,
149 FDCat_DateTime,
150 FDCat_Finance,
151 FDCat_Inf,
152 FDCat_Math,
153 FDCat_Tech
157 struct FuncDataBase
159 const sal_Char* pIntName;
160 sal_uInt16 nUINameID; // resource ID to UI name
161 sal_uInt16 nDescrID; // resource ID to description, parameter names and ~ description
162 bool bDouble; // name already exist in Calc
163 bool bWithOpt; // first parameter is internal
164 sal_uInt16 nCompListID; // resource ID to list of valid names
165 sal_uInt16 nNumOfParams; // number of named / described parameters
166 FDCategory eCat; // function category
167 const char* pSuffix; // if bDouble, append a suffix other than "_ADD" for UI
171 class FuncData
173 private:
174 OUString aIntName;
175 sal_uInt16 nUINameID;
176 sal_uInt16 nDescrID; // leads also to parameter descriptions!
177 bool bDouble; // flag for names that already exist in Calc
178 bool bWithOpt; // has internal parameter on first position
180 sal_uInt16 nParam; // num of parameters
181 sal_uInt16 nCompID;
182 std::vector<OUString> aCompList; // list of all valid names
183 FDCategory eCat; // function category
184 OUString aSuffix; // if bDouble and not empty, append a suffix other than "_ADD" for UI
186 public:
187 FuncData( const FuncDataBase& rBaseData, ResMgr& );
188 virtual ~FuncData();
190 inline sal_uInt16 GetUINameID() const;
191 inline sal_uInt16 GetDescrID() const;
192 inline bool IsDouble() const;
193 inline const OUString& GetSuffix() const;
195 sal_uInt16 GetStrIndex( sal_uInt16 nParamNum ) const;
196 inline bool Is( const OUString& rCompareTo ) const;
198 inline const std::vector<OUString> &
199 GetCompNameList() const;
201 inline FDCategory GetCategory() const;
204 typedef std::vector< FuncData > FuncDataList;
206 void InitFuncDataList ( FuncDataList& rList, ResMgr& rResMgr );
208 // Predicate for use with std::find_if
209 struct FindFuncData
211 const OUString& m_rId;
212 explicit FindFuncData( const OUString& rId ) : m_rId(rId) {}
213 bool operator() ( FuncData& rCandidate ) const { return rCandidate.Is(m_rId); }
216 class AnalysisResId : public ResId
218 public:
219 AnalysisResId( sal_uInt16 nId, ResMgr& rResMgr );
223 class AnalysisRscStrLoader : public Resource
225 private:
226 OUString aStr;
227 public:
228 AnalysisRscStrLoader( sal_uInt16 nRsc, sal_uInt16 nStrId, ResMgr& rResMgr ) :
229 Resource( AnalysisResId( nRsc, rResMgr ) ),
230 aStr( AnalysisResId( nStrId, rResMgr ) )
232 FreeResource();
235 const OUString& GetString() const { return aStr; }
240 /// sorted list with unique sal_Int32 values
241 class SortedIndividualInt32List
243 private:
244 std::vector<sal_Int32> maVector;
245 protected:
246 void Insert( sal_Int32 nDay );
247 void Insert( sal_Int32 nDay, sal_Int32 nNullDate, bool bInsertOnWeekend );
248 void Insert( double fDay, sal_Int32 nNullDate, bool bInsertOnWeekend )
249 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
251 /** @param rAnyConv must be an initialized ScaAnyConmverter
252 @param bInsertOnWeekend insertion mode: false = holidays on weekend are omitted */
253 void InsertHolidayList(
254 const ScaAnyConverter& rAnyConv,
255 const css::uno::Any& rHolAny,
256 sal_Int32 nNullDate,
257 bool bInsertOnWeekend ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
259 public:
260 SortedIndividualInt32List();
261 virtual ~SortedIndividualInt32List();
263 inline sal_uInt32 Count() const
264 { return maVector.size(); }
266 /// @return element on position nIndex or 0 on invalid index
267 inline sal_Int32 Get( sal_uInt32 n ) const
268 { return maVector[n]; }
270 /// @return true if nVal (internal date representation) is contained
271 bool Find( sal_Int32 nVal ) const;
273 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
274 holidays on weekend are omitted */
275 void InsertHolidayList(
276 ScaAnyConverter& rAnyConv,
277 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
278 const css::uno::Any& rHolAny,
279 sal_Int32 nNullDate) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
283 class ScaDoubleList
285 private:
286 std::vector<double> maVector;
287 protected:
288 inline void ListAppend( double fValue ) { maVector.push_back(fValue); }
290 inline void Append( double fValue ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
291 { if( CheckInsert( fValue ) ) ListAppend( fValue ); }
293 /** @param rAnyConv must be an initialized ScaAnyConmverter
294 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
295 void Append(
296 const ScaAnyConverter& rAnyConv,
297 const css::uno::Any& rAny,
298 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
300 /** @param rAnyConv must be an initialized ScaAnyConmverter
301 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
302 void Append(
303 const ScaAnyConverter& rAnyConv,
304 const css::uno::Sequence< css::uno::Any >& rAnySeq,
305 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
307 /** @param rAnyConv must be an initialized ScaAnyConmverter
308 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
309 void Append(
310 const ScaAnyConverter& rAnyConv,
311 const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rAnySeq,
312 bool bIgnoreEmpty ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
314 public:
315 virtual ~ScaDoubleList() {}
317 inline sal_uInt32 Count() const
318 { return maVector.size(); }
319 inline double Get( sal_uInt32 n ) const
320 { return maVector[n]; }
322 void Append( const css::uno::Sequence< css::uno::Sequence< double > >& rValueArr )
323 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
324 void Append( const css::uno::Sequence< css::uno::Sequence< sal_Int32 > >& rValueArr )
325 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
327 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
328 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted */
329 void Append(
330 ScaAnyConverter& rAnyConv,
331 const css::uno::Reference< css::beans::XPropertySet >& xOpt,
332 const css::uno::Sequence< css::uno::Any >& rAnySeq ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
334 virtual bool CheckInsert( double fValue ) const
335 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
339 /// stores double values >0.0, throws exception for double values <0.0, does nothing for 0.0
340 class ScaDoubleListGT0 : public ScaDoubleList
342 public:
343 virtual bool CheckInsert( double fValue ) const
344 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) override;
348 /// stores double values >=0.0, throws exception for double values <0.0
349 class ScaDoubleListGE0 : public ScaDoubleList
351 public:
352 virtual bool CheckInsert( double fValue ) const
353 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) override;
357 class Complex
359 double r;
360 double i;
361 sal_Unicode c;
363 public:
364 inline Complex( double fReal, double fImag = 0.0, sal_Unicode cC = '\0' );
365 explicit Complex( const OUString& rComplexAsString ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
367 inline static bool IsImagUnit( sal_Unicode c );
368 static bool ParseString( const OUString& rComplexAsString, Complex& rReturn );
369 OUString GetString() const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
371 inline double Real() const;
372 inline double Imag() const;
374 double Arg() const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
375 inline double Abs() const;
377 // following functions change the complex number itself to avoid unnecessary copy actions!
378 void Power( double fPower ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
379 void Sqrt();
380 void Sin() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
381 void Cos() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
382 void Div( const Complex& rDivisor ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
383 void Exp();
384 inline void Conjugate();
385 void Ln() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
386 void Log10() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
387 void Log2() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
388 inline void Mult( double fFact );
389 inline void Mult( const Complex& rMult );
390 inline void Sub( const Complex& rMult );
391 inline void Add( const Complex& rAdd );
392 void Tan() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
393 void Sec() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
394 void Csc() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
395 void Cot() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
396 void Sinh() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
397 void Cosh() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
398 void Sech() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
399 void Csch() throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
404 enum ComplListAppendHandl
406 AH_EmptyAsErr,
407 AH_EmpyAs0,
408 AH_IgnoreEmpty
412 class ComplexList
414 private:
415 std::vector<Complex*> maVector;
416 public:
417 virtual ~ComplexList();
419 inline const Complex* Get( sal_uInt32 nIndex ) const;
421 inline bool empty() const
422 { return maVector.empty(); }
423 inline sal_uInt32 Count() const
424 { return maVector.size(); }
426 inline void Append( Complex* pNew );
427 void Append( const css::uno::Sequence< css::uno::Sequence< OUString > >& rComplexNumList, ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
428 void Append( const css::uno::Sequence< css::uno::Any >& aMultPars,ComplListAppendHandl eAH = AH_EmpyAs0 ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
432 enum ConvertDataClass
434 CDC_Mass, CDC_Length, CDC_Time, CDC_Pressure, CDC_Force, CDC_Energy, CDC_Power, CDC_Magnetism,
435 CDC_Temperature, CDC_Volume, CDC_Area, CDC_Speed, CDC_Information
439 #define INV_MATCHLEV 1764 // guess, what this is... :-)
442 class ConvertDataList;
444 class ConvertData
446 protected:
447 friend class ConvertDataList;
448 double fConst;
449 OUString aName;
450 ConvertDataClass eClass;
451 bool bPrefixSupport;
452 public:
453 ConvertData(
454 const sal_Char pUnitName[],
455 double fConvertConstant,
456 ConvertDataClass eClass,
457 bool bPrefSupport = false );
459 virtual ~ConvertData();
461 sal_Int16 GetMatchingLevel( const OUString& rRef ) const;
462 // 0.0 = no equality
463 // 1.0 = matches exact
464 // rest = matches without an assumed prefix of one character
465 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
467 virtual double Convert( double fVal, const ConvertData& rTo,
468 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
469 // converts fVal from this unit to rFrom unit
470 // throws exception if not from same class
471 // this implementation is for proportional cases only
472 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
473 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
475 inline ConvertDataClass Class() const;
476 inline bool IsPrefixSupport() const;
479 class ConvertDataLinear : public ConvertData
481 protected:
482 double fOffs;
483 public:
484 inline ConvertDataLinear(
485 const sal_Char pUnitName[],
486 double fConvertConstant,
487 double fConvertOffset,
488 ConvertDataClass eClass,
489 bool bPrefSupport = false );
491 virtual ~ConvertDataLinear();
493 virtual double Convert( double fVal, const ConvertData& rTo,
494 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const throw( css::uno::RuntimeException, css::lang::IllegalArgumentException ) override;
495 // for cases where f(x) = a + bx applies (e.g. Temperatures)
497 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const override;
498 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const override;
502 class ConvertDataList
504 private:
505 std::vector<ConvertData*> maVector;
506 public:
507 ConvertDataList();
508 virtual ~ConvertDataList();
510 double Convert( double fVal, const OUString& rFrom, const OUString& rTo ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
514 inline bool IsLeapYear( sal_uInt16 n )
516 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
520 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod )
522 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
526 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
527 { // monday = 0, ..., sunday = 6
528 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
532 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode ) throw( css::uno::RuntimeException, css::lang::IllegalArgumentException )
534 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
538 inline sal_uInt16 FuncData::GetUINameID() const
540 return nUINameID;
544 inline sal_uInt16 FuncData::GetDescrID() const
546 return nDescrID;
550 inline bool FuncData::IsDouble() const
552 return bDouble;
556 inline const OUString& FuncData::GetSuffix() const
558 return aSuffix;
562 inline bool FuncData::Is( const OUString& r ) const
564 return aIntName == r;
568 inline const std::vector<OUString> & FuncData::GetCompNameList() const
570 return aCompList;
574 inline FDCategory FuncData::GetCategory() const
576 return eCat;
580 inline Complex::Complex( double fReal, double fImag, sal_Unicode cC ) :
581 r( fReal ), i( fImag ), c( cC )
586 inline double Complex::Real() const
588 return r;
592 inline double Complex::Imag() const
594 return i;
598 inline double Complex::Abs() const
600 return sqrt( r * r + i * i );
604 void Complex::Conjugate()
606 i = -i;
610 inline void Complex::Mult( double f )
612 i *= f;
613 r *= f;
617 inline void Complex::Mult( const Complex& rM )
619 double r_ = r;
620 double i_ = i;
622 r = r_ * rM.r - i_ * rM.i;
623 i = r_ * rM.i + i_ * rM.r;
625 if( !c ) c = rM.c;
629 inline void Complex::Sub( const Complex& rC )
631 r -= rC.r;
632 i -= rC.i;
633 if( !c ) c = rC.c;
637 inline void Complex::Add( const Complex& rAdd )
639 r += rAdd.r;
640 i += rAdd.i;
641 if( !c ) c = rAdd.c;
645 inline const Complex* ComplexList::Get( sal_uInt32 n ) const
647 return maVector[n];
651 inline void ComplexList::Append( Complex* p )
653 maVector.push_back(p);
657 inline ConvertDataClass ConvertData::Class() const
659 return eClass;
662 inline bool ConvertData::IsPrefixSupport() const
664 return bPrefixSupport;
667 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e,
668 bool bPrefSupport ) :
669 ConvertData( p, fC, e, bPrefSupport ),
670 fOffs( fO )
675 /// Helper class for date calculation for various financial functions
676 class ScaDate
678 private:
679 sal_uInt16 nOrigDay; /// is the day of the original date.
680 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
681 sal_uInt16 nMonth; /// is the current month (one-based).
682 sal_uInt16 nYear; /// is the current year.
683 bool bLastDayMode : 1; /// if true, recalculate nDay after every calculation.
684 bool bLastDay : 1; /// is true, if original date was the last day in month.
685 bool b30Days : 1; /// is true, if every month has 30 days in calculations.
686 bool bUSMode : 1; /// is true, if the US method of 30-day-calculations is used.
688 /// Calculates nDay from nOrigDay and current date.
689 void setDay();
691 /// @return count of days in current month
692 inline sal_uInt16 getDaysInMonth() const;
693 /// @return count of days in given month
694 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
696 /// @ return count of days in the given month range
697 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
698 /// @ return count of days in the given year range
699 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
701 /// Adds/subtracts the given count of years, does not adjust day.
702 void doAddYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
704 public:
705 ScaDate();
706 /** @param nBase
707 date handling mode (days in month / days in year):
708 0 = 30 days / 360 days (US NASD)
709 1 = exact / exact
710 2 = exact / 360
711 3 = exact / 365
712 4 = 30 days / 360 days (Europe)
713 5 = exact / exact (no last day adjustment) */
714 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
715 ScaDate( const ScaDate& rCopy );
716 ScaDate& operator=( const ScaDate& rCopy );
718 /// @return the current month.
719 inline sal_uInt16 getMonth() const { return nMonth; };
720 /// @return the current year.
721 inline sal_uInt16 getYear() const { return nYear; };
723 /// adds/subtracts the given count of months, adjusts day
724 void addMonths( sal_Int32 nMonthCount ) throw( css::lang::IllegalArgumentException );
726 /// sets the given year, adjusts day
727 inline void setYear( sal_uInt16 nNewYear );
728 /// adds/subtracts the given count of years, adjusts day
729 inline void addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException );
731 /// @return the internal number of the current date
732 sal_Int32 getDate( sal_Int32 nNullDate ) const;
733 /// @return the number of days between the two dates
734 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo ) throw( css::lang::IllegalArgumentException );
736 bool operator<( const ScaDate& rCmp ) const;
737 inline bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
738 inline bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
739 inline bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
742 inline sal_uInt16 ScaDate::getDaysInMonth() const
744 return getDaysInMonth( nMonth );
747 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
749 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
752 inline void ScaDate::setYear( sal_uInt16 nNewYear )
754 nYear = nNewYear;
755 setDay();
758 inline void ScaDate::addYears( sal_Int32 nYearCount ) throw( css::lang::IllegalArgumentException )
760 doAddYears( nYearCount );
761 setDay();
765 /// Helper class for Any->double conversion, using current language settings
766 class ScaAnyConverter
768 private:
769 css::uno::Reference< css::util::XNumberFormatter2 > xFormatter;
770 sal_Int32 nDefaultFormat;
771 bool bHasValidFormat;
773 /** Converts a string to double using the number formatter. If the formatter is not
774 valid, ::rtl::math::stringToDouble() with english separators will be used.
775 @throws css::lang::IllegalArgumentException
776 on strings not representing any double value.
777 @return the converted double value. */
778 double convertToDouble(
779 const OUString& rString ) const
780 throw( css::lang::IllegalArgumentException );
782 public:
783 explicit ScaAnyConverter(
784 const css::uno::Reference< css::uno::XComponentContext >& xContext );
785 ~ScaAnyConverter();
787 /// Initializing with current language settings
788 void init(
789 const css::uno::Reference< css::beans::XPropertySet >& xPropSet )
790 throw( css::uno::RuntimeException );
792 /** Converts an Any to double (without initialization).
793 The Any can be empty or contain a double or string.
794 @throws css::lang::IllegalArgumentException
795 on other Any types or on invalid strings.
796 @return true if the Any contains a double or a non-empty valid string,
797 false if the Any is empty or the string is empty */
798 bool getDouble(
799 double& rfResult,
800 const css::uno::Any& rAny ) const
801 throw( css::lang::IllegalArgumentException );
803 /** Converts an Any to double (with initialization).
804 The Any can be empty or contain a double or string.
805 @throws css::lang::IllegalArgumentException
806 on other Any types or on invalid strings.
807 @return true if the Any contains a double or a non-empty valid string,
808 false if the Any is empty or the string is empty */
809 bool getDouble(
810 double& rfResult,
811 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
812 const css::uno::Any& rAny )
813 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
815 /** Converts an Any to double (with initialization).
816 The Any can be empty or contain a double or string.
817 @throws css::lang::IllegalArgumentException
818 on other Any types or on invalid strings.
819 @return the value of the double or string or fDefault if the Any or string is empty */
820 double getDouble(
821 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
822 const css::uno::Any& rAny,
823 double fDefault )
824 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
826 /** Converts an Any to sal_Int32 (with initialization).
827 The Any can be empty or contain a double or string.
828 @throws css::lang::IllegalArgumentException
829 on other Any types or on invalid values or strings.
830 @return true if the Any contains a double or a non-empty valid string,
831 false if the Any is empty or the string is empty */
832 bool getInt32(
833 sal_Int32& rnResult,
834 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
835 const css::uno::Any& rAny )
836 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
838 /** Converts an Any to sal_Int32 (with initialization).
839 The Any can be empty or contain a double or string.
840 @throws css::lang::IllegalArgumentException
841 on other Any types or on invalid values or strings.
842 @return the truncated value of the double or string or nDefault if the Any or string is empty */
843 sal_Int32 getInt32(
844 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
845 const css::uno::Any& rAny,
846 sal_Int32 nDefault )
847 throw( css::uno::RuntimeException, css::lang::IllegalArgumentException );
852 #endif
854 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */