win32/handle: add simple handle wrapper

This commit is contained in:
Danny Robson 2016-04-19 16:08:25 +10:00
parent aa780963e0
commit 3a2b33e0df
4 changed files with 116 additions and 1 deletions

View File

@ -295,6 +295,8 @@ UTIL_FILES += \
library_win32.hpp \
library_win32.cpp \
time_win32.cpp \
win32/handle.cpp \
win32/handle.hpp \
win32/registry.hpp \
win32/registry.cpp
endif

View File

@ -51,7 +51,23 @@ AC_DEFINE([__STDC_FORMAT_MACROS], [1], [use C format macros])
AC_FUNC_MMAP
AC_CHECK_FUNC([backtrace])
AM_CONDITIONAL([HAVE_EXECINFO], [test "x$ac_cv_func_backtrace" = "xtrue"])
AM_CONDITIONAL([HAVE_EXECINFO], [test "x$ac_cv_func_backtrace" == "xyes"])
AC_MSG_CHECKING([for SymFromAddr])
save_LIBS="$LIBS"
LIBS+=-ldbghelp
AC_LINK_IFELSE([AC_LANG_PROGRAM(
[
#include <windows.h>
#include <dbghelp.h>
],
[SymFromAddr(nullptr,0,nullptr,nullptr); return 0;])],[
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
LIBS="$save_LIBS"
])
AC_SEARCH_LIBS([clock_gettime], [rt], [], [
# windows doesn't have clock_gettime

61
win32/handle.cpp Normal file
View File

@ -0,0 +1,61 @@
#include "./handle.hpp"
#include "../except.hpp"
using util::win32::handle;
///////////////////////////////////////////////////////////////////////////////
handle::handle (HANDLE &&h):
m_native (h)
{ ; }
//-----------------------------------------------------------------------------
handle::handle (handle &&rhs):
m_native (rhs.m_native)
{
rhs.m_native = nullptr;
}
//-----------------------------------------------------------------------------
handle::~handle ()
{
CloseHandle (m_native);
}
///////////////////////////////////////////////////////////////////////////////
handle::operator HANDLE& (void) &
{
return native ();
}
//-----------------------------------------------------------------------------
HANDLE&
handle::native (void) &
{
return m_native;
}
//-----------------------------------------------------------------------------
const HANDLE&
handle::native (void) const &
{
return m_native;
}
///////////////////////////////////////////////////////////////////////////////
handle
handle::current_process (void)
{
auto process = GetCurrentProcess ();
if (!process)
util::win32_error::throw_code ();
return process;
}

36
win32/handle.hpp Normal file
View File

@ -0,0 +1,36 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
*/
#include <windows.h>
namespace util { namespace win32 {
struct handle {
handle (HANDLE&&);
handle (handle&&);
handle (void) = delete;
handle (const handle&) = delete;
~handle ();
operator HANDLE& (void) &;
HANDLE& native (void) &;
const HANDLE& native (void) const &;
static handle current_process (void);
private:
HANDLE m_native;
};
} }