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 : #ifndef INCLUDED_TOOLS_FRACT_HXX
20 : #define INCLUDED_TOOLS_FRACT_HXX
21 :
22 : #include <tools/toolsdllapi.h>
23 :
24 : class SvStream;
25 :
26 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Fraction
27 : {
28 : private:
29 : long nNumerator;
30 : long nDenominator;
31 :
32 : public:
33 0 : Fraction() { nNumerator = 0; nDenominator = 1; }
34 : Fraction( const Fraction & rFrac );
35 : Fraction( long nNum, long nDen=1 );
36 : Fraction( double dVal );
37 :
38 : bool IsValid() const;
39 :
40 0 : long GetNumerator() const { return nNumerator; }
41 0 : long GetDenominator() const { return nDenominator; }
42 :
43 : operator long() const;
44 : operator double() const;
45 :
46 : Fraction& operator=( const Fraction& rfrFrac );
47 :
48 : Fraction& operator+=( const Fraction& rfrFrac );
49 : Fraction& operator-=( const Fraction& rfrFrac );
50 : Fraction& operator*=( const Fraction& rfrFrac );
51 : Fraction& operator/=( const Fraction& rfrFrac );
52 :
53 : void ReduceInaccurate( unsigned nSignificantBits );
54 :
55 : friend inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
56 : friend inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
57 : friend inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
58 : friend inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
59 :
60 : TOOLS_DLLPUBLIC friend bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
61 : friend inline bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
62 : TOOLS_DLLPUBLIC friend bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
63 : TOOLS_DLLPUBLIC friend bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
64 : friend inline bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
65 : friend inline bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
66 :
67 : TOOLS_DLLPUBLIC friend SvStream& ReadFraction( SvStream& rIStream, Fraction& rFract );
68 : TOOLS_DLLPUBLIC friend SvStream& WriteFraction( SvStream& rOStream, const Fraction& rFract );
69 : };
70 :
71 0 : inline Fraction::Fraction( const Fraction& rFrac )
72 : {
73 0 : nNumerator = rFrac.nNumerator;
74 0 : nDenominator = rFrac.nDenominator;
75 0 : }
76 :
77 0 : inline Fraction& Fraction::operator=( const Fraction& rFrac )
78 : {
79 0 : nNumerator = rFrac.nNumerator;
80 0 : nDenominator = rFrac.nDenominator;
81 0 : return *this;
82 : }
83 :
84 0 : inline bool Fraction::IsValid() const
85 : {
86 0 : return (nDenominator > 0);
87 : }
88 :
89 0 : inline Fraction::operator long() const
90 : {
91 0 : if ( nDenominator > 0 )
92 0 : return (nNumerator / nDenominator);
93 : else
94 0 : return 0;
95 : }
96 :
97 0 : inline Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 )
98 : {
99 0 : Fraction aErg( rVal1 );
100 0 : aErg += rVal2;
101 0 : return aErg;
102 : }
103 :
104 : inline Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 )
105 : {
106 : Fraction aErg( rVal1 );
107 : aErg -= rVal2;
108 : return aErg;
109 : }
110 :
111 0 : inline Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 )
112 : {
113 0 : Fraction aErg( rVal1 );
114 0 : aErg *= rVal2;
115 0 : return aErg;
116 : }
117 :
118 0 : inline Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 )
119 : {
120 0 : Fraction aErg( rVal1 );
121 0 : aErg /= rVal2;
122 0 : return aErg;
123 : }
124 :
125 0 : inline bool operator !=( const Fraction& rVal1, const Fraction& rVal2 )
126 : {
127 0 : return !(rVal1 == rVal2);
128 : }
129 :
130 0 : inline bool operator <=( const Fraction& rVal1, const Fraction& rVal2 )
131 : {
132 0 : return !(rVal1 > rVal2);
133 : }
134 :
135 : inline bool operator >=( const Fraction& rVal1, const Fraction& rVal2 )
136 : {
137 : return !(rVal1 < rVal2);
138 : }
139 :
140 : #endif
141 :
142 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|