RAII的非线性指针? [英] One-liner for RAII on non pointer?

查看:138
本文介绍了RAII的非线性指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关主题

std :: unique_ptr,deleters和Win32 API

要使用Win32句柄作为RAII,以下行

To use a Win32 Handle as a RAII, I can use the following line

std::unique_ptr<std::remove_pointer<HANDLE>::type, decltype(&CloseHandle)> m_mutex(CreateMutex(NULL, FALSE, NULL), &::CloseHandle);

对我来说,这是一个干净的一行,完全符合我想要的。

For me this is a clean one-liner and does exactly what I want.

对于SOCKET,它不会使用同一行编译,因为SOCKET不能为nullptr。

When it comes to SOCKET, it won't compile with this same line since SOCKET cannot be nullptr.

做它的工作是以下:

struct SocketDeleter
{
    typedef SOCKET pointer;

    void operator()(SOCKET h) 
    { 
        ::closesocket(h);
    }
};

// Start listen socket.
std::unique_ptr<SOCKET, SocketDeleter> sock(socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP));

我不喜欢在这个实现是任何不同类型的资源,我想要使用,我需要复制/粘贴相同的代码,只更改关闭函数。

What I don't like in this implementation is that any different type of ressources I'll want to use, I'll need to copy/paste the same code to only change the closing function.

我可以使用一个宏,但这是真的丑陋,两次使用

I could use a Macro, but this is really ugly and can't be used twice

#define RAII_UNIQUE_RESOURCE(varName, classType, init, closure)  \
struct deleterMacro                                             \
{                                                               \
    typedef classType pointer;                                  \
    void operator()(classType h)                                \
    {                                                           \
        closure(h);                                             \
    }                                                           \
};                                                              \
std::unique_ptr<classType, deleterMacro> varName(init);

// Compile, but breaks as soon as 2 sockets defined.
RAII_UNIQUE_RESOURCE(sock, SOCKET, socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP), ::closesocket);

我试图使用模板,但是我不能将我的函数指针传递给operator

I tried to use a template, but I cannot pass my function pointer to the operator() function, as far as I know.

template<class T, class methodDeclaration, class pFuncPointer>
struct deleter
{
    typedef T pointer;

    void operator()(T h)
    {
        // Is there a way?? 
        methodDeclaration toCall = pFuncPointer;
        toCall(h);
    }
};
// With a call such as ...
std::unique_ptr<SOCKET, deleter<SOCKET, std::function<decltype(::closesocket)>, ::closesocket>> sock2(socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_UDP));


推荐答案

最后,我想使用另一个Kerrek SB答案。这是STD唯一资源的建议。

Finally, I want with another Kerrek SB answer. It's the proposal for STD Unique Resource.

#ifndef UNIQUE_RESOURCE_H_
#define UNIQUE_RESOURCE_H_

#include <type_traits>

// From standard proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3949.pdf
// Slightly modified to compile on VS2012.
namespace std
{
    namespace experimental
    {
        enum class invoke_it { once, again };
        template<typename R,typename D>
        class unique_resource_t 
        {
            R resource;
            D deleter;
            bool execute_on_destruction; // exposition only
            unique_resource_t& operator=(unique_resource_t const &);
            unique_resource_t(unique_resource_t const &); // no copies!
        public:
            // construction
            explicit unique_resource_t(R && resource, D && deleter, bool shouldrun=true)
                : resource(std::move(resource))
                , deleter(std::move(deleter))
                , execute_on_destruction(shouldrun)
            {

            }
            // move
            unique_resource_t(unique_resource_t &&other) /*noexcept*/
                :resource(std::move(other.resource))
                ,deleter(std::move(other.deleter))
                ,execute_on_destruction(other.execute_on_destruction)
            {
                    other.release();
            }
            unique_resource_t& operator=(unique_resource_t &&other) 
            {
                this->invoke(invoke_it::once);
                deleter=std::move(other.deleter);
                resource=std::move(other.resource);
                execute_on_destruction=other.execute_on_destruction;
                other.release();
                return *this;
            }
            // resource release
            ~unique_resource_t() 
            {
                this->invoke(invoke_it::once);
            }
            void invoke(invoke_it const strategy = invoke_it::once) 
            {
                if (execute_on_destruction) {
                    try {
                        this->get_deleter()(resource);
                    } catch(...){}
                }
                execute_on_destruction = strategy==invoke_it::again;
            }
            R const & release() /*noexcept*/{
                execute_on_destruction = false;
                return this->get();
            }
            void reset(R && newresource) /*noexcept*/ {
                invoke(invoke_it::again);
                resource = std::move(newresource);
            }
            // resource access
            R const & get() const /*noexcept*/ {
                return resource;
            }
            operator R const &() const /*noexcept*/ 
            {
                return resource;
            }
            R operator->() const /*noexcept*/ 
            {
                return resource;
            }

//             Couldn't make this function compile on VS2012
//             std::add_lvalue_reference<std::remove_pointer<R>::type>::type operator*() const 
//             {
//                     return * resource;
//             }

            // deleter access
            const D & get_deleter() const /*noexcept*/ 
            {
                return deleter;
            }
        };

        //factories
        template<typename R,typename D>
        unique_resource_t<R,D> unique_resource( R && r,D t) /*noexcept*/ 
        {
                return unique_resource_t<R,D>(std::move(r), std::move(t),true);
        }
            template<typename R,typename D>
        unique_resource_t<R,D>
            unique_resource_checked(R r, R invalid, D t ) /*noexcept*/ {
                bool shouldrun = (r != invalid);
                return unique_resource_t<R,D>(std::move(r), std::move(t), shouldrun);
        }
    }
}
#endif /* UNIQUE RESOURCE H */

使用

auto listenSocket = std::experimental::unique_resource_checked(socket(AF_INET,SOCK_STREAM,IPPROTO_TCP), INVALID_SOCKET, closesocket);

希望这很快就足够了!

这篇关于RAII的非线性指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆