-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFBAudioFileWrapper.hpp
102 lines (80 loc) · 2.23 KB
/
SFBAudioFileWrapper.hpp
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
//
// Copyright (c) 2021 - 2024 Stephen F. Booth <me@sbooth.org>
// Part of https://github.com/sbooth/CXXAudioUtilities
// MIT license
//
#pragma once
#import <AudioToolbox/AudioFile.h>
namespace SFB {
/// A bare-bones wrapper around @c AudioFile that does little more than call @c AudioFileClose on destruction
class AudioFileWrapper
{
public:
/// Creates an empty @c AudioFileWrapper
constexpr AudioFileWrapper() noexcept = default;
// This class is non-copyable
AudioFileWrapper(const AudioFileWrapper& rhs) = delete;
// This class is non-assignable
AudioFileWrapper& operator=(const AudioFileWrapper& rhs) = delete;
/// Calls @c AudioFileClose on the managed @c AudioFile
inline ~AudioFileWrapper()
{
reset();
}
/// Move constructor
inline AudioFileWrapper(AudioFileWrapper&& rhs) noexcept
: mAudioFile{rhs.release()}
{}
// Move assignment operator
inline AudioFileWrapper& operator=(AudioFileWrapper&& rhs)
{
if(this != &rhs)
reset(rhs.release());
return *this;
}
/// Creates an @c AudioFileWrapper managing @c audioFile
inline AudioFileWrapper(AudioFileID audioFile) noexcept
: mAudioFile{audioFile}
{}
/// Returns @c true if the managed @c AudioFile is not @c nullptr
inline explicit operator bool() const noexcept
{
return mAudioFile != nullptr;
}
/// Returns the managed @c AudioFile
inline operator AudioFileID() const noexcept
{
return mAudioFile;
}
/// Returns the managed @c AudioFile
inline AudioFileID get() const noexcept
{
return mAudioFile;
}
/// Closes the managed @c AudioFile and replaces it with @c audioFile
inline void reset(AudioFileID audioFile = nullptr) noexcept
{
auto oldAudioFile = mAudioFile;
mAudioFile = audioFile;
if(oldAudioFile)
AudioFileClose(oldAudioFile);
}
/// Swaps the managed @c AudioFile of @c *this and @c other
inline void swap(AudioFileWrapper& other) noexcept
{
auto audioFile = mAudioFile;
mAudioFile = other.mAudioFile;
other.mAudioFile = audioFile;
}
/// Releases ownership of the managed @c AudioFile and returns it
inline AudioFileID release() noexcept
{
auto oldAudioFile = mAudioFile;
mAudioFile = nullptr;
return oldAudioFile;
}
private:
/// The managed @c AudioFile
AudioFileID mAudioFile = nullptr;
};
} // namespace SFB