Skip to content

Commit

Permalink
Merge branch 'kcat:c++-rewrite' into c++-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeDeeJay authored Jan 23, 2025
2 parents 36de8c0 + 1276cd1 commit bdae19e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <atomic>
#include <mutex>
#include <string>

#include <dsound.h>

Expand Down
16 changes: 6 additions & 10 deletions src/comptr.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef COMPTR_H
#define COMPTR_H

#include <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
#include <variant>
Expand All @@ -11,21 +9,19 @@ template<typename T>
struct ComPtr {
using element_type = T;

static constexpr bool RefIsNoexcept{noexcept(std::declval<T&>().AddRef())
&& noexcept(std::declval<T&>().Release())};

ComPtr() noexcept = default;
ComPtr(const ComPtr &rhs) noexcept(RefIsNoexcept) : mPtr{rhs.mPtr}
ComPtr(const ComPtr &rhs) noexcept(noexcept(mPtr->AddRef())) : mPtr{rhs.mPtr}
{ if(mPtr) mPtr->AddRef(); }
ComPtr(ComPtr&& rhs) noexcept : mPtr{rhs.mPtr} { rhs.mPtr = nullptr; }
ComPtr(std::nullptr_t) noexcept { } /* NOLINT(google-explicit-constructor) */
explicit ComPtr(T *ptr) noexcept : mPtr{ptr} { }
~ComPtr() { if(mPtr) mPtr->Release(); }

/* NOLINTNEXTLINE(bugprone-unhandled-self-assignment) Yes it is. */
ComPtr& operator=(const ComPtr &rhs) noexcept(RefIsNoexcept)
ComPtr& operator=(const ComPtr &rhs)
noexcept(noexcept(rhs.mPtr->AddRef()) && noexcept(mPtr->Release()))
{
if constexpr(RefIsNoexcept)
if constexpr(noexcept(rhs.mPtr->AddRef()) && noexcept(mPtr->Release()))
{
if(rhs.mPtr) rhs.mPtr->AddRef();
if(mPtr) mPtr->Release();
Expand All @@ -40,7 +36,7 @@ struct ComPtr {
return *this;
}
}
ComPtr& operator=(ComPtr&& rhs) noexcept(RefIsNoexcept)
ComPtr& operator=(ComPtr&& rhs) noexcept(noexcept(mPtr->Release()))
{
if(&rhs != this)
{
Expand All @@ -50,7 +46,7 @@ struct ComPtr {
return *this;
}

void reset(T *ptr=nullptr) noexcept(RefIsNoexcept)
void reset(T *ptr=nullptr) noexcept(noexcept(mPtr->Release()))
{
if(mPtr) mPtr->Release();
mPtr = ptr;
Expand Down
47 changes: 24 additions & 23 deletions src/fullduplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ HRESULT STDMETHODCALLTYPE DSFullDuplex::QueryInterface(REFIID riid, void **ppvOb
*ppvObject = mDS8Iface.as<IDirectSound8*>();
return S_OK;
}
if(riid == IID_IDirectSound)
{
mDS8Iface.AddRef();
*ppvObject = mDS8Iface.as<IDirectSound*>();
return S_OK;
}
if(riid == IID_IDirectSoundCapture)
{
mDSCIface.AddRef();
Expand Down Expand Up @@ -100,13 +106,8 @@ HRESULT STDMETHODCALLTYPE DSFullDuplex::Initialize(const GUID *captureGuid, cons
}

try {
auto hr = DSound8OAL::Create(true)->QueryInterface(IID_IDirectSound8,
ds::out_ptr(mDS8Handle));
if(FAILED(hr))
return hr;

hr = mDS8Handle->Initialize(renderGuid);
if(FAILED(hr))
mDS8Handle = DSound8OAL::Create(true);
if(const auto hr = mDS8Handle->Initialize(renderGuid); FAILED(hr))
{
mDS8Handle = nullptr;
return hr;
Expand All @@ -118,7 +119,11 @@ HRESULT STDMETHODCALLTYPE DSFullDuplex::Initialize(const GUID *captureGuid, cons
return E_FAIL;
}

mDS8Handle->SetCooperativeLevel(hwnd, level);
if(const auto hr = mDS8Handle->SetCooperativeLevel(hwnd, level); FAILED(hr))
{
mDS8Handle = nullptr;
return hr;
}

auto dsbuf = ComPtr<IDirectSoundBuffer>{};
if(auto hr = mDS8Handle->CreateSoundBuffer(dsBufferDesc, ds::out_ptr(dsbuf), nullptr);
Expand All @@ -129,13 +134,8 @@ HRESULT STDMETHODCALLTYPE DSFullDuplex::Initialize(const GUID *captureGuid, cons
}

try {
auto hr = DSCapture::Create(true)->QueryInterface(IID_IDirectSoundCapture8,
ds::out_ptr(mDSCHandle));
if(FAILED(hr))
return hr;

hr = mDSCHandle->Initialize(captureGuid);
if(FAILED(hr))
mDSCHandle = DSCapture::Create(true);
if(const auto hr = mDSCHandle->Initialize(captureGuid); FAILED(hr))
{
mDS8Handle = nullptr;
mDSCHandle = nullptr;
Expand All @@ -158,16 +158,17 @@ HRESULT STDMETHODCALLTYPE DSFullDuplex::Initialize(const GUID *captureGuid, cons
return hr;
}

if(auto hr = dsbuf->QueryInterface(IID_IDirectSoundBuffer8,
reinterpret_cast<void**>(dsBuffer8)); FAILED(hr))
auto dsbuf8 = ComPtr<IDirectSoundBuffer8>{};
if(auto hr = dsbuf->QueryInterface(IID_IDirectSoundBuffer8, ds::out_ptr(dsbuf8)); FAILED(hr))
return hr;
if(auto hr = dscbuf->QueryInterface(IID_IDirectSoundCaptureBuffer8,
reinterpret_cast<void**>(dsCaptureBuffer8)); FAILED(hr))
{
(*dsBuffer8)->Release();
*dsBuffer8 = nullptr;

auto dscbuf8 = ComPtr<IDirectSoundCaptureBuffer8>{};
if(auto hr = dscbuf->QueryInterface(IID_IDirectSoundCaptureBuffer8, ds::out_ptr(dscbuf8));
FAILED(hr))
return hr;
}

*dsBuffer8 = dsbuf8.release();
*dsCaptureBuffer8 = dscbuf8.release();

return DS_OK;
}
Expand Down
21 changes: 18 additions & 3 deletions src/fullduplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#define DSFULLDUPLEX_H

#include <atomic>
#include <bit>

#include <dsound.h>

#include "comptr.h"

class DSound8OAL;
class DSCapture;


class DSFullDuplex final : IDirectSoundFullDuplex {
DSFullDuplex();
Expand Down Expand Up @@ -72,7 +76,18 @@ class DSFullDuplex final : IDirectSoundFullDuplex {
DS8& operator=(const DS8&) = delete;

template<typename T>
T as() noexcept { return static_cast<T>(this); }
T as() noexcept
{
/* MinGW headers do not have IDirectSound8 inherit from
* IDirectSound, which MSVC apparently does. IDirectSound is a
* strict subset of IDirectSound8, so the interface is ABI
* compatible.
*/
if constexpr(std::is_same_v<T,IDirectSound*> && !std::is_base_of_v<IDirectSound,DS8>)
return std::bit_cast<T>(static_cast<IDirectSound8*>(this));
else
return static_cast<T>(this);
}
};
DS8 mDS8Iface;

Expand Down Expand Up @@ -107,8 +122,8 @@ class DSFullDuplex final : IDirectSoundFullDuplex {

std::atomic<ULONG> mTotalRef{1u}, mFdRef{1u}, mDS8Ref{0u}, mDSCRef{0u}, mUnkRef{0u};

ComPtr<IDirectSound8> mDS8Handle;
ComPtr<IDirectSoundCapture8> mDSCHandle;
ComPtr<DSound8OAL> mDS8Handle;
ComPtr<DSCapture> mDSCHandle;

public:
~DSFullDuplex();
Expand Down

0 comments on commit bdae19e

Please sign in to comment.