Audacity 3.2.0
SampleCount.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file SampleCount.h
6
7 Paul Licameli split from audacity/Types.h
8
9**********************************************************************/
10#ifndef __AUDACITY_SAMPLE_COUNT__
11#define __AUDACITY_SAMPLE_COUNT__
12
13#include <cstddef>
14#include <limits>
15
17
18class MATH_API sampleCount
19{
20public:
21 using type = long long;
22 static_assert(sizeof(type) == 8, "Wrong width of sampleCount");
23
24 sampleCount () : value { 0 } {}
25
26 // Allow implicit conversion from integral types
27 sampleCount ( type v ) : value { v } {}
28 sampleCount ( unsigned long long v ) : value ( v ) {}
29 sampleCount ( int v ) : value { v } {}
30 sampleCount ( unsigned v ) : value { v } {}
31 sampleCount ( long v ) : value { v } {}
32
33 // unsigned long is 64 bit on some platforms. Let it narrow.
34 sampleCount ( unsigned long v ) : value ( v ) {}
35
36 // Beware implicit conversions from floating point values!
37 // Otherwise the meaning of binary operators with sampleCount change
38 // their meaning when sampleCount is not an alias!
39 explicit sampleCount ( float f ) : value ( f ) {}
40 explicit sampleCount ( double d ) : value ( d ) {}
41
42 sampleCount ( const sampleCount& ) = default;
43 sampleCount &operator= ( const sampleCount& ) = default;
44
45 float as_float() const { return value; }
46 double as_double() const { return value; }
47
48 long long as_long_long() const { return value; }
49
50 size_t as_size_t() const;
51
52 sampleCount &operator += (sampleCount b) { value += b.value; return *this; }
53 sampleCount &operator -= (sampleCount b) { value -= b.value; return *this; }
54 sampleCount &operator *= (sampleCount b) { value *= b.value; return *this; }
55 sampleCount &operator /= (sampleCount b) { value /= b.value; return *this; }
56 sampleCount &operator %= (sampleCount b) { value %= b.value; return *this; }
57
58 sampleCount operator - () const { return -value; }
59
60 sampleCount &operator ++ () { ++value; return *this; }
61 sampleCount operator ++ (int)
62 { sampleCount result{ *this }; ++value; return result; }
63
64 sampleCount &operator -- () { --value; return *this; }
65 sampleCount operator -- (int)
66 { sampleCount result{ *this }; --value; return result; }
67
69 static sampleCount max() { return std::numeric_limits<type>::max(); }
70
71private:
73};
74
76{
77 return a.as_long_long() == b.as_long_long();
78}
79
81{
82 return !(a == b);
83}
84
86{
87 return a.as_long_long() < b.as_long_long();
88}
89
91{
92 return !(a < b);
93}
94
96{
97 return b < a;
98}
99
101{
102 return !(b < a);
103}
104
106{
107 return sampleCount{ a } += b;
108}
109
111{
112 return sampleCount{ a } -= b;
113}
114
116{
117 return sampleCount{ a } *= b;
118}
119
121{
122 return sampleCount{ a } /= b;
123}
124
126{
127 return sampleCount{ a } %= b;
128}
129
130// ----------------------------------------------------------------------------
131// Function returning the minimum of a sampleCount and a size_t,
132// hiding the casts
133// ----------------------------------------------------------------------------
134
135MATH_API
136size_t limitSampleBufferSize( size_t bufferSize, sampleCount limit );
137
138#endif
int min(int a, int b)
bool operator<=(sampleCount a, sampleCount b)
Definition: SampleCount.h:100
sampleCount operator*(sampleCount a, sampleCount b)
Definition: SampleCount.h:115
MATH_API size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
bool operator>(sampleCount a, sampleCount b)
Definition: SampleCount.h:95
sampleCount operator+(sampleCount a, sampleCount b)
Definition: SampleCount.h:105
bool operator!=(sampleCount a, sampleCount b)
Definition: SampleCount.h:80
sampleCount operator-(sampleCount a, sampleCount b)
Definition: SampleCount.h:110
bool operator<(sampleCount a, sampleCount b)
Definition: SampleCount.h:85
bool operator==(sampleCount a, sampleCount b)
Definition: SampleCount.h:75
sampleCount operator/(sampleCount a, sampleCount b)
Definition: SampleCount.h:120
sampleCount operator%(sampleCount a, sampleCount b)
Definition: SampleCount.h:125
bool operator>=(sampleCount a, sampleCount b)
Definition: SampleCount.h:90
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
sampleCount(double d)
Definition: SampleCount.h:40
sampleCount(unsigned v)
Definition: SampleCount.h:30
long long as_long_long() const
Definition: SampleCount.h:48
sampleCount(unsigned long v)
Definition: SampleCount.h:34
static sampleCount min()
Definition: SampleCount.h:68
sampleCount(long v)
Definition: SampleCount.h:31
sampleCount(type v)
Definition: SampleCount.h:27
sampleCount(float f)
Definition: SampleCount.h:39
long long type
Definition: SampleCount.h:21
sampleCount(int v)
Definition: SampleCount.h:29
sampleCount(unsigned long long v)
Definition: SampleCount.h:28
static sampleCount max()
Definition: SampleCount.h:69
float as_float() const
Definition: SampleCount.h:45
double as_double() const
Definition: SampleCount.h:46
sampleCount(const sampleCount &)=default