win32/handle: merge various handle wrapper classes

This commit is contained in:
Danny Robson 2016-04-27 17:01:45 +10:00
parent eb1b042e06
commit cc001c4788
5 changed files with 25 additions and 50 deletions

16
io.hpp
View File

@ -53,22 +53,6 @@ namespace util {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
#ifdef PLATFORM_WIN32
struct handle : util::nocopy {
public:
explicit handle (HANDLE);
explicit handle ();
~handle ();
void reset (HANDLE);
operator HANDLE (void) const;
HANDLE fd;
};
#endif
/// Reads an entire file into memory. /// Reads an entire file into memory.
std::vector<char> slurp (const boost::filesystem::path&); std::vector<char> slurp (const boost::filesystem::path&);
std::vector<char> slurp (FILE *); std::vector<char> slurp (FILE *);

View File

@ -127,32 +127,3 @@ mapped_file::cend (void) const
{ {
return data () + size (); return data () + size ();
} }
//-----------------------------------------------------------------------------
util::handle::handle():
fd (INVALID_HANDLE_VALUE)
{ ; }
util::handle::~handle ()
{
reset (INVALID_HANDLE_VALUE);
}
void
util::handle::reset (HANDLE _handle)
{
if (fd != INVALID_HANDLE_VALUE)
if (!CloseHandle (fd))
win32_error::throw_code ();
fd = _handle;
}
util::handle::operator HANDLE (void) const
{
return fd;
}

View File

@ -17,7 +17,7 @@
#ifndef __UTIL_IO_WIN32_HPP #ifndef __UTIL_IO_WIN32_HPP
#define __UTIL_IO_WIN32_HPP #define __UTIL_IO_WIN32_HPP
#include "io.hpp" #include "win32/handle.hpp"
#include <cstdint> #include <cstdint>
#include <boost/filesystem/path.hpp> #include <boost/filesystem/path.hpp>
@ -55,8 +55,8 @@ namespace util {
const uint8_t* cend (void) const; const uint8_t* cend (void) const;
private: private:
handle m_file; ::util::win32::handle m_file;
handle m_mapping; ::util::win32::handle m_mapping;
std::unique_ptr<uint8_t,BOOL(*)(LPCVOID)> m_data; std::unique_ptr<uint8_t,BOOL(*)(LPCVOID)> m_data;
size_t m_size; size_t m_size;

View File

@ -6,6 +6,12 @@ using util::win32::handle;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
handle::handle ():
m_native (INVALID_HANDLE_VALUE)
{ ; }
//-----------------------------------------------------------------------------
handle::handle (HANDLE &&h): handle::handle (HANDLE &&h):
m_native (h) m_native (h)
{ ; } { ; }
@ -22,7 +28,8 @@ handle::handle (handle &&rhs):
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
handle::~handle () handle::~handle ()
{ {
CloseHandle (m_native); if (m_native != INVALID_HANDLE_VALUE)
CloseHandle (m_native);
} }
@ -49,6 +56,16 @@ handle::native (void) const &
} }
///////////////////////////////////////////////////////////////////////////////
void
handle::reset (HANDLE rhs)
{
if (m_native != INVALID_HANDLE_VALUE)
CloseHandle (m_native);
m_native = rhs;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
handle handle
handle::current_process (void) handle::current_process (void)

View File

@ -18,9 +18,9 @@
namespace util { namespace win32 { namespace util { namespace win32 {
struct handle { struct handle {
handle ();
handle (HANDLE&&); handle (HANDLE&&);
handle (handle&&); handle (handle&&);
handle (void) = delete;
handle (const handle&) = delete; handle (const handle&) = delete;
~handle (); ~handle ();
@ -28,6 +28,9 @@ namespace util { namespace win32 {
HANDLE& native (void) &; HANDLE& native (void) &;
const HANDLE& native (void) const &; const HANDLE& native (void) const &;
void reset (HANDLE);
void reset (handle&&);
static handle current_process (void); static handle current_process (void);
private: private: