fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / include / tools / bigint.hxx
blobd3a2c2f2cff523774639756c455e4ae4821ad4b0
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 _BIGINT_HXX
20 #define _BIGINT_HXX
22 #include <climits>
23 #include <rtl/ustring.hxx>
24 #include "tools/toolsdllapi.h"
25 #include <tools/solar.h>
27 class SvStream;
29 #ifdef _TLBIGINT_INT64
30 struct SbxINT64;
31 struct SbxUINT64;
32 #endif
34 #define MAX_DIGITS 8
36 class Fraction;
38 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED BigInt
40 private:
41 long nVal;
42 unsigned short nNum[MAX_DIGITS];
43 sal_uInt8 nLen : 5; // current length
44 sal_Bool bIsNeg : 1, // Is Sign negative?
45 bIsBig : 1, // sal_True == BigInt
46 bIsSet : 1; // Not "Null" (not "not 0")
48 TOOLS_DLLPRIVATE void MakeBigInt(BigInt const &);
49 TOOLS_DLLPRIVATE void Normalize();
50 TOOLS_DLLPRIVATE void Mult(BigInt const &, sal_uInt16);
51 TOOLS_DLLPRIVATE void Div(sal_uInt16, sal_uInt16 &);
52 TOOLS_DLLPRIVATE sal_Bool IsLess(BigInt const &) const;
53 TOOLS_DLLPRIVATE void AddLong(BigInt &, BigInt &);
54 TOOLS_DLLPRIVATE void SubLong(BigInt &, BigInt &);
55 TOOLS_DLLPRIVATE void MultLong(BigInt const &, BigInt &) const;
56 TOOLS_DLLPRIVATE void DivLong(BigInt const &, BigInt &) const;
57 TOOLS_DLLPRIVATE void ModLong(BigInt const &, BigInt &) const;
58 TOOLS_DLLPRIVATE sal_Bool ABS_IsLess(BigInt const &) const;
60 public:
61 BigInt();
62 BigInt( short nVal );
63 BigInt( long nVal );
64 BigInt( int nVal );
65 BigInt( double nVal );
66 BigInt( sal_uInt16 nVal );
67 BigInt( sal_uInt32 nVal );
68 BigInt( const BigInt& rBigInt );
69 BigInt( const OUString& rString );
70 #ifdef _TLBIGINT_INT64
71 BigInt( const SbxINT64 &r );
72 BigInt( const SbxUINT64 &r );
73 #endif
75 operator short() const;
76 operator long() const;
77 operator int() const;
78 operator double() const;
79 operator sal_uInt16() const;
80 operator sal_uIntPtr() const;
82 void Set( sal_Bool bSet ) { bIsSet = bSet; }
83 OUString GetString() const;
85 sal_Bool IsSet() const { return bIsSet; }
86 sal_Bool IsNeg() const;
87 sal_Bool IsZero() const;
88 sal_Bool IsOne() const;
89 sal_Bool IsLong() const { return !bIsBig; }
90 void Abs();
91 #ifdef _TLBIGINT_INT64
92 sal_Bool INT64 ( SbxINT64 *p ) const;
93 sal_Bool UINT64( SbxUINT64 *p ) const;
94 #endif
96 BigInt& operator =( const BigInt& rVal );
97 BigInt& operator +=( const BigInt& rVal );
98 BigInt& operator -=( const BigInt& rVal );
99 BigInt& operator *=( const BigInt& rVal );
100 BigInt& operator /=( const BigInt& rVal );
101 BigInt& operator %=( const BigInt& rVal );
103 BigInt& operator =( const short nValue );
104 BigInt& operator =( const long nValue );
105 BigInt& operator =( const int nValue );
106 BigInt& operator =( const sal_uInt16 nValue );
108 friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 );
109 friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 );
110 friend inline BigInt operator *( const BigInt& rVal1, const BigInt& rVal2 );
111 friend inline BigInt operator /( const BigInt& rVal1, const BigInt& rVal2 );
112 friend inline BigInt operator %( const BigInt& rVal1, const BigInt& rVal2 );
114 TOOLS_DLLPUBLIC friend sal_Bool operator==( const BigInt& rVal1, const BigInt& rVal2 );
115 friend inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 );
116 TOOLS_DLLPUBLIC friend sal_Bool operator< ( const BigInt& rVal1, const BigInt& rVal2 );
117 TOOLS_DLLPUBLIC friend sal_Bool operator> ( const BigInt& rVal1, const BigInt& rVal2 );
118 friend inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 );
119 friend inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 );
121 friend class Fraction;
124 inline BigInt::BigInt()
126 bIsSet = sal_False;
127 bIsBig = sal_False;
128 nVal = 0;
131 inline BigInt::BigInt( short nValue )
133 bIsSet = sal_True;
134 bIsBig = sal_False;
135 nVal = nValue;
138 inline BigInt::BigInt( long nValue )
140 bIsSet = sal_True;
141 bIsBig = sal_False;
142 nVal = nValue;
145 inline BigInt::BigInt( int nValue )
147 bIsSet = sal_True;
148 bIsBig = sal_False;
149 nVal = nValue;
152 inline BigInt::BigInt( sal_uInt16 nValue )
154 bIsSet = sal_True;
155 bIsBig = sal_False;
156 nVal = nValue;
159 inline BigInt::operator short() const
161 if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX )
162 return (short)nVal;
163 else
164 return 0;
167 inline BigInt::operator long() const
169 if ( !bIsBig )
170 return nVal;
171 else
172 return 0;
175 inline BigInt::operator int() const
177 if ( !bIsBig && (nVal == (long)(int)nVal) )
178 return (int)nVal;
179 else
180 return 0;
183 inline BigInt::operator sal_uInt16() const
185 if ( !bIsBig && nVal >= 0 && nVal <= USHRT_MAX )
186 return (sal_uInt16)nVal;
187 else
188 return 0;
191 inline BigInt& BigInt::operator =( const short nValue )
193 bIsSet = sal_True;
194 bIsBig = sal_False;
195 nVal = nValue;
197 return *this;
200 inline BigInt& BigInt::operator =( const long nValue )
202 bIsSet = sal_True;
203 bIsBig = sal_False;
204 nVal = nValue;
206 return *this;
209 inline BigInt& BigInt::operator =( const int nValue )
211 bIsSet = sal_True;
212 bIsBig = sal_False;
213 nVal = nValue;
215 return *this;
218 inline BigInt& BigInt::operator =( const sal_uInt16 nValue )
220 bIsSet = sal_True;
221 bIsBig = sal_False;
222 nVal = nValue;
224 return *this;
227 inline sal_Bool BigInt::IsNeg() const
229 if ( !bIsBig )
230 return (nVal < 0);
231 else
232 return (sal_Bool)bIsNeg;
235 inline sal_Bool BigInt::IsZero() const
237 if ( bIsBig )
238 return sal_False;
239 else
240 return (nVal == 0);
243 inline sal_Bool BigInt::IsOne() const
245 if ( bIsBig )
246 return sal_False;
247 else
248 return (nVal == 1);
251 inline void BigInt::Abs()
253 if ( bIsBig )
254 bIsNeg = sal_False;
255 else if ( nVal < 0 )
256 nVal = -nVal;
259 inline BigInt operator+( const BigInt &rVal1, const BigInt &rVal2 )
261 BigInt aErg( rVal1 );
262 aErg += rVal2;
263 return aErg;
266 inline BigInt operator-( const BigInt &rVal1, const BigInt &rVal2 )
268 BigInt aErg( rVal1 );
269 aErg -= rVal2;
270 return aErg;
273 inline BigInt operator*( const BigInt &rVal1, const BigInt &rVal2 )
275 BigInt aErg( rVal1 );
276 aErg *= rVal2;
277 return aErg;
280 inline BigInt operator/( const BigInt &rVal1, const BigInt &rVal2 )
282 BigInt aErg( rVal1 );
283 aErg /= rVal2;
284 return aErg;
287 inline BigInt operator%( const BigInt &rVal1, const BigInt &rVal2 )
289 BigInt aErg( rVal1 );
290 aErg %= rVal2;
291 return aErg;
294 inline sal_Bool operator!=( const BigInt& rVal1, const BigInt& rVal2 )
296 return !(rVal1 == rVal2);
299 inline sal_Bool operator<=( const BigInt& rVal1, const BigInt& rVal2 )
301 return !( rVal1 > rVal2);
304 inline sal_Bool operator>=( const BigInt& rVal1, const BigInt& rVal2 )
306 return !(rVal1 < rVal2);
309 #endif
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */