From 3a2b33e0df25238263ae38fb60d5a2c7e534961e Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 19 Apr 2016 16:08:25 +1000 Subject: [PATCH] win32/handle: add simple handle wrapper --- Makefile.am | 2 ++ configure.ac | 18 +++++++++++++- win32/handle.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ win32/handle.hpp | 36 ++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 win32/handle.cpp create mode 100644 win32/handle.hpp diff --git a/Makefile.am b/Makefile.am index f45633b8..ec1bc824 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index 003f2b0c..fc9683e6 100644 --- a/configure.ac +++ b/configure.ac @@ -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 +#include +], +[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 diff --git a/win32/handle.cpp b/win32/handle.cpp new file mode 100644 index 00000000..4cccb334 --- /dev/null +++ b/win32/handle.cpp @@ -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; +} diff --git a/win32/handle.hpp b/win32/handle.hpp new file mode 100644 index 00000000..ce6b0d76 --- /dev/null +++ b/win32/handle.hpp @@ -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 + */ + +#include + +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; + }; +} }