LibreOffice Module tools (master) 1
fract.hxx
Go to the documentation of this file.
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 <sal/types.h>
23#include <tools/toolsdllapi.h>
24#include <tools/long.hxx>
25#include <ostream>
26#include <type_traits>
27
28class SvStream;
29
31{
33 sal_Int32 mnNumerator = 0;
34 sal_Int32 mnDenominator = 1;
35 bool mbValid = true;
36public:
37 Fraction() = default;
38 Fraction( const Fraction & rFrac ) = default;
39 Fraction( Fraction && rFrac ) = default;
40 explicit Fraction( double dVal );
41 Fraction( double nNum, double nDen );
42 Fraction( sal_Int64 nNum, sal_Int64 nDen );
43 // just to prevent ambiguity between the sal_Int64 and double constructors
44 template<typename T1, typename T2> Fraction(
45 T1 nNum, T2 nDen,
46 typename std::enable_if<std::is_integral<T1>::value && std::is_integral<T2>::value, int>::type = 0)
47 : Fraction( sal_Int64(nNum), sal_Int64(nDen) ) {}
48
49 bool IsValid() const { return mbValid; }
50
51 sal_Int32 GetNumerator() const;
52 sal_Int32 GetDenominator() const;
53
54 explicit operator sal_Int32() const;
55#if SAL_TYPES_SIZEOFPOINTER == 8
56 explicit operator ::tools::Long() const { return operator sal_Int32(); }
57#endif
58 explicit operator double() const;
59
60 Fraction& operator=( const Fraction& rfrFrac ) = default;
61 Fraction& operator=( Fraction&& rfrFrac ) = default;
62 Fraction& operator=( double v ) { return operator=(Fraction(v)); }
63
64 Fraction& operator+=( const Fraction& rfrFrac );
65 Fraction& operator-=( const Fraction& rfrFrac );
66 Fraction& operator*=( const Fraction& rfrFrac );
67 Fraction& operator/=( const Fraction& rfrFrac );
68 Fraction& operator+=( double v ) { return operator+=(Fraction(v)); }
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
73 void ReduceInaccurate( unsigned nSignificantBits );
74
76 static Fraction MakeFraction(tools::Long nN1, tools::Long nN2, tools::Long nD1, tools::Long nD2);
77
78 // Compute value usable as hash.
79 size_t GetHashValue() const;
80
81 TOOLS_DLLPUBLIC friend Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
82 TOOLS_DLLPUBLIC friend Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
83 TOOLS_DLLPUBLIC friend Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
84 TOOLS_DLLPUBLIC friend Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
85
86 TOOLS_DLLPUBLIC friend bool operator==( const Fraction& rVal1, const Fraction& rVal2 );
87 TOOLS_DLLPUBLIC friend bool operator!=( const Fraction& rVal1, const Fraction& rVal2 );
88 TOOLS_DLLPUBLIC friend bool operator< ( const Fraction& rVal1, const Fraction& rVal2 );
89 TOOLS_DLLPUBLIC friend bool operator> ( const Fraction& rVal1, const Fraction& rVal2 );
90 TOOLS_DLLPUBLIC friend bool operator<=( const Fraction& rVal1, const Fraction& rVal2 );
91 TOOLS_DLLPUBLIC friend bool operator>=( const Fraction& rVal1, const Fraction& rVal2 );
92};
93
94TOOLS_DLLPUBLIC Fraction operator+( const Fraction& rVal1, const Fraction& rVal2 );
95TOOLS_DLLPUBLIC Fraction operator-( const Fraction& rVal1, const Fraction& rVal2 );
96TOOLS_DLLPUBLIC Fraction operator*( const Fraction& rVal1, const Fraction& rVal2 );
97TOOLS_DLLPUBLIC Fraction operator/( const Fraction& rVal1, const Fraction& rVal2 );
98TOOLS_DLLPUBLIC bool operator !=( const Fraction& rVal1, const Fraction& rVal2 );
99TOOLS_DLLPUBLIC bool operator <=( const Fraction& rVal1, const Fraction& rVal2 );
100TOOLS_DLLPUBLIC bool operator >=( const Fraction& rVal1, const Fraction& rVal2 );
101
102inline Fraction operator+( double v1, const Fraction& rVal2 ) { return Fraction(v1) + rVal2; }
103inline Fraction operator-( double v1, const Fraction& rVal2 ) { return Fraction(v1) - rVal2; }
104inline Fraction operator*( double v1, const Fraction& rVal2 ) { return Fraction(v1) * rVal2; }
105inline Fraction operator/( double v1, const Fraction& rVal2 ) { return Fraction(v1) / rVal2; }
106
107inline Fraction operator+( const Fraction& rVal1, double v2 ) { return rVal1 + Fraction(v2); }
108inline Fraction operator-( const Fraction& rVal1, double v2 ) { return rVal1 - Fraction(v2); }
109inline Fraction operator*( const Fraction& rVal1, double v2 ) { return rVal1 * Fraction(v2); }
110inline Fraction operator/( const Fraction& rVal1, double v2 ) { return rVal1 / Fraction(v2); }
111
112template<typename charT, typename traits>
113inline std::basic_ostream<charT, traits> & operator <<(
114 std::basic_ostream<charT, traits> & rStream, const Fraction& rFraction)
115{
116 rStream << "(" << rFraction.GetNumerator() << "/" << rFraction.GetDenominator() << ")";
117 return rStream;
118}
119
120#endif
121
122/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:806
bool operator<(const BigInt &rVal1, const BigInt &rVal2)
Definition: bigint.cxx:818
sal_Int32 GetNumerator() const
Definition: fract.cxx:282
Fraction & operator/=(double v)
Definition: fract.hxx:71
Fraction & operator=(const Fraction &rfrFrac)=default
Fraction & operator+=(double v)
Definition: fract.hxx:68
Fraction & operator=(double v)
Definition: fract.hxx:62
Fraction()=default
Fraction & operator*=(double v)
Definition: fract.hxx:70
Fraction(Fraction &&rFrac)=default
Fraction & operator=(Fraction &&rfrFrac)=default
sal_Int32 GetDenominator() const
Definition: fract.cxx:292
Fraction(T1 nNum, T2 nDen, typename std::enable_if< std::is_integral< T1 >::value &&std::is_integral< T2 >::value, int >::type=0)
Definition: fract.hxx:44
Fraction(const Fraction &rFrac)=default
bool IsValid() const
Definition: fract.hxx:49
Fraction & operator-=(double v)
Definition: fract.hxx:69
float v
bool operator>(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:377
TOOLS_DLLPUBLIC Fraction operator*(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:326
TOOLS_DLLPUBLIC bool operator>=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:350
TOOLS_DLLPUBLIC Fraction operator-(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:319
TOOLS_DLLPUBLIC bool operator<=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:345
TOOLS_DLLPUBLIC bool operator!=(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:340
TOOLS_DLLPUBLIC Fraction operator+(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:312
TOOLS_DLLPUBLIC Fraction operator/(const Fraction &rVal1, const Fraction &rVal2)
Definition: fract.cxx:333
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &rStream, const Fraction &rFraction)
Definition: fract.hxx:113
long Long
Definition: long.hxx:34
bool mbValid
timeval & operator-=(timeval &t1, const timeval &t2)
tools::Rectangle & operator+=(tools::Rectangle &rRect, const SvBorder &rBorder)
Definition: svborder.cxx:22
#define TOOLS_DLLPUBLIC
Definition: toolsdllapi.h:28
#define SAL_WARN_UNUSED
SmFace & operator*=(SmFace &rFace, const Fraction &rFrac)