File: auto_handle.hpp

package info (click to toggle)
supercollider 1%3A3.11.2%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 71,152 kB
  • sloc: cpp: 387,846; lisp: 80,328; ansic: 76,515; sh: 22,779; python: 7,932; makefile: 2,333; perl: 1,123; javascript: 915; java: 677; xml: 582; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (123 lines) | stat: -rwxr-xr-x 3,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          http://www.boost.org/LICENSE_1_0.txt)
 *
 * (C) Copyright 2005-2007 Anthony Williams
 * (C) Copyright 2007 David Deakins
 * (C) Copyright 2013 Andrey Semashev
 */
/*!
 * \file   auto_handle.hpp
 *
 * \brief  This header is the Boost.Sync library implementation, see the library documentation
 *         at http://www.boost.org/doc/libs/release/libs/sync/doc/html/index.html.
 *
 * This header contains a scope guard to automatically release Windows handles.
 */

#ifndef BOOST_SYNC_DETAIL_AUTO_HANDLE_HPP_INCLUDED_
#define BOOST_SYNC_DETAIL_AUTO_HANDLE_HPP_INCLUDED_

#include <cstddef>
#include <boost/assert.hpp>
#include <boost/detail/winapi/handles.hpp>
#include <boost/detail/winapi/get_last_error.hpp>
#include <boost/detail/winapi/get_current_process.hpp>
#include <boost/sync/detail/config.hpp>
#include <boost/sync/detail/throw_exception.hpp>
#include <boost/sync/exceptions/resource_error.hpp>
#include <boost/sync/detail/header.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

namespace boost {

namespace sync {

namespace detail {

namespace windows {

class auto_handle
{
private:
    boost::detail::winapi::HANDLE_ m_handle;

public:
    BOOST_CONSTEXPR explicit auto_handle(boost::detail::winapi::HANDLE_ h = NULL) BOOST_NOEXCEPT : m_handle(h)
    {
    }

    ~auto_handle()
    {
        reset();
    }

    void reset(boost::detail::winapi::HANDLE_ h = NULL) BOOST_NOEXCEPT
    {
        if (m_handle && m_handle != boost::detail::winapi::invalid_handle_value)
        {
            BOOST_VERIFY(boost::detail::winapi::CloseHandle(m_handle));
        }
        m_handle = h;
    }

    boost::detail::winapi::HANDLE_ get() const BOOST_NOEXCEPT
    {
        return m_handle;
    }

    operator boost::detail::winapi::HANDLE_ () const BOOST_NOEXCEPT
    {
        return m_handle;
    }

    bool operator! () const BOOST_NOEXCEPT
    {
        return !m_handle;
    }

    boost::detail::winapi::HANDLE_ release() BOOST_NOEXCEPT
    {
        const boost::detail::winapi::HANDLE_ h = m_handle;
        m_handle = NULL;
        return h;
    }

    boost::detail::winapi::HANDLE_ duplicate() const
    {
        const boost::detail::winapi::HANDLE_ current_process = boost::detail::winapi::GetCurrentProcess();
        boost::detail::winapi::HANDLE_ new_handle = NULL;
        if (boost::detail::winapi::DuplicateHandle(current_process, m_handle, current_process, &new_handle, 0, false, boost::detail::winapi::duplicate_same_access) == 0)
        {
            const boost::detail::winapi::DWORD_ err = boost::detail::winapi::GetLastError();
            BOOST_SYNC_DETAIL_THROW(resource_error, (err)("boost::sync: failed to duplicate a handle"));
        }
        return new_handle;
    }

    void swap(auto_handle& that)
    {
        const boost::detail::winapi::HANDLE_ h = m_handle;
        m_handle = that.m_handle;
        that.m_handle = h;
    }

    BOOST_DELETED_FUNCTION(auto_handle(auto_handle const&))
    BOOST_DELETED_FUNCTION(auto_handle& operator= (auto_handle const&))
};

} // namespace windows

} // namespace detail

} // namespace sync

} // namespace boost

#include <boost/sync/detail/footer.hpp>

#endif // BOOST_SYNC_DETAIL_AUTO_HANDLE_HPP_INCLUDED_