Created
September 28, 2014 16:20
-
-
Save egtra/b5ecad667c9a1843e823 to your computer and use it in GitHub Desktop.
Global Interface Tableのラッパー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
git_ptr.hpp: Copyright 2014 Egtra | |
Boost Software License - Version 1.0 - August 17th, 2003 | |
Permission is hereby granted, free of charge, to any person or organization | |
obtaining a copy of the software and accompanying documentation covered by | |
this license (the "Software") to use, reproduce, display, distribute, | |
execute, and transmit the Software, and to prepare derivative works of the | |
Software, and to permit third-parties to whom the Software is furnished to | |
do so, all subject to the following: | |
The copyright notices in the Software and this entire statement, including | |
the above license grant, this restriction and the following disclaimer, | |
must be included in all copies of the Software, in whole or in part, and | |
all derivative works of the Software, unless such copies or derivative | |
works are solely in the form of machine-executable object code generated by | |
a source language processor. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <atomic> | |
#include <system_error> | |
#include <utility> // swap | |
#include <comdef.h> | |
class git_ptr_base | |
{ | |
protected: | |
static void throw_if_failed(HRESULT hr) | |
{ | |
if (FAILED(hr)) | |
throw std::system_error(static_cast<int>(hr), std::system_category()); | |
} | |
static IGlobalInterfaceTablePtr get_git() | |
{ | |
IGlobalInterfaceTablePtr ret; | |
throw_if_failed(CoCreateInstance(CLSID_StdGlobalInterfaceTable, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&ret))); | |
return ret; | |
} | |
template<typename T> | |
static DWORD register_git(T* p) | |
{ | |
if (p == nullptr) | |
return 0; | |
DWORD ret; | |
throw_if_failed(get_git()->RegisterInterfaceInGlobal(p, __uuidof(T), &ret)); | |
return ret; | |
} | |
static void revoke_git(DWORD cookie) | |
{ | |
if (cookie == 0) | |
return; | |
(void)get_git()->RevokeInterfaceFromGlobal(cookie); | |
} | |
template<typename T> | |
static T* get_from_git(DWORD cookie) | |
{ | |
if (cookie == 0) | |
return nullptr; | |
T* ret; | |
throw_if_failed(get_git()->GetInterfaceFromGlobal(cookie, IID_PPV_ARGS(&ret))); | |
return ret; | |
} | |
template<typename Interface> | |
struct com_smart_ptr | |
{ | |
_COM_SMARTPTR_TYPEDEF(Interface, __uuidof(Interface)); | |
typedef InterfacePtr type; | |
}; | |
}; | |
template<typename T> | |
class git_ptr : protected git_ptr_base | |
{ | |
public: | |
git_ptr() throw() : m_cookie() {} | |
git_ptr(_In_opt_ T* p) : m_cookie(register_git(p)) {} | |
explicit git_ptr(DWORD cookie) throw() : m_cookie(cookie) {} | |
#if _MSC_VER >= 1600 | |
git_ptr(git_ptr&& y) throw() : m_cookie(y.release()) {} | |
git_ptr& operator=(git_ptr&& y) throw() | |
{ | |
reset(y.release()); | |
return *this; | |
} | |
#endif | |
git_ptr& operator=(_In_opt_ T* p) | |
{ | |
reset(p); | |
return *this; | |
} | |
#if _MSC_VER >= 1800 | |
git_ptr(const git_ptr&) = delete; | |
git_ptr& operator=(const git_ptr&) = delete; | |
#endif | |
~git_ptr() { revoke_git(m_cookie); } | |
void reset() throw() { revoke_git(release()); } | |
void reset(_In_opt_ T* p) { reset(register_git(p)); } | |
void reset(DWORD cookie) { git_ptr(cookie).swap(*this); } | |
void swap(git_ptr& y) { std::swap(m_cookie, y.m_cookie); } | |
DWORD release() throw() | |
{ | |
DWORD ret = m_cookie; | |
m_cookie = 0; | |
return ret; | |
} | |
#if _MSC_VER >= 1800 | |
explicit operator bool() const throw() { return m_cookie != 0; } | |
#endif | |
typename com_smart_ptr<T>::type get() const throw() { return get_from_git<T>(m_cookie); } | |
DWORD get_cookie() const throw() { return m_cookie; } | |
private: | |
DWORD m_cookie; | |
}; | |
template<typename T> | |
class atomic_git_ptr : protected git_ptr_base | |
{ | |
public: | |
atomic_git_ptr() throw() : m_cookie() {} | |
atomic_git_ptr(_In_opt_ T* p) : m_cookie(register_git(p)) {} | |
explicit atomic_git_ptr(DWORD cookie) throw() : m_cookie(cookie) {} | |
#if _MSC_VER >= 1600 | |
atomic_git_ptr(git_ptr<T>&& y) throw() : m_cookie(y.release()) {} | |
atomic_git_ptr& operator=(git_ptr<T>&& y) throw() | |
{ | |
reset(y.release()); | |
return *this; | |
} | |
#endif | |
atomic_git_ptr& operator=(_In_opt_ T* p) | |
{ | |
reset(p); | |
return *this; | |
} | |
#if _MSC_VER >= 1800 | |
atomic_git_ptr(atomic_git_ptr&&) = delete; | |
atomic_git_ptr(const atomic_git_ptr&) = delete; | |
atomic_git_ptr& operator=(atomic_git_ptr&&) = delete; | |
atomic_git_ptr& operator=(const atomic_git_ptr&) = delete; | |
#endif | |
~atomic_git_ptr() | |
{ | |
//revoke_git(m_cookie.non_atomic_load()); | |
revoke_git(m_cookie.load(std::memory_order_relaxed)); | |
} | |
void reset() throw() { revoke_git(release()); } | |
void reset(_In_opt_ T* p) { reset(register_git(p)); } | |
void reset(DWORD cookie) { revoke_git(m_cookie.exchange(cookie)); } | |
#if _MSC_VER >= 1600 | |
git_ptr<T> exchange(git_ptr<T>&& desired) throw() | |
{ | |
return git_ptr<T>(m_cookie.exchange(desired.release())); | |
} | |
#endif | |
DWORD release() throw() { return m_cookie.exchange(0); } | |
#if _MSC_VER >= 1800 | |
explicit operator bool() const throw() { return m_cookie != 0; } | |
#endif | |
typename com_smart_ptr<T>::type get() const throw() { return get_from_git<T>(m_cookie); } | |
DWORD get_cookie() const throw() { return m_cookie; } | |
bool is_lock_free() const throw() { return m_cookie.is_lock_free(); } | |
private: | |
std::atomic<DWORD> m_cookie; | |
}; | |
template<typename T> | |
git_ptr<T> make_git(_In_ T* p) | |
{ | |
return git_ptr<T>(p); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment