Audacity  3.0.3
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 
17 class MATH_API sampleCount
18 {
19 public:
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 
67 private:
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 
131 MATH_API
132 size_t limitSampleBufferSize( size_t bufferSize, sampleCount limit );
133 
134 #endif
operator!=
bool operator!=(sampleCount a, sampleCount b)
Definition: SampleCount.h:76
sampleCount::as_float
float as_float() const
Definition: SampleCount.h:44
sampleCount::as_double
double as_double() const
Definition: SampleCount.h:45
sampleCount::as_long_long
long long as_long_long() const
Definition: SampleCount.h:47
operator>=
bool operator>=(sampleCount a, sampleCount b)
Definition: SampleCount.h:86
sampleCount::sampleCount
sampleCount(unsigned long long v)
Definition: SampleCount.h:27
sampleCount::sampleCount
sampleCount(type v)
Definition: SampleCount.h:26
operator+
sampleCount operator+(sampleCount a, sampleCount b)
Definition: SampleCount.h:101
sampleCount::sampleCount
sampleCount(unsigned v)
Definition: SampleCount.h:29
operator-
sampleCount operator-(sampleCount a, sampleCount b)
Definition: SampleCount.h:106
operator/
sampleCount operator/(sampleCount a, sampleCount b)
Definition: SampleCount.h:116
operator<
bool operator<(sampleCount a, sampleCount b)
Definition: SampleCount.h:81
sampleCount::sampleCount
sampleCount()
Definition: SampleCount.h:23
sampleCount::type
long long type
Definition: SampleCount.h:20
operator>
bool operator>(sampleCount a, sampleCount b)
Definition: SampleCount.h:91
operator%
sampleCount operator%(sampleCount a, sampleCount b)
Definition: SampleCount.h:121
limitSampleBufferSize
MATH_API size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:23
operator==
bool operator==(sampleCount a, sampleCount b)
Definition: SampleCount.h:71
sampleCount::sampleCount
sampleCount(long v)
Definition: SampleCount.h:30
sampleCount::sampleCount
sampleCount(double d)
Definition: SampleCount.h:39
sampleCount::sampleCount
sampleCount(unsigned long v)
Definition: SampleCount.h:33
operator<=
bool operator<=(sampleCount a, sampleCount b)
Definition: SampleCount.h:96
sampleCount
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:18
sampleCount::value
type value
Definition: SampleCount.h:68
sampleCount::sampleCount
sampleCount(int v)
Definition: SampleCount.h:28
sampleCount::sampleCount
sampleCount(const sampleCount &)=default
operator*
sampleCount operator*(sampleCount a, sampleCount b)
Definition: SampleCount.h:111
sampleCount::sampleCount
sampleCount(float f)
Definition: SampleCount.h:38