fraction.cpp
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* akvirtualcamera, virtual camera for Mac and Windows.
* Copyright (C) 2020 Gonzalo Exequiel Pedone
*
* akvirtualcamera is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* akvirtualcamera is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
*
* Web-Site: http://webcamoid.github.io/
*/
#include <cmath>
#include <cstdlib>
#include <sstream>
#include <string>
#include "fraction.h"
#include "utils.h"
namespace AkVCam
{
class FractionPrivate
{
public:
int64_t m_num;
int64_t m_den;
template<typename T> inline static int sign(T value)
{
return value < 0? -1: 1;
}
};
}
AkVCam::Fraction::Fraction()
{
this->d = new FractionPrivate;
this->d->m_num = 0;
this->d->m_den = 0;
}
AkVCam::Fraction::Fraction(int64_t num, int64_t den)
{
this->d = new FractionPrivate;
this->d->m_num = num;
this->d->m_den = den;
}
AkVCam::Fraction::Fraction(const std::string &str)
{
this->d = new FractionPrivate;
this->d->m_num = 0;
this->d->m_den = 1;
auto pos = str.find('/');
if (pos == std::string::npos) {
auto strCpy = trimmed(str);
this->d->m_num = uint32_t(strtol(strCpy.c_str(), nullptr, 10));
} else {
auto numStr = trimmed(str.substr(0, pos));
auto denStr = trimmed(str.substr(pos + 1));
this->d->m_num = uint32_t(strtol(numStr.c_str(), nullptr, 10));
this->d->m_den = uint32_t(strtol(denStr.c_str(), nullptr, 10));
if (this->d->m_den < 1) {
this->d->m_num = 0;
this->d->m_den = 1;
}
}
}
AkVCam::Fraction::Fraction(const Fraction &other)
{
this->d = new FractionPrivate;
this->d->m_num = other.d->m_num;
this->d->m_den = other.d->m_den;
}
AkVCam::Fraction::~Fraction()
{
delete this->d;
}
AkVCam::Fraction &AkVCam::Fraction::operator =(const Fraction &other)
{
if (this != &other) {
this->d->m_num = other.d->m_num;
this->d->m_den = other.d->m_den;
}
return *this;
}
bool AkVCam::Fraction::operator ==(const Fraction &other) const
{
if (this->d->m_den == 0 && other.d->m_den != 0)
return false;
if (this->d->m_den != 0 && other.d->m_den == 0)
return false;
return this->d->m_num * other.d->m_den == this->d->m_den * other.d->m_num;
}
bool AkVCam::Fraction::operator <(const Fraction &other) const
{
return this->d->m_num * other.d->m_den < this->d->m_den * other.d->m_num;
}
int64_t AkVCam::Fraction::num() const
{
return this->d->m_num;
}
int64_t &AkVCam::Fraction::num()
{
return this->d->m_num;
}
int64_t AkVCam::Fraction::den() const
{
return this->d->m_den;
}
int64_t &AkVCam::Fraction::den()
{
return this->d->m_den;
}
double AkVCam::Fraction::value() const
{
return double(this->d->m_num) / double(this->d->m_den);
}
std::string AkVCam::Fraction::toString() const
{
std::stringstream ss;
ss << this->d->m_num << '/' << this->d->m_den;
return ss.str();
}
bool AkVCam::Fraction::isInfinity() const
{
return this->d->m_num != 0 && this->d->m_den == 0;
}
int AkVCam::Fraction::sign() const
{
return FractionPrivate::sign(this->d->m_num) == FractionPrivate::sign(this->d->m_den)? 1: -1;
}
bool AkVCam::Fraction::isFraction(const std::string &str)
{
auto pos = str.find('/');
if (pos == std::string::npos) {
auto strCpy = trimmed(str);
char *p = nullptr;
strtol(strCpy.c_str(), &p, 10);
if (*p)
return false;
} else {
auto numStr = trimmed(str.substr(0, pos));
auto denStr = trimmed(str.substr(pos + 1));
char *p = nullptr;
char *q = nullptr;
strtol(numStr.c_str(), &p, 10);
strtol(denStr.c_str(), &q, 10);
if (*p || *q)
return false;
}
return true;
}
std::ostream &operator <<(std::ostream &os, const AkVCam::Fraction &fraction)
{
os << fraction.toString();
return os;
}