Resolves tdf#133662 - Copy version info reworked
[LibreOffice.git] / include / tools / fract.hxx
blobed1f5f0be64922cad92ebea900afb2172a4a4970
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_TOOLS_FRACT_HXX
20 #define INCLUDED_TOOLS_FRACT_HXX
22 #include <sal/types.h>
23 #include <tools/toolsdllapi.h>
24 #include <memory>
25 #include <ostream>
26 #include <type_traits>
28 class SvStream;
30 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Fraction final
32 /// these two fields form a boost::rational, but I didn't want to put more boost headers into the global space
33 sal_Int32 mnNumerator = 0;
34 sal_Int32 mnDenominator = 1;
35 bool mbValid = true;
37 public:
38 Fraction() = default;
39 Fraction( const Fraction & rFrac ) = default;
40 Fraction( Fraction && rFrac ) = default;
41 explicit Fraction( double dVal );
42 Fraction( double nNum, double nDen );
43 Fraction( sal_Int64 nNum, sal_Int64 nDen );
44 // just to prevent ambiguity between the sal_Int64 and double constructors
45 template<typename T1, typename T2> Fraction(
46 T1 nNum, T2 nDen,
47 typename std::enable_if<std::is_integral<T1>::value && std::is_integral<T2>::value, int>::type = 0)
48 : Fraction( sal_Int64(nNum), sal_Int64(nDen) ) {}
50 bool IsValid() const { return mbValid; }
52 sal_Int32 GetNumerator() const;
53 sal_Int32 GetDenominator() const;
55 explicit operator sal_Int32() const;
56 #if SAL_TYPES_SIZEOFLONG == 8
57 explicit operator long() const { return sal_Int32(*this); }
58 #endif
59 explicit operator double() const;
61 Fraction& operator=( const Fraction& rfrFrac ) = default;
62 Fraction& operator=( Fraction&& rfrFrac ) = default;
63 Fraction& operator=( double v ) { return operator=(Fraction(v)); }
65 Fraction& operator+=( const Fraction& rfrFrac );
66 Fraction& operator-=( const Fraction& rfrFrac );
67 Fraction& operator*=( const Fraction& rfrFrac );
68 Fraction& operator/=( const Fraction& rfrFrac );
69 Fraction& operator+=( double v ) { return operator+=(Fraction(v)); }
70 Fraction& operator-=( double v ) { return operator-=(Fraction(v)); }
71 Fraction& operator*=( double v ) { return operator*=(Fraction(v)); }
72 Fraction& operator/=( double v ) { return operator/=(Fraction(v)); }
74 void ReduceInaccurate( unsigned nSignificantBits );
76 TOOLS_DLLPUBLIC friend Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
77 TOOLS_DLLPUBLIC friend Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
78 TOOLS_DLLPUBLIC friend Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
79 TOOLS_DLLPUBLIC friend Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
81 TOOLS_DLLPUBLIC friend bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
82 TOOLS_DLLPUBLIC friend bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
83 TOOLS_DLLPUBLIC friend bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
84 TOOLS_DLLPUBLIC friend bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
85 TOOLS_DLLPUBLIC friend bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
86 TOOLS_DLLPUBLIC friend bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
88 TOOLS_DLLPUBLIC friend SvStream& ReadFraction( SvStream& rIStream, Fraction & rFract );
89 TOOLS_DLLPUBLIC friend SvStream& WriteFraction( SvStream& rOStream, const Fraction& rFract );
92 TOOLS_DLLPUBLIC Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
93 TOOLS_DLLPUBLIC Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
94 TOOLS_DLLPUBLIC Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
95 TOOLS_DLLPUBLIC Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
96 TOOLS_DLLPUBLIC bool operator !=( const Fraction& rVal1, const Fraction& rVal2 );
97 TOOLS_DLLPUBLIC bool operator <=( const Fraction& rVal1, const Fraction& rVal2 );
98 TOOLS_DLLPUBLIC bool operator >=( const Fraction& rVal1, const Fraction& rVal2 );
100 inline Fraction operator+( double v1, const Fraction& rVal2 ) { return Fraction(v1) + rVal2; }
101 inline Fraction operator-( double v1, const Fraction& rVal2 ) { return Fraction(v1) - rVal2; }
102 inline Fraction operator*( double v1, const Fraction& rVal2 ) { return Fraction(v1) * rVal2; }
103 inline Fraction operator/( double v1, const Fraction& rVal2 ) { return Fraction(v1) / rVal2; }
105 inline Fraction operator+( const Fraction& rVal1, double v2 ) { return rVal1 + Fraction(v2); }
106 inline Fraction operator-( const Fraction& rVal1, double v2 ) { return rVal1 - Fraction(v2); }
107 inline Fraction operator*( const Fraction& rVal1, double v2 ) { return rVal1 * Fraction(v2); }
108 inline Fraction operator/( const Fraction& rVal1, double v2 ) { return rVal1 / Fraction(v2); }
110 template<typename charT, typename traits>
111 inline std::basic_ostream<charT, traits> & operator <<(
112 std::basic_ostream<charT, traits> & rStream, const Fraction& rFraction)
114 rStream << "(" << rFraction.GetNumerator() << "/" << rFraction.GetDenominator() << ")";
115 return rStream;
118 #endif
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */