What wrapper class in C++ should I use for automated resource management?

前端 未结 8 1056
余生分开走
余生分开走 2020-12-16 13:56

I\'m a C++ amateur. I\'m writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper clas

相关标签:
8条回答
  • 2020-12-16 14:22

    MFC has some suitable primitives (look at CFile for example), but not the standard library.

    0 讨论(0)
  • 2020-12-16 14:22
    template <typename Traits>
    class unique_handle
    {
        using pointer = typename Traits::pointer;
    
        pointer m_value;
    
        auto close() throw() -> void
        {
            if (*this)
            {
                Traits::close(m_value);
            }
        }
    
    public:
    
        unique_handle(unique_handle const &) = delete;
        auto operator=(unique_handle const &)->unique_handle & = delete;
    
        explicit unique_handle(pointer value = Traits::invalid()) throw() :
            m_value{ value }
        {
        }
    
        unique_handle(unique_handle && other) throw() :
            m_value{ other.release() }
        {
        }
    
        auto operator=(unique_handle && other) throw() -> unique_handle &
        {
            if (this != &other)
            {
                reset(other.release());
            }
    
            return *this;
        }
    
        ~unique_handle() throw()
        {
            close();
        }
    
        explicit operator bool() const throw()
        {
            return m_value != Traits::invalid();
        }
    
        auto get() const throw() -> pointer
        {
            return m_value;
        }
    
        auto get_address_of() throw() -> pointer *
        {
            ASSERT(!*this);
            return &m_value;
        }
    
        auto release() throw() -> pointer
        {
            auto value = m_value;
            m_value = Traits::invalid();
            return value;
        }
    
        auto reset(pointer value = Traits::invalid()) throw() -> bool
        {
            if (m_value != value)
            {
                close();
                m_value = value;
            }
    
            return static_cast<bool>(*this);
        }
    
        auto swap(unique_handle<Traits> & other) throw() -> void
        {
            std::swap(m_value, other.m_value);
        }
    };
    
    template <typename Traits>
    auto swap(unique_handle<Traits> & left,
        unique_handle<Traits> & right) throw() -> void
    {
        left.swap(right);
    }
    
    template <typename Traits>
    auto operator==(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() == right.get();
    }
    
    template <typename Traits>
    auto operator!=(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() != right.get();
    }
    
    template <typename Traits>
    auto operator<(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() < right.get();
    }
    
    template <typename Traits>
    auto operator>=(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() >= right.get();
    }
    
    template <typename Traits>
    auto operator>(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() > right.get();
    }
    
    template <typename Traits>
    auto operator<=(unique_handle<Traits> const & left,
        unique_handle<Traits> const & right) throw() -> bool
    {
        return left.get() <= right.get();
    }
    
    struct null_handle_traits
    {
        using pointer = HANDLE;
    
        static auto invalid() throw() -> pointer
        {
            return nullptr;
        }
    
        static auto close(pointer value) throw() -> void
        {
            VERIFY(CloseHandle(value));
        }
    };
    
    struct invalid_handle_traits
    {
        using pointer = HANDLE;
    
        static auto invalid() throw() -> pointer
        {
            return INVALID_HANDLE_VALUE;
        }
    
        static auto close(pointer value) throw() -> void
        {
            VERIFY(CloseHandle(value));
        }
    };
    
    using null_handle = unique_handle<null_handle_traits>;
    using invalid_handle = unique_handle<invalid_handle_traits>;
    
    0 讨论(0)
  • 2020-12-16 14:28

    I don't think there is anything in the standard library, and I also doubt that shared pointers (as in boost) can be used (since those would expect pointer to HANDLE, not HANDLE).

    It shouldn't be hard to write one yourself, following the scope guard idiom (and making use of templates/function pointers etc if you so choose).

    0 讨论(0)
  • 2020-12-16 14:30

    Write your own. It's only a few lines of code. It's just such a simple task that it's not worth it to provide a generic reusable version.

    struct FileWrapper {
      FileWrapper(...) : h(CreateFile(...)) {}
      ~FileWrapper() { CloseHandle(h); }
    
    private:
      HANDLE h;
    };
    

    Think about what a generic version would have to do: It'd have to be parametrizable so you can specify any pair of functions, and any number of arguments to them. Just instantiating such an object would likely take as many lines of code as the above class definition.

    Of course, C++0x might tip the balance somewhat with the addition of lambda expressions. Two lambda expressions could easily be passed to a generic wrapper class, so once C++0x supports comes around, we might see such a generic RAII class added to Boost or something.

    But at the moment, it's easier to just roll your own whenever you need it.

    As for adding reference counting, I'd advise against it. Reference counting is expensive (suddenly your handle has to be dynamically allocated, and reference counters have to be maintained on every assignment), and very hard to get right. It's an area just bursting with subtle race conditions in a threaded environment.

    If you do need reference counting, just do something like boost::shared_ptr<FileWrapper>: wrap your custom ad-hoc RAII classes in a shared_ptr.

    0 讨论(0)
  • 2020-12-16 14:32

    Here's one based off the EnsureCleanup code from 'Windows via C/C++': http://www.codeproject.com/KB/cpp/template2003.aspx

    0 讨论(0)
  • 2020-12-16 14:38

    Visual C++ 2008 supports TR1 through the Feature Pack, and TR1 includes shared_ptr. I would use this -- it's a very powerful smart pointer class and can be generalized to do the type of resource management you are asking for.

    TR1 is effectively an extension to the Standard. I believe it's still officially "pre-standard", but effectively you can consider it locked down.

    0 讨论(0)
提交回复
热议问题