Skip to content

Commit

Permalink
use lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
nu774 committed May 5, 2017
1 parent 8ce8120 commit 8babb9e
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 38 deletions.
13 changes: 7 additions & 6 deletions AudioCodecX.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ class AudioCodecX {
}
void attach(AudioCodec codec, bool takeOwn)
{
typedef OSStatus (*disposer_t)(AudioCodec);
struct F {
static OSStatus dispose(AudioCodec) { return 0; }
auto dispose = [](AudioCodec x) {
auto y = reinterpret_cast<AudioComponentInstance>(x);
AudioComponentInstanceDispose(y);
};
disposer_t acdispose =
reinterpret_cast<disposer_t>(AudioComponentInstanceDispose);
m_codec.reset(codec, takeOwn ? acdispose : F::dispose);
if (takeOwn)
m_codec.reset(codec, dispose);
else
m_codec.reset(codec, [](AudioCodec) {});
}
operator AudioCodec() { return m_codec.get(); }

Expand Down
8 changes: 4 additions & 4 deletions AudioConverterX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ AudioConverterX::AudioConverterX(const AudioStreamBasicDescription &iasbd,
}
void AudioConverterX::attach(AudioConverterRef converter, bool takeOwn)
{
struct F {
static OSStatus dispose(AudioConverterRef) { return 0; }
};
m_converter.reset(converter, takeOwn ? AudioConverterDispose : F::dispose);
if (takeOwn)
m_converter.reset(converter, AudioConverterDispose);
else
m_converter.reset(converter, [](AudioConverterRef){});
}

UInt32 AudioConverterX::getSampleRateConverterComplexity()
Expand Down
8 changes: 4 additions & 4 deletions AudioFileX.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ class AudioFileX {
}
void attach(AudioFileID file, bool takeOwn)
{
struct F {
static OSStatus dispose(AudioFileID af) { return 0; }
};
m_file.reset(file, takeOwn ? AudioFileClose : F::dispose);
if (takeOwn)
m_file.reset(file, AudioFileClose);
else
m_file.reset(file, [](AudioFileID){});
}
operator AudioFileID() { return m_file.get(); }
void close() { m_file.reset(); }
Expand Down
8 changes: 4 additions & 4 deletions ExtAudioFileX.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class ExtAudioFileX {
}
void attach(ExtAudioFileRef file, bool takeOwn)
{
struct F {
static OSStatus dispose(ExtAudioFileRef) { return 0; }
};
m_file.reset(file, takeOwn ? ExtAudioFileDispose : F::dispose);
if (takeOwn)
m_file.reset(file, ExtAudioFileDispose);
else
m_file.reset(file, [](ExtAudioFileRef){});
}
operator ExtAudioFileRef() { return m_file.get(); }

Expand Down
3 changes: 1 addition & 2 deletions dl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ class DL {
DL() {}
DL(HMODULE handle, bool own=true)
{
struct noop { static void call(HMODULE x) {} };
if (own)
m_module.reset(handle, FreeLibrary);
else
m_module.reset(handle, noop::call);
m_module.reset(handle, [](HMODULE){});
}
bool load(const std::wstring &path)
{
Expand Down
3 changes: 1 addition & 2 deletions logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class Log {
bool is_enabled() { return m_streams.size() != 0; }
void enable_stderr()
{
struct Lambda { static void call(FILE*) {} };
if (m_stderr_type != FILE_TYPE_UNKNOWN)
m_streams.push_back(std::shared_ptr<FILE>(stderr, Lambda::call));
m_streams.push_back(std::shared_ptr<FILE>(stderr, [](FILE*){}));
}
void enable_file(const wchar_t *filename)
{
Expand Down
11 changes: 4 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1599,13 +1599,10 @@ int wmain1(int argc, wchar_t **argv)
const wchar_t *ifilename = 0;

if (opts.sort_args) {
struct Sorter {
static bool cmp(const wchar_t *a, const wchar_t *b)
{
return std::wcscmp(a, b) < 0;
}
};
std::sort(&argv[0], &argv[argc], Sorter::cmp);
std::sort(&argv[0], &argv[argc],
[](const wchar_t *a, const wchar_t *b) {
return std::wcscmp(a, b) < 0;
});
}
for (int i = 0; i < argc; ++i) {
ifilename = argv[i];
Expand Down
9 changes: 3 additions & 6 deletions textfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ void throwIfError(HRESULT expr, const char *msg)

std::wstring load_text_file(const std::wstring &path, uint32_t codepage)
{
struct F {
static void release(IUnknown *x) { x->Release(); }
};

IStream *stream;
HRESULT hr = SHCreateStreamOnFileW(path.c_str(),
STGM_READ | STGM_SHARE_DENY_WRITE,
&stream);
if (FAILED(hr)) win32::throw_error(path, hr);
std::shared_ptr<IStream> streamPtr(stream, F::release);
auto release_func = [](IUnknown *x) { x->Release(); };
std::shared_ptr<IStream> streamPtr(stream, release_func);

LARGE_INTEGER li = {{ 0 }};
ULARGE_INTEGER ui;
Expand All @@ -43,7 +40,7 @@ std::wstring load_text_file(const std::wstring &path, uint32_t codepage)
IMultiLanguage2 *mlang;
HR(CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_INPROC_SERVER,
IID_IMultiLanguage2, (void**)(&mlang)));
std::shared_ptr<IMultiLanguage2> mlangPtr(mlang, F::release);
std::shared_ptr<IMultiLanguage2> mlangPtr(mlang, release_func);

if (!codepage) {
DetectEncodingInfo encoding[5];
Expand Down
6 changes: 3 additions & 3 deletions win32util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ namespace win32 {
inline std::shared_ptr<FILE> fopen(const std::wstring &path,
const wchar_t *mode)
{
struct noop { static void call(FILE*) {} };
auto noop_close = [](FILE *){};
if (path != L"-")
return std::shared_ptr<FILE>(wfopenx(path.c_str(), mode),
std::fclose);
else if (std::wcschr(mode, L'r'))
return std::shared_ptr<FILE>(stdin, noop::call);
return std::shared_ptr<FILE>(stdin, noop_close);
else
return std::shared_ptr<FILE>(stdout, noop::call);
return std::shared_ptr<FILE>(stdout, noop_close);
}

inline bool is_seekable(int fd)
Expand Down

0 comments on commit 8babb9e

Please sign in to comment.