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
16
17class MATH_API sampleCount
18{
19public:
20 using type = long long;
21 static_assert(sizeof(type) == 8, "Wrong width of sampleCount");
22
23 sampleCount () : value { 0 } {}
24
25 // Allow implicit conversion from integral types
26 sampleCount ( type v ) : value { v } {}
27 sampleCount ( unsigned long long v ) : value ( v ) {}
28 sampleCount ( int v ) : value { v } {}
29 sampleCount ( unsigned v ) : value { v } {}
30 sampleCount ( long v ) : value { v } {}
31
32 // unsigned long is 64 bit on some platforms. Let it narrow.
33 sampleCount ( unsigned long v ) : value ( v ) {}
34
35 // Beware implicit conversions from floating point values!
36 // Otherwise the meaning of binary operators with sampleCount change
37 // their meaning when sampleCount is not an alias!
38 explicit sampleCount ( float f ) : value ( f ) {}
39 explicit sampleCount ( double d ) : value ( d ) {}
40
41 sampleCount ( const sampleCount& ) = default;
42 sampleCount &operator= ( const sampleCount& ) = default;
43
44 float as_float() const { return value; }
45 double as_double() const { return value; }
46
47 long long as_long_long() const { return value; }
48
49 size_t as_size_t() const;
50
51 sampleCount &operator += (sampleCount b) { value += b.value; return *this; }
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
57 sampleCount operator - () const { return -value; }
58
59 sampleCount &operator ++ () { ++value; return *this; }
60 sampleCount operator ++ (int)
61 { sampleCount result{ *this }; ++value; return result; }
62
63 sampleCount &operator -- () { --value; return *this; }
64 sampleCount operator -- (int)
65 { sampleCount result{ *this }; --value; return result; }
66
67private:
69};
70
72{
73 return a.as_long_long() == b.as_long_long();
74}
75
77{
78 return !(a == b);
79}
80
82{
83 return a.as_long_long() < b.as_long_long();
84}
85
87{
88 return !(a < b);
89}
90
92{
93 return b < a;
94}
95
97{
98 return !(b < a);
99}
100
102{
103 return sampleCount{ a } += b;
104}
105
107{
108 return sampleCount{ a } -= b;
109}
110
112{
113 return sampleCount{ a } *= b;
114}
115
117{
118 return sampleCount{ a } /= b;
119}
120
122{
123 return sampleCount{ a } %= b;
124}
125
126// ----------------------------------------------------------------------------
127// Function returning the minimum of a sampleCount and a size_t,
128// hiding the casts
129// ----------------------------------------------------------------------------
130
131MATH_API
132size_t limitSampleBufferSize( size_t bufferSize, sampleCount limit );
133
134#endif
bool operator<=(sampleCount a, sampleCount b)
Definition: SampleCount.h:96
sampleCount operator*(sampleCount a, sampleCount b)
Definition: SampleCount.h:111
MATH_API size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
bool operator>(sampleCount a, sampleCount b)
Definition: SampleCount.h:91
sampleCount operator+(sampleCount a, sampleCount b)
Definition: SampleCount.h:101
bool operator!=(sampleCount a, sampleCount b)
Definition: SampleCount.h:76
sampleCount operator-(sampleCount a, sampleCount b)
Definition: SampleCount.h:106
bool operator<(sampleCount a, sampleCount b)
Definition: SampleCount.h:81
bool operator==(sampleCount a, sampleCount b)
Definition: SampleCount.h:71
sampleCount operator/(sampleCount a, sampleCount b)
Definition: SampleCount.h:116
sampleCount operator%(sampleCount a, sampleCount b)
Definition: SampleCount.h:121
bool operator>=(sampleCount a, sampleCount b)
Definition: SampleCount.h:86
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
sampleCount(double d)
Definition: SampleCount.h:39
sampleCount(unsigned v)
Definition: SampleCount.h:29
long long as_long_long() const
Definition: SampleCount.h:47
sampleCount(unsigned long v)
Definition: SampleCount.h:33
sampleCount(long v)
Definition: SampleCount.h:30
sampleCount(type v)
Definition: SampleCount.h:26
sampleCount(float f)
Definition: SampleCount.h:38
long long type
Definition: SampleCount.h:20
sampleCount(int v)
Definition: SampleCount.h:28
sampleCount(unsigned long long v)
Definition: SampleCount.h:27
float as_float() const
Definition: SampleCount.h:44
double as_double() const
Definition: SampleCount.h:45
sampleCount(const sampleCount &)=default