Skip to content

Commit

Permalink
Merge 1bf1b63 into 1839122
Browse files Browse the repository at this point in the history
  • Loading branch information
sanomari authored Feb 7, 2021
2 parents 1839122 + 1bf1b63 commit 802091b
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sakura/sakura.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@
<ClInclude Include="..\sakura_core\apiwrap\CommonControl.h" />
<ClInclude Include="..\sakura_core\apiwrap\StdApi.h" />
<ClInclude Include="..\sakura_core\apiwrap\StdControl.h" />
<ClInclude Include="..\sakura_core\basis\TComImpl.hpp" />
<ClInclude Include="..\sakura_core\basis\CErrorInfo.h" />
<ClInclude Include="..\sakura_core\basis\CLaxInteger.h" />
<ClInclude Include="..\sakura_core\basis\CMyPoint.h" />
<ClInclude Include="..\sakura_core\basis\CMyRect.h" />
Expand Down Expand Up @@ -607,6 +609,7 @@
<ItemGroup>
<ClCompile Include="..\sakura_core\apiwrap\StdApi.cpp" />
<ClCompile Include="..\sakura_core\apiwrap\StdControl.cpp" />
<ClCompile Include="..\sakura_core\basis\CErrorInfo.cpp" />
<ClCompile Include="..\sakura_core\basis\CLaxInteger.cpp" />
<ClCompile Include="..\sakura_core\basis\CMyPoint.cpp" />
<ClCompile Include="..\sakura_core\basis\CMyRect.cpp" />
Expand Down
9 changes: 9 additions & 0 deletions sakura/sakura.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,12 @@
<ClInclude Include="..\sakura_core\GrepInfo.h">
<Filter>Cpp Source Files</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\basis\TComImpl.hpp">
<Filter>Cpp Source Files\basis</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\basis\CErrorInfo.h">
<Filter>Cpp Source Files\basis</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\resource\auto_scroll_center.cur">
Expand Down Expand Up @@ -2273,6 +2279,9 @@
<ClCompile Include="..\sakura_core\GrepInfo.cpp">
<Filter>Cpp Source Files</Filter>
</ClCompile>
<ClCompile Include="..\sakura_core\basis\CErrorInfo.cpp">
<Filter>Cpp Source Files\basis</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="..\resource\auto_scroll_center.bmp">
Expand Down
90 changes: 90 additions & 0 deletions sakura_core/basis/CErrorInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include "basis/CErrorInfo.h"

/*!
* コンストラクタ
*/
CErrorInfo::CErrorInfo(
const std::wstring_view& source, //!< [in] ソース
const std::wstring_view& description //!< [in] 説明
)
: bstrSource_(source.data())
, bstrDescription_(description.data())
{
}

/*!
* GUIDを取得する
*/
IFACEMETHODIMP CErrorInfo::GetGUID(GUID *pGUID)
{
if (!pGUID) return E_POINTER;
*pGUID = GUID_NULL;
return S_OK;
}

/*!
* ソース情報を取得する
*/
IFACEMETHODIMP CErrorInfo::GetSource(BSTR *pBstrSource)
{
if (!pBstrSource) return E_POINTER;
*pBstrSource = bstrSource_.copy();
return S_OK;
}

/*!
* 説明を取得する
*/
IFACEMETHODIMP CErrorInfo::GetDescription(BSTR *pBstrDescription)
{
if (!pBstrDescription) return E_POINTER;
*pBstrDescription = bstrDescription_.copy();
return S_OK;
}

/*!
* ヘルプファイルのパスを取得する
*/
IFACEMETHODIMP CErrorInfo::GetHelpFile(BSTR *pBstrHelpFile)
{
if (!pBstrHelpFile) return E_POINTER;
_bstr_t bstrHelpFile_;
*pBstrHelpFile = bstrHelpFile_.copy();
return S_OK;
}

/*!
* ヘルプコンテキストを取得する
*/
IFACEMETHODIMP CErrorInfo::GetHelpContext(DWORD *pdwHelpContext)
{
if (!pdwHelpContext) return E_POINTER;
constexpr DWORD dwHelpContext_ = 0;
*pdwHelpContext = dwHelpContext_;
return S_OK;
}
64 changes: 64 additions & 0 deletions sakura_core/basis/CErrorInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once

#include <tchar.h>
#include <Windows.h>

#include <comdef.h>
#include <OAIdl.h>

#include <string_view>

#include "basis/TComImpl.hpp"

/*!
* @brief COMエラー情報クラス
*
* Windowsのエラー情報を提供するために使用するクラスです。
* _com_raise_error関数にエラー情報を渡すことにより、日本語メッセージを含む例外を投げることができます。
*
* @see https://docs.microsoft.com/en-us/windows/win32/api/oaidl/nn-oaidl-ierrorinfo
* @see https://docs.microsoft.com/en-us/cpp/cpp/com-raise-error
*/
class CErrorInfo : public TComImpl<IErrorInfo> {
_bstr_t bstrSource_;
_bstr_t bstrDescription_;

public:
CErrorInfo(
const std::wstring_view& source, //!< [in] ソース
const std::wstring_view& description //!< [in] 説明
);

IFACEMETHODIMP GetGUID(GUID *pGUID) override;
IFACEMETHODIMP GetSource(BSTR *pBstrSource) override;
IFACEMETHODIMP GetDescription(BSTR *pBstrDescription) override;
IFACEMETHODIMP GetHelpFile(BSTR *pBstrHelpFile) override;
IFACEMETHODIMP GetHelpContext(DWORD *pdwHelpContext) override;
};

//! メッセージからエラー情報を生成する
#define MakeMsgError(msg) new CErrorInfo(_CRT_WIDE(__FILE__) L"(" _CRT_WIDE(_CRT_STRINGIZE(__LINE__)) L")", (msg))
91 changes: 91 additions & 0 deletions sakura_core/basis/TComImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once

#include <tchar.h>
#include <Windows.h>

#include <comdef.h>
#include <comutil.h>
#include <OAIdl.h>

#include <memory>
#include <type_traits>

/*!
* TComImpl - COMオブジェクトの実装クラステンプレート
* 使用上の注意:
* 1. 生成はnewで行い、deleteはしないでください。
* 自動変数で生成するとヒープエラーが出ます。
* 2. 生成したらAddRef()し、不要になったらRelease()してください。
*/
template<class TargetInterface, class DeleteorType = std::default_delete<TargetInterface>, std::enable_if_t<std::is_base_of_v<IUnknown, TargetInterface>, std::nullptr_t> = nullptr>
class TComImpl : public TargetInterface
{
LONG nRefCount_ = 0;

using Me = TComImpl<TargetInterface>;

public:
TComImpl() = default;
TComImpl(const Me&) = delete;
Me& operator = (const Me&) = delete;
TComImpl(Me&&) = delete;
Me& operator = (Me&&) = delete;

IFACEMETHODIMP QueryInterface(REFIID iid, void ** ppvObject) override
{
if (ppvObject == nullptr) {
return E_POINTER;
}

if (IsEqualIID(iid, __uuidof(TargetInterface)) ||
IsEqualIID(iid, IID_IUnknown))
{
*ppvObject = this;
AddRef();
return S_OK;
}

return E_NOINTERFACE;
}

IFACEMETHODIMP_(ULONG) AddRef() override
{
return ::InterlockedIncrement(&nRefCount_);
}

IFACEMETHODIMP_(ULONG) Release() override
{
const LONG nRefCount = ::InterlockedDecrement(&nRefCount_);

if (nRefCount == 0) {
const DeleteorType deletor;
deletor(this);
}

return nRefCount;
}
};
35 changes: 35 additions & 0 deletions sakura_core/basis/_com_raise_error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include <comdef.h>
#include <OAIdl.h>

/*!
MinGW向け_com_raise_error実装
*/
void _com_raise_error(HRESULT hr, IErrorInfo* pErrorInfo)
{
throw _com_error(hr, pErrorInfo, true);
}
Loading

0 comments on commit 802091b

Please sign in to comment.