Skip to content

Commit

Permalink
Move FreeThreadedHen to separate files
Browse files Browse the repository at this point in the history
The AtlHen files became a bit big.
  • Loading branch information
100029962 committed Apr 11, 2023
1 parent 03713fe commit 71ceef9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 59 deletions.
28 changes: 0 additions & 28 deletions AtlServer/AtlHen.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include "pch.h"
#include "AtlHen.h"
#include <comdef.h>
#include <cassert>
#include <future>
#include <ComUtility/Utility.h>
#include <winrt/base.h>

HRESULT AtlHen::Cluck()
{
Expand All @@ -18,30 +14,6 @@ HRESULT AtlHen::CluckAsync(IAsyncCluckObserver* cluckObserver)
return cluckObserver->OnCluck();
}

HRESULT FreeThreadedHen::FinalConstruct()
{
return CoCreateFreeThreadedMarshaler(GetControllingUnknown(), &m_marshaler);
}

HRESULT FreeThreadedHen::Cluck()
{
assert(m_myThreadId == GetCurrentThreadId());
return S_OK;
}

HRESULT FreeThreadedHen::CluckAsync(IAsyncCluckObserver* cluckObserver)
{
winrt::com_ptr<IAsyncCluckObserver> observer;
observer.copy_from(cluckObserver);

auto result = std::async(std::launch::async, [observer] {
ComRuntime comRuntime{ Apartment::MultiThreaded };
return observer->OnCluck();
});

return result.get();
}

HRESULT AtlCluckObserver::OnCluck()
{
assert(m_myThreadId == GetCurrentThreadId());
Expand Down
31 changes: 0 additions & 31 deletions AtlServer/AtlHen.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,6 @@ class ATL_NO_VTABLE AtlHen :

OBJECT_ENTRY_AUTO(CLSID_AtlHen, AtlHen)



class ATL_NO_VTABLE FreeThreadedHen :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<FreeThreadedHen, &CLSID_FreeThreadedHen>,
public IHen
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_HEN)
DECLARE_PROTECT_FINAL_CONSTRUCT()
DECLARE_GET_CONTROLLING_UNKNOWN()

BEGIN_COM_MAP(FreeThreadedHen)
COM_INTERFACE_ENTRY(IHen)
COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_marshaler)
END_COM_MAP()

HRESULT FinalConstruct();

HRESULT Cluck() override;
HRESULT CluckAsync(IAsyncCluckObserver* cluckObserver) override;

private:
CComPtr<IUnknown> m_marshaler;
unsigned long m_myThreadId = GetCurrentThreadId();
};

OBJECT_ENTRY_AUTO(CLSID_FreeThreadedHen, FreeThreadedHen)



class ATL_NO_VTABLE AtlCluckObserver : public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<AtlCluckObserver, &CLSID_AtlCluckObserver>,
public IAsyncCluckObserver
Expand Down
2 changes: 2 additions & 0 deletions AtlServer/AtlServer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ItemGroup>
<ClCompile Include="AtlHen.cpp" />
<ClCompile Include="AtlServer.cpp" />
<ClCompile Include="FreeThreadedHen.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
Expand All @@ -35,6 +36,7 @@
<ItemGroup>
<ClInclude Include="AtlHen.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="FreeThreadedHen.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="targetver.h" />
Expand Down
2 changes: 2 additions & 0 deletions AtlServer/AtlServer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<ClCompile Include="AtlHen.cpp" />
<ClCompile Include="AtlServer.cpp" />
<ClCompile Include="pch.cpp" />
<ClCompile Include="FreeThreadedHen.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AtlHen.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="FreeThreadedHen.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="AtlServer.rc" />
Expand Down
30 changes: 30 additions & 0 deletions AtlServer/FreeThreadedHen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "pch.h"
#include "FreeThreadedHen.h"
#include <cassert>
#include <future>
#include <ComUtility/Utility.h>
#include <winrt/base.h>

HRESULT FreeThreadedHen::FinalConstruct()
{
return CoCreateFreeThreadedMarshaler(GetControllingUnknown(), &m_marshaler);
}

HRESULT FreeThreadedHen::Cluck()
{
assert(m_myThreadId == GetCurrentThreadId());
return S_OK;
}

HRESULT FreeThreadedHen::CluckAsync(IAsyncCluckObserver* cluckObserver)
{
winrt::com_ptr<IAsyncCluckObserver> observer;
observer.copy_from(cluckObserver);

auto result = std::async(std::launch::async, [observer] {
ComRuntime comRuntime{ Apartment::MultiThreaded };
return observer->OnCluck();
});

return result.get();
}
36 changes: 36 additions & 0 deletions AtlServer/FreeThreadedHen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include "resource.h" // main symbols
#include <AtlServer/AtlServer.h>

using namespace ATL;

/** A hen that can be called from any thread by aggregating
* the free threaded marshaler
*/
class ATL_NO_VTABLE FreeThreadedHen :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<FreeThreadedHen, &CLSID_FreeThreadedHen>,
public IHen
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_HEN)
DECLARE_PROTECT_FINAL_CONSTRUCT()
DECLARE_GET_CONTROLLING_UNKNOWN()

BEGIN_COM_MAP(FreeThreadedHen)
COM_INTERFACE_ENTRY(IHen)
COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_marshaler)
END_COM_MAP()

HRESULT FinalConstruct();

HRESULT Cluck() override;
HRESULT CluckAsync(IAsyncCluckObserver* cluckObserver) override;

private:
CComPtr<IUnknown> m_marshaler;
unsigned long m_myThreadId = GetCurrentThreadId();
};

OBJECT_ENTRY_AUTO(CLSID_FreeThreadedHen, FreeThreadedHen)

0 comments on commit 71ceef9

Please sign in to comment.