AVPKit
Rational.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef RATIONAL_H_
21 #define RATIONAL_H_
22 
23 #include <com/avpkit/core/FfmpegIncludes.h>
24 #include <com/avpkit/core/IRational.h>
25 
26 namespace com { namespace avpkit { namespace core
27 {
28 
29  class Rational : public IRational
30  {
31  VS_JNIUTILS_REFCOUNTED_OBJECT(Rational);
32  public:
33 
34  // IRational Interface implementation
35  virtual IRational* copy();
36  virtual int32_t getNumerator() { return mRational.num; }
37  virtual int32_t getDenominator() { return mRational.den; }
38  virtual int32_t compareTo(IRational*other);
39  virtual double getDouble();
40  virtual int32_t reduce(int64_t num, int64_t den, int64_t max);
41  virtual IRational* multiply(IRational *arg);
42  virtual IRational* divide(IRational *arg);
43  virtual IRational* subtract(IRational *arg);
44  virtual IRational* add(IRational *arg);
45  virtual int64_t rescale(int64_t origValue, IRational* origBase);
46 
52  static Rational *make(double d);
60  static Rational *make(AVRational *src);
71  static Rational* make(Rational *src);
72 
84  static Rational *make(int32_t num, int32_t den);
85 
86  virtual int64_t rescale(int64_t origValue,
87  IRational* origBase,
88  Rounding rounding);
89 
90  static int64_t rescale(int64_t srcValue,
91  int32_t dstNumerator,
92  int32_t dstDenominator,
93  int32_t srcNumerator,
94  int32_t srcDenominator,
95  Rounding rounding);
96 
97  virtual void setNumerator(int32_t value);
98  virtual void setDenominator(int32_t value);
99  virtual void setValue(double value);
100  virtual double getValue();
101  virtual bool isFinalized();
102  virtual void init();
103  protected:
104  Rational();
105  virtual ~Rational();
106  private:
107  // note not a pointer.
108  AVRational mRational;
109  bool mInitialized;
110  };
111 
112 }}}
113 
114 #endif /*RATIONAL_H_*/
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
static IRational * make()
Get a new rational that will be set to 0/0.
Definition: IRational.cpp:79
virtual void init()
Marks this object as finalized and immutable.
Definition: Rational.cpp:41
virtual IRational * multiply(IRational *arg)
Multiplies this number by arg.
Definition: Rational.cpp:177
virtual void setDenominator(int32_t value)
Sets the denominator on this object.
Definition: Rational.cpp:118
virtual double getValue()
An alias for getDouble() but matching JavaBean conventions.
Definition: Rational.cpp:71
virtual double getDouble()
Rational to double conversion.
Definition: Rational.cpp:151
virtual int32_t getDenominator()
Get the denominator for this rational.
Definition: Rational.h:37
virtual int32_t reduce(int64_t num, int64_t den, int64_t max)
Reduce a fraction to it's lowest common denominators.
Definition: Rational.cpp:168
virtual void setValue(double value)
Sets the numerator and denominator on this object by reducing the double to the closest integer numer...
Definition: Rational.cpp:64
virtual IRational * divide(IRational *arg)
Divides this rational by arg.
Definition: Rational.cpp:194
virtual int64_t rescale(int64_t origValue, IRational *origBase)
Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this...
Definition: Rational.cpp:244
virtual void setNumerator(int32_t value)
Sets the numerator on this object.
Definition: Rational.cpp:112
virtual IRational * add(IRational *arg)
Adds arg to this rational.
Definition: Rational.cpp:227
virtual int32_t compareTo(IRational *other)
Compare a rational to this rational.
Definition: Rational.cpp:141
virtual bool isFinalized()
Returns true if init() has been called and this object is now considered finalized and immutable.
Definition: Rational.cpp:124
virtual int32_t getNumerator()
Get the numerator for this rational.
Definition: Rational.h:36
virtual IRational * subtract(IRational *arg)
Subtracts arg from this rational.
Definition: Rational.cpp:211
virtual IRational * copy()
Creates a new IRational object by copying (by value) this object.
Definition: Rational.cpp:92
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...