Bump version to 6.0-36
[LibreOffice.git] / scaddins / source / analysis / analysishelper.hxx
blobfa68eee35f0d9f17462772d36c3406028c8c430c
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 "analysisdefs.hxx"
37 #include <vector>
40 namespace sca { namespace analysis {
42 class SortedIndividualInt32List;
43 class ScaAnyConverter;
46 #define PI 3.1415926535897932
47 #define EOL ( ( const sal_Char* ) 1 )
48 #define EOE ( ( const sal_Char* ) 2 )
51 inline bool IsLeapYear( sal_uInt16 nYear );
53 #ifdef DISABLE_DYNLOADING
55 // Avoid clash with the functions with same name in
56 // scaddins/source/datefunc/datefunc.cxx. I am not sure if each pair
57 // have identical semantics, but if yes, one copy should be enough,
58 // but what would be a suitable library where such functions could go?
59 // Or can the analysis library depend on the date library or the other
60 // way around?
62 #define DaysInMonth analysishelper_DaysInMonth
63 #define DateToDays analysishelper_DateToDays
64 #define DaysToDate analysishelper_DaysToDate
65 #define GetNullDate analysishelper_GetNullDate
67 #endif
69 sal_uInt16 DaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear );
70 sal_Int32 DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear );
71 /// @throws css::lang::IllegalArgumentException
72 void DaysToDate( sal_Int32 nDays, sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear );
73 /// @throws css::uno::RuntimeException
74 sal_Int32 GetNullDate( const css::uno::Reference< css::beans::XPropertySet >& xOptions );
75 sal_Int32 GetDiffDate360(
76 sal_uInt16 nDay1, sal_uInt16 nMonth1, sal_uInt16 nYear1, bool bLeapYear1,
77 sal_uInt16 nDay2, sal_uInt16 nMonth2, sal_uInt16 nYear2,
78 bool bUSAMethod );
79 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod );
80 sal_Int32 GetDiffDate360( sal_Int32 nNullDate, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod );
82 sal_Int32 GetDaysInYears( sal_uInt16 nYear1, sal_uInt16 nYear2 );
83 inline sal_Int16 GetDayOfWeek( sal_Int32 nDate );
84 /// @throws css::uno::RuntimeException
85 /// @throws css::lang::IllegalArgumentException
86 sal_Int32 GetDiffDate( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode,
87 sal_Int32* pOptDaysIn1stYear );
88 /// @throws css::uno::RuntimeException
89 /// @throws css::lang::IllegalArgumentException
90 double GetYearDiff( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode );
91 /// @throws css::uno::RuntimeException
92 /// @throws css::lang::IllegalArgumentException
93 sal_Int32 GetDaysInYear( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nMode );
94 /// @throws css::uno::RuntimeException
95 /// @throws css::lang::IllegalArgumentException
96 double GetYearFrac( sal_Int32 nNullDate, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode );
97 /// @throws css::uno::RuntimeException
98 /// @throws css::lang::IllegalArgumentException
99 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode );
101 double BinomialCoefficient( double n, double k );
102 double GetGcd( double f1, double f2 );
103 /// @throws css::uno::RuntimeException
104 /// @throws css::lang::IllegalArgumentException
105 double ConvertToDec( const OUString& rFromNum, sal_uInt16 nBaseFrom, sal_uInt16 nCharLim );
106 /// @throws css::uno::RuntimeException
107 /// @throws css::lang::IllegalArgumentException
108 OUString ConvertFromDec(
109 double fNum, double fMin, double fMax, sal_uInt16 nBase,
110 sal_Int32 nPlaces, sal_Int32 nMaxPlaces, bool bUsePlaces );
111 double Erf( double fX );
112 double Erfc( double fX );
113 bool ParseDouble( const sal_Unicode*& rpDoubleAsString, double& rReturn );
114 OUString GetString( double fNumber, bool bLeadingSign, sal_uInt16 nMaxNumOfDigits = 15 );
116 /// @throws css::uno::RuntimeException
117 /// @throws css::lang::IllegalArgumentException
118 double GetAmordegrc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
119 double fRestVal, double fPer, double fRate, sal_Int32 nBase );
120 /// @throws css::uno::RuntimeException
121 /// @throws css::lang::IllegalArgumentException
122 double GetAmorlinc( sal_Int32 nNullDate, double fCost, sal_Int32 nDate, sal_Int32 nFirstPer,
123 double fRestVal, double fPer, double fRate, sal_Int32 nBase );
124 /// @throws css::uno::RuntimeException
125 /// @throws css::lang::IllegalArgumentException
126 double GetDuration( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup,
127 double fYield, sal_Int32 nFreq, sal_Int32 nBase );
128 /// @throws css::uno::RuntimeException
129 /// @throws css::lang::IllegalArgumentException
130 double GetYieldmat( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
131 double fRate, double fPrice, sal_Int32 nBase );
132 /// @throws css::uno::RuntimeException
133 /// @throws css::lang::IllegalArgumentException
134 double GetOddfprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
135 sal_Int32 nFirstCoup, double fRate, double fYield, double fRedemp,
136 sal_Int32 nFreq, sal_Int32 nBase );
137 /// @throws css::uno::RuntimeException
138 /// @throws css::lang::IllegalArgumentException
139 double getYield_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fCoup, double fPrice,
140 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase );
141 /// @throws css::uno::RuntimeException
142 /// @throws css::lang::IllegalArgumentException
143 double getPrice_( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, double fRate, double fYield,
144 double fRedemp, sal_Int32 nFreq, sal_Int32 nBase );
145 /// @throws css::uno::RuntimeException
146 /// @throws css::lang::IllegalArgumentException
147 double GetOddfyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nIssue,
148 sal_Int32 nFirstCoup, double fRate, double fPrice, double fRedemp,
149 sal_Int32 nFreq, sal_Int32 nBase );
150 /// @throws css::uno::RuntimeException
151 /// @throws css::lang::IllegalArgumentException
152 double GetOddlprice( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
153 double fRate, double fYield, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase );
154 /// @throws css::uno::RuntimeException
155 /// @throws css::lang::IllegalArgumentException
156 double GetOddlyield( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nLastInterest,
157 double fRate, double fPrice, double fRedemp, sal_Int32 nFreq, sal_Int32 nBase );
158 double GetPmt( double fRate, double fNper, double fPv, double fFv, sal_Int32 nPayType );
159 double GetFv( double fRate, double fNper, double fPmt, double fPv, sal_Int32 nPayType );
161 /// @throws css::uno::RuntimeException
162 /// @throws css::lang::IllegalArgumentException
163 double GetCouppcd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
164 sal_Int32 nBase );
165 /// @throws css::uno::RuntimeException
166 /// @throws css::lang::IllegalArgumentException
167 double GetCoupncd( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
168 sal_Int32 nBase );
169 /// @throws css::uno::RuntimeException
170 /// @throws css::lang::IllegalArgumentException
171 double GetCoupdaybs( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
172 sal_Int32 nBase );
173 /// @throws css::uno::RuntimeException
174 /// @throws css::lang::IllegalArgumentException
175 double GetCoupdaysnc( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
176 sal_Int32 nBase );
178 /// @throws css::uno::RuntimeException
179 /// @throws css::lang::IllegalArgumentException
180 double GetCoupnum( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat,
181 sal_Int32 nFreq, sal_Int32 nBase );
182 /// @throws css::uno::RuntimeException
183 /// @throws css::lang::IllegalArgumentException
184 double GetCoupdays( sal_Int32 nNullDate, sal_Int32 nSettle, sal_Int32 nMat, sal_Int32 nFreq,
185 sal_Int32 nBase );
188 enum class FDCategory
190 DateTime,
191 Finance,
192 Inf,
193 Math,
194 Tech
198 struct FuncDataBase
200 const sal_Char* pIntName;
201 const char* pUINameID; // resource ID to UI name
202 const char** pDescrID; // resource ID to description, parameter names and ~ description
203 bool bDouble; // name already exist in Calc
204 bool bWithOpt; // first parameter is internal
205 const char** pCompListID; // list of valid names
206 sal_uInt16 nNumOfParams; // number of named / described parameters
207 FDCategory eCat; // function category
208 const char* pSuffix; // if bDouble, append a suffix other than "_ADD" for UI
212 class FuncData final
214 private:
215 OUString aIntName;
216 const char* pUINameID;
217 const char** pDescrID; // leads also to parameter descriptions!
218 bool bDouble; // flag for names that already exist in Calc
219 bool bWithOpt; // has internal parameter on first position
221 sal_uInt16 nParam; // num of parameters
222 std::vector<OUString> aCompList; // list of all valid names
223 FDCategory eCat; // function category
224 OUString aSuffix; // if bDouble and not empty, append a suffix other than "_ADD" for UI
226 public:
227 FuncData(const FuncDataBase& rBaseData);
228 ~FuncData();
230 inline const char* GetUINameID() const;
231 inline const char** GetDescrID() const;
232 inline bool IsDouble() const;
233 inline const OUString& GetSuffix() const;
235 sal_uInt16 GetStrIndex( sal_uInt16 nParamNum ) const;
236 inline bool Is( const OUString& rCompareTo ) const;
238 inline const std::vector<OUString> &
239 GetCompNameList() const;
241 inline FDCategory GetCategory() const;
244 typedef std::vector< FuncData > FuncDataList;
246 void InitFuncDataList(FuncDataList& rList);
248 // Predicate for use with std::find_if
249 struct FindFuncData
251 const OUString& m_rId;
252 explicit FindFuncData( const OUString& rId ) : m_rId(rId) {}
253 bool operator() ( FuncData const & rCandidate ) const { return rCandidate.Is(m_rId); }
256 /// sorted list with unique sal_Int32 values
257 class SortedIndividualInt32List final
259 private:
260 std::vector<sal_Int32> maVector;
262 void Insert( sal_Int32 nDay );
263 void Insert( sal_Int32 nDay, sal_Int32 nNullDate, bool bInsertOnWeekend );
264 /// @throws css::uno::RuntimeException
265 /// @throws css::lang::IllegalArgumentException
266 void Insert( double fDay, sal_Int32 nNullDate, bool bInsertOnWeekend );
268 /** @param rAnyConv must be an initialized ScaAnyConmverter
269 @param bInsertOnWeekend insertion mode: false = holidays on weekend are omitted
270 @throws css::uno::RuntimeException
271 @throws css::lang::IllegalArgumentException
273 void InsertHolidayList(
274 const ScaAnyConverter& rAnyConv,
275 const css::uno::Any& rHolAny,
276 sal_Int32 nNullDate,
277 bool bInsertOnWeekend );
279 public:
280 SortedIndividualInt32List();
281 ~SortedIndividualInt32List();
283 sal_uInt32 Count() const
284 { return maVector.size(); }
286 /// @return element on position nIndex or 0 on invalid index
287 sal_Int32 Get( sal_uInt32 n ) const
288 { return maVector[n]; }
290 /// @return true if nVal (internal date representation) is contained
291 bool Find( sal_Int32 nVal ) const;
293 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
294 holidays on weekend are omitted
295 @throws css::uno::RuntimeException
296 @throws css::lang::IllegalArgumentException
298 void InsertHolidayList(
299 ScaAnyConverter& rAnyConv,
300 const css::uno::Reference< css::beans::XPropertySet >& xOptions,
301 const css::uno::Any& rHolAny,
302 sal_Int32 nNullDate);
306 class ScaDoubleList
308 private:
309 std::vector<double> maVector;
310 protected:
311 void ListAppend( double fValue ) { maVector.push_back(fValue); }
313 /// @throws css::uno::RuntimeException
314 /// @throws css::lang::IllegalArgumentException
315 void Append( double fValue )
316 { if( CheckInsert( fValue ) ) ListAppend( fValue ); }
318 /** @param rAnyConv must be an initialized ScaAnyConmverter
319 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted
320 @throws css::uno::RuntimeException
321 @throws css::lang::IllegalArgumentException
323 void Append(
324 const ScaAnyConverter& rAnyConv,
325 const css::uno::Any& rAny,
326 bool bIgnoreEmpty );
328 /** @param rAnyConv must be an initialized ScaAnyConmverter
329 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted
330 @throws css::uno::RuntimeException
331 @throws css::lang::IllegalArgumentException
333 void Append(
334 const ScaAnyConverter& rAnyConv,
335 const css::uno::Sequence< css::uno::Any >& rAnySeq,
336 bool bIgnoreEmpty );
338 /** @param rAnyConv must be an initialized ScaAnyConmverter
339 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted
340 @throws css::uno::RuntimeException
341 @throws css::lang::IllegalArgumentException
343 void Append(
344 const ScaAnyConverter& rAnyConv,
345 const css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rAnySeq,
346 bool bIgnoreEmpty );
348 public:
349 virtual ~ScaDoubleList() {}
351 sal_uInt32 Count() const
352 { return maVector.size(); }
353 double Get( sal_uInt32 n ) const
354 { return maVector[n]; }
356 /// @throws css::uno::RuntimeException
357 /// @throws css::lang::IllegalArgumentException
358 void Append( const css::uno::Sequence< css::uno::Sequence< double > >& rValueArr );
359 /// @throws css::uno::RuntimeException
360 /// @throws css::lang::IllegalArgumentException
361 void Append( const css::uno::Sequence< css::uno::Sequence< sal_Int32 > >& rValueArr );
363 /** @param rAnyConv is an initialized or uninitialized ScaAnyConverter
364 @param bIgnoreEmpty handling of empty Any's/strings: false = inserted as 0.0; true = omitted
365 @throws css::uno::RuntimeException
366 @throws css::lang::IllegalArgumentException
368 void Append(
369 ScaAnyConverter& rAnyConv,
370 const css::uno::Reference< css::beans::XPropertySet >& xOpt,
371 const css::uno::Sequence< css::uno::Any >& rAnySeq );
373 /// @throws css::uno::RuntimeException
374 /// @throws css::lang::IllegalArgumentException
375 virtual bool CheckInsert( double fValue ) const;
379 /// stores double values >0.0, throws exception for double values <0.0, does nothing for 0.0
380 class ScaDoubleListGT0 : public ScaDoubleList
382 public:
383 virtual bool CheckInsert( double fValue ) const override;
387 /// stores double values >=0.0, throws exception for double values <0.0
388 class ScaDoubleListGE0 : public ScaDoubleList
390 public:
391 virtual bool CheckInsert( double fValue ) const override;
395 class Complex
397 double r;
398 double i;
399 sal_Unicode c;
401 public:
402 inline Complex( double fReal, double fImag = 0.0, sal_Unicode cC = '\0' );
403 /// @throws css::uno::RuntimeException
404 /// @throws css::lang::IllegalArgumentException
405 explicit Complex( const OUString& rComplexAsString );
407 inline static bool IsImagUnit( sal_Unicode c );
408 static bool ParseString( const OUString& rComplexAsString, Complex& rReturn );
409 /// @throws css::uno::RuntimeException
410 /// @throws css::lang::IllegalArgumentException
411 OUString GetString() const;
413 inline double Real() const;
414 inline double Imag() const;
416 /// @throws css::uno::RuntimeException
417 /// @throws css::lang::IllegalArgumentException
418 double Arg() const;
419 inline double Abs() const;
421 // following functions change the complex number itself to avoid unnecessary copy actions!
422 /// @throws css::uno::RuntimeException
423 /// @throws css::lang::IllegalArgumentException
424 void Power( double fPower );
425 void Sqrt();
426 /// @throws css::uno::RuntimeException
427 /// @throws css::lang::IllegalArgumentException
428 void Sin();
429 /// @throws css::uno::RuntimeException
430 /// @throws css::lang::IllegalArgumentException
431 void Cos();
432 /// @throws css::uno::RuntimeException
433 /// @throws css::lang::IllegalArgumentException
434 void Div( const Complex& rDivisor );
435 void Exp();
436 inline void Conjugate();
437 /// @throws css::uno::RuntimeException
438 /// @throws css::lang::IllegalArgumentException
439 void Ln();
440 /// @throws css::uno::RuntimeException
441 /// @throws css::lang::IllegalArgumentException
442 void Log10();
443 /// @throws css::uno::RuntimeException
444 /// @throws css::lang::IllegalArgumentException
445 void Log2();
446 inline void Mult( double fFact );
447 inline void Mult( const Complex& rMult );
448 inline void Sub( const Complex& rMult );
449 inline void Add( const Complex& rAdd );
450 /// @throws css::uno::RuntimeException
451 /// @throws css::lang::IllegalArgumentException
452 void Tan();
453 /// @throws css::uno::RuntimeException
454 /// @throws css::lang::IllegalArgumentException
455 void Sec();
456 /// @throws css::uno::RuntimeException
457 /// @throws css::lang::IllegalArgumentException
458 void Csc();
459 /// @throws css::uno::RuntimeException
460 /// @throws css::lang::IllegalArgumentException
461 void Cot();
462 /// @throws css::uno::RuntimeException
463 /// @throws css::lang::IllegalArgumentException
464 void Sinh();
465 /// @throws css::uno::RuntimeException
466 /// @throws css::lang::IllegalArgumentException
467 void Cosh();
468 /// @throws css::uno::RuntimeException
469 /// @throws css::lang::IllegalArgumentException
470 void Sech();
471 /// @throws css::uno::RuntimeException
472 /// @throws css::lang::IllegalArgumentException
473 void Csch();
478 enum ComplListAppendHandl
480 AH_EmptyAsErr,
481 AH_EmpyAs0,
482 AH_IgnoreEmpty
486 class ComplexList final
488 private:
489 std::vector<Complex> maVector;
490 public:
491 ~ComplexList();
493 inline const Complex& Get( sal_uInt32 nIndex ) const;
495 bool empty() const
496 { return maVector.empty(); }
497 sal_uInt32 Count() const
498 { return maVector.size(); }
500 inline void Append( Complex&& pNew );
501 /// @throws css::uno::RuntimeException
502 /// @throws css::lang::IllegalArgumentException
503 void Append( const css::uno::Sequence< css::uno::Sequence< OUString > >& rComplexNumList, ComplListAppendHandl eAH );
504 /// @throws css::uno::RuntimeException
505 /// @throws css::lang::IllegalArgumentException
506 void Append( const css::uno::Sequence< css::uno::Any >& aMultPars,ComplListAppendHandl eAH );
510 enum ConvertDataClass
512 CDC_Mass, CDC_Length, CDC_Time, CDC_Pressure, CDC_Force, CDC_Energy, CDC_Power, CDC_Magnetism,
513 CDC_Temperature, CDC_Volume, CDC_Area, CDC_Speed, CDC_Information
517 #define INV_MATCHLEV 1764 // guess, what this is... :-)
520 class ConvertDataList;
522 class ConvertData
524 protected:
525 friend class ConvertDataList;
526 double fConst;
527 OUString aName;
528 ConvertDataClass eClass;
529 bool bPrefixSupport;
530 public:
531 ConvertData(
532 const sal_Char pUnitName[],
533 double fConvertConstant,
534 ConvertDataClass eClass,
535 bool bPrefSupport = false );
537 virtual ~ConvertData();
539 sal_Int16 GetMatchingLevel( const OUString& rRef ) const;
540 // 0.0 = no equality
541 // 1.0 = matches exact
542 // rest = matches without an assumed prefix of one character
543 // rest gives power for 10 represented by the prefix (e.g. 3 for k or -9 for n
545 /// @throws css::uno::RuntimeException
546 /// @throws css::lang::IllegalArgumentException
547 virtual double Convert( double fVal, const ConvertData& rTo,
548 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const;
549 // converts fVal from this unit to rFrom unit
550 // throws exception if not from same class
551 // this implementation is for proportional cases only
552 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const;
553 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const;
555 inline ConvertDataClass Class() const;
558 class ConvertDataLinear final : public ConvertData
560 double fOffs;
561 public:
562 inline ConvertDataLinear(
563 const sal_Char pUnitName[],
564 double fConvertConstant,
565 double fConvertOffset,
566 ConvertDataClass eClass,
567 bool bPrefSupport = false );
569 virtual ~ConvertDataLinear() override;
571 virtual double Convert( double fVal, const ConvertData& rTo,
572 sal_Int16 nMatchLevelFrom, sal_Int16 nMatchLevelTo ) const override;
573 // for cases where f(x) = a + bx applies (e.g. Temperatures)
575 virtual double ConvertToBase( double fVal, sal_Int16 nMatchLevel ) const override;
576 virtual double ConvertFromBase( double fVal, sal_Int16 nMatchLevel ) const override;
580 class ConvertDataList
582 private:
583 std::vector<ConvertData*> maVector;
584 public:
585 ConvertDataList();
586 ~ConvertDataList();
588 /// @throws css::uno::RuntimeException
589 /// @throws css::lang::IllegalArgumentException
590 double Convert( double fVal, const OUString& rFrom, const OUString& rTo );
594 inline bool IsLeapYear( sal_uInt16 n )
596 return ( (( ( n % 4 ) == 0 ) && ( ( n % 100 ) != 0)) || ( ( n % 400 ) == 0 ) );
600 inline sal_Int32 GetDiffDate360( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nDate1, sal_Int32 nDate2, bool bUSAMethod )
602 return GetDiffDate360( GetNullDate( xOpt ), nDate1, nDate2, bUSAMethod );
606 inline sal_Int16 GetDayOfWeek( sal_Int32 n )
607 { // monday = 0, ..., sunday = 6
608 return static_cast< sal_Int16 >( ( n - 1 ) % 7 );
612 inline double GetYearFrac( const css::uno::Reference< css::beans::XPropertySet >& xOpt, sal_Int32 nStartDate, sal_Int32 nEndDate, sal_Int32 nMode )
614 return GetYearFrac( GetNullDate( xOpt ), nStartDate, nEndDate, nMode );
618 inline const char* FuncData::GetUINameID() const
620 return pUINameID;
624 inline const char** FuncData::GetDescrID() const
626 return pDescrID;
630 inline bool FuncData::IsDouble() const
632 return bDouble;
636 inline const OUString& FuncData::GetSuffix() const
638 return aSuffix;
642 inline bool FuncData::Is( const OUString& r ) const
644 return aIntName == r;
648 inline const std::vector<OUString> & FuncData::GetCompNameList() const
650 return aCompList;
654 inline FDCategory FuncData::GetCategory() const
656 return eCat;
660 inline Complex::Complex( double fReal, double fImag, sal_Unicode cC ) :
661 r( fReal ), i( fImag ), c( cC )
666 inline double Complex::Real() const
668 return r;
672 inline double Complex::Imag() const
674 return i;
678 inline double Complex::Abs() const
680 return sqrt( r * r + i * i );
684 void Complex::Conjugate()
686 i = -i;
690 inline void Complex::Mult( double f )
692 i *= f;
693 r *= f;
697 inline void Complex::Mult( const Complex& rM )
699 double r_ = r;
700 double i_ = i;
702 r = r_ * rM.r - i_ * rM.i;
703 i = r_ * rM.i + i_ * rM.r;
705 if( !c ) c = rM.c;
709 inline void Complex::Sub( const Complex& rC )
711 r -= rC.r;
712 i -= rC.i;
713 if( !c ) c = rC.c;
717 inline void Complex::Add( const Complex& rAdd )
719 r += rAdd.r;
720 i += rAdd.i;
721 if( !c ) c = rAdd.c;
725 inline const Complex& ComplexList::Get( sal_uInt32 n ) const
727 return maVector[n];
731 inline void ComplexList::Append( Complex&& p )
733 maVector.emplace_back(p);
737 inline ConvertDataClass ConvertData::Class() const
739 return eClass;
742 inline ConvertDataLinear::ConvertDataLinear( const sal_Char* p, double fC, double fO, ConvertDataClass e,
743 bool bPrefSupport ) :
744 ConvertData( p, fC, e, bPrefSupport ),
745 fOffs( fO )
750 /// Helper class for date calculation for various financial functions
751 class ScaDate
753 private:
754 sal_uInt16 nOrigDay; /// is the day of the original date.
755 sal_uInt16 nDay; /// is the calculated day depending on the current month/year.
756 sal_uInt16 nMonth; /// is the current month (one-based).
757 sal_uInt16 nYear; /// is the current year.
758 bool bLastDayMode : 1; /// if true, recalculate nDay after every calculation.
759 bool bLastDay : 1; /// is true, if original date was the last day in month.
760 bool b30Days : 1; /// is true, if every month has 30 days in calculations.
761 bool bUSMode : 1; /// is true, if the US method of 30-day-calculations is used.
763 /// Calculates nDay from nOrigDay and current date.
764 void setDay();
766 /// @return count of days in current month
767 inline sal_uInt16 getDaysInMonth() const;
768 /// @return count of days in given month
769 inline sal_uInt16 getDaysInMonth( sal_uInt16 _nMon ) const;
771 /// @ return count of days in the given month range
772 sal_Int32 getDaysInMonthRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
773 /// @ return count of days in the given year range
774 sal_Int32 getDaysInYearRange( sal_uInt16 nFrom, sal_uInt16 nTo ) const;
776 /// Adds/subtracts the given count of years, does not adjust day.
778 /// @throws css::lang::IllegalArgumentException
779 void doAddYears( sal_Int32 nYearCount );
781 public:
782 ScaDate();
783 /** @param nBase
784 date handling mode (days in month / days in year):
785 0 = 30 days / 360 days (US NASD)
786 1 = exact / exact
787 2 = exact / 360
788 3 = exact / 365
789 4 = 30 days / 360 days (Europe)
790 5 = exact / exact (no last day adjustment) */
791 ScaDate( sal_Int32 nNullDate, sal_Int32 nDate, sal_Int32 nBase );
792 ScaDate( const ScaDate& rCopy );
793 ScaDate& operator=( const ScaDate& rCopy );
795 /// @return the current month.
796 sal_uInt16 getMonth() const { return nMonth; };
797 /// @return the current year.
798 sal_uInt16 getYear() const { return nYear; };
800 /// adds/subtracts the given count of months, adjusts day
802 /// @throws css::lang::IllegalArgumentException
803 void addMonths( sal_Int32 nMonthCount );
805 /// sets the given year, adjusts day
806 inline void setYear( sal_uInt16 nNewYear );
807 /// adds/subtracts the given count of years, adjusts day
809 /// @throws css::lang::IllegalArgumentException
810 inline void addYears( sal_Int32 nYearCount );
812 /// @return the internal number of the current date
813 sal_Int32 getDate( sal_Int32 nNullDate ) const;
814 /// @return the number of days between the two dates
816 /// @throws css::lang::IllegalArgumentException
817 static sal_Int32 getDiff( const ScaDate& rFrom, const ScaDate& rTo );
819 bool operator<( const ScaDate& rCmp ) const;
820 bool operator<=( const ScaDate& rCmp ) const { return !(rCmp < *this); }
821 bool operator>( const ScaDate& rCmp ) const { return rCmp < *this; }
822 bool operator>=( const ScaDate& rCmp ) const { return !(*this < rCmp); }
825 inline sal_uInt16 ScaDate::getDaysInMonth() const
827 return getDaysInMonth( nMonth );
830 inline sal_uInt16 ScaDate::getDaysInMonth( sal_uInt16 _nMon ) const
832 return b30Days ? 30 : DaysInMonth( _nMon, nYear );
835 inline void ScaDate::setYear( sal_uInt16 nNewYear )
837 nYear = nNewYear;
838 setDay();
841 inline void ScaDate::addYears( sal_Int32 nYearCount )
843 doAddYears( nYearCount );
844 setDay();
848 /// Helper class for Any->double conversion, using current language settings
849 class ScaAnyConverter
851 private:
852 css::uno::Reference< css::util::XNumberFormatter2 > xFormatter;
853 sal_Int32 nDefaultFormat;
854 bool bHasValidFormat;
856 /** Converts a string to double using the number formatter. If the formatter is not
857 valid, ::rtl::math::stringToDouble() with english separators will be used.
858 @throws css::lang::IllegalArgumentException
859 on strings not representing any double value.
860 @return the converted double value. */
861 double convertToDouble(
862 const OUString& rString ) const;
864 public:
865 explicit ScaAnyConverter(
866 const css::uno::Reference< css::uno::XComponentContext >& xContext );
867 ~ScaAnyConverter();
869 /// Initializing with current language settings
871 /// @throws css::uno::RuntimeException
872 void init(
873 const css::uno::Reference< css::beans::XPropertySet >& xPropSet );
875 /** Converts an Any to double (without initialization).
876 The Any can be empty or contain a double or string.
877 @throws css::lang::IllegalArgumentException
878 on other Any types or on invalid strings.
879 @return true if the Any contains a double or a non-empty valid string,
880 false if the Any is empty or the string is empty */
881 bool getDouble(
882 double& rfResult,
883 const css::uno::Any& rAny ) const;
885 /** Converts an Any to double (with initialization).
886 The Any can be empty or contain a double or string.
887 @throws css::lang::IllegalArgumentException
888 on other Any types or on invalid strings.
889 @return true if the Any contains a double or a non-empty valid string,
890 false if the Any is empty or the string is empty */
891 bool getDouble(
892 double& rfResult,
893 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
894 const css::uno::Any& rAny );
896 /** Converts an Any to double (with initialization).
897 The Any can be empty or contain a double or string.
898 @throws css::lang::IllegalArgumentException
899 on other Any types or on invalid strings.
900 @return the value of the double or string or fDefault if the Any or string is empty */
901 double getDouble(
902 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
903 const css::uno::Any& rAny,
904 double fDefault );
906 /** Converts an Any to sal_Int32 (with initialization).
907 The Any can be empty or contain a double or string.
908 @throws css::lang::IllegalArgumentException
909 on other Any types or on invalid values or strings.
910 @return true if the Any contains a double or a non-empty valid string,
911 false if the Any is empty or the string is empty */
912 bool getInt32(
913 sal_Int32& rnResult,
914 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
915 const css::uno::Any& rAny );
917 /** Converts an Any to sal_Int32 (with initialization).
918 The Any can be empty or contain a double or string.
919 @throws css::lang::IllegalArgumentException
920 on other Any types or on invalid values or strings.
921 @return the truncated value of the double or string or nDefault if the Any or string is empty */
922 sal_Int32 getInt32(
923 const css::uno::Reference< css::beans::XPropertySet >& xPropSet,
924 const css::uno::Any& rAny,
925 sal_Int32 nDefault );
930 #endif
932 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */