safe_ptr 实现

发布于 2024-10-07 12:33:53 字数 7916 浏览 5 评论 0原文

我正在尝试实现 std::shared_ptr 的安全版本,称为“safe_ptr”,它保证“非空”。

编辑:删除问题。如果有兴趣请参阅编辑。向任何感兴趣的人发布最终解决方案:

此代码现在托管在 Google 代码上。

#pragma once

#include <memory>
#include <type_traits>
#include <exception>

template<typename T>
class safe_ptr
{   
    template <typename> friend class safe_ptr;
public:
    typedef T element_type;

    safe_ptr() : impl_(std::make_shared<T>()){} 

    safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){}

    template<typename U>
    safe_ptr(const safe_ptr<U>& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(other.impl_){}

    template<typename U>    
    safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(std::make_shared<U>(impl)) {}

    template<typename U, typename D>        
    safe_ptr(const U& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(new U(impl), dtor) {}

    template<typename U>    
    safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(std::make_shared<U>(std::forward<U>(impl))) {}

    template<typename U, typename D>    
    safe_ptr(U&& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(new U(std::forward<U>(impl)), dtor) {}

    template<typename U>    
    explicit safe_ptr(const std::shared_ptr<U>& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>    
    explicit safe_ptr(std::shared_ptr<U>&& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(std::move(impl))
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>    
    explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U, typename D>    
    explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>
    typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type
    operator=(const safe_ptr<U>& other)
    {
        safe_ptr<T> temp(other);
        temp.swap(*this);
        return *this;
    }

    template <typename U>
    typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type
    operator=(U&& impl)
    {
        safe_ptr<T> temp(std::forward<T>(impl));
        temp.swap(*this);
        return *this;
    }

    T& operator*() const { return *impl_.get();}

    T* operator->() const { return impl_.get();}

    T* get() const { return impl_.get();}

    bool unique() const { return impl_.unique();}

    long use_count() const { return impl_.use_count();}

    void swap(safe_ptr& other) { impl_.swap(other.impl_); } 

    operator std::shared_ptr<T>() const { return impl_;}

    template<class U>
    bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); }

    template<class U>
    bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }

    template<class D, class U> 
    D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }

private:    
    std::shared_ptr<T> impl_;
};

template<class T, class U>
bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() == b.get();
}

template<class T, class U>
bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() != b.get();
}

template<class T, class U>
bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() < b.get();
}

template<class T, class U>
bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() > b.get();
}

template<class T, class U>
bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() >= b.get();
}

template<class T, class U>
bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() <= b.get();
}

template<class E, class T, class U>
std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out, const safe_ptr<U>& p)
{
    return out << p.get();
}

template<class T> 
void swap(safe_ptr<T>& a, safe_ptr<T>& b)
{
    a.swap(b);
}

template<class T> 
T* get_pointer(safe_ptr<T> const& p)
{
    return p.get();
}

template <class T, class U>
safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)
{
    return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
}

template <class T, class U>
safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)
{
    return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
}

template <class T, class U>
safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
{
    auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
    if(!temp)
        throw std::bad_cast();
    return safe_ptr<T>(temp);
}

template<typename T>
safe_ptr<T> make_safe()
{
    return safe_ptr<T>();
}

template<typename T, typename P0>
safe_ptr<T> make_safe(P0&& p0)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
}

template<typename T, typename P0, typename P1>
safe_ptr<T> make_safe(P0&& p0, P1&& p1)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
}

template<typename T, typename P0, typename P1, typename P2>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
}

template<typename T, typename P0, typename P1, typename P2, typename P3>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
}

template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3), std::forward<P3>(p4)));
}

I'm trying to implement a safe version of std::shared_ptr, called "safe_ptr" which guarantees "non-nullness".

EDIT: Removed question. See edit if interested. Posting final solution to anyone interested:

This code is now hosted on google code.

#pragma once

#include <memory>
#include <type_traits>
#include <exception>

template<typename T>
class safe_ptr
{   
    template <typename> friend class safe_ptr;
public:
    typedef T element_type;

    safe_ptr() : impl_(std::make_shared<T>()){} 

    safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){}

    template<typename U>
    safe_ptr(const safe_ptr<U>& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(other.impl_){}

    template<typename U>    
    safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(std::make_shared<U>(impl)) {}

    template<typename U, typename D>        
    safe_ptr(const U& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(new U(impl), dtor) {}

    template<typename U>    
    safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(std::make_shared<U>(std::forward<U>(impl))) {}

    template<typename U, typename D>    
    safe_ptr(U&& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
        : impl_(new U(std::forward<U>(impl)), dtor) {}

    template<typename U>    
    explicit safe_ptr(const std::shared_ptr<U>& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>    
    explicit safe_ptr(std::shared_ptr<U>&& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(std::move(impl))
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>    
    explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U, typename D>    
    explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)
    {
        if(!impl_)
            throw std::invalid_argument("impl");
    }

    template<typename U>
    typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type
    operator=(const safe_ptr<U>& other)
    {
        safe_ptr<T> temp(other);
        temp.swap(*this);
        return *this;
    }

    template <typename U>
    typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type
    operator=(U&& impl)
    {
        safe_ptr<T> temp(std::forward<T>(impl));
        temp.swap(*this);
        return *this;
    }

    T& operator*() const { return *impl_.get();}

    T* operator->() const { return impl_.get();}

    T* get() const { return impl_.get();}

    bool unique() const { return impl_.unique();}

    long use_count() const { return impl_.use_count();}

    void swap(safe_ptr& other) { impl_.swap(other.impl_); } 

    operator std::shared_ptr<T>() const { return impl_;}

    template<class U>
    bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); }

    template<class U>
    bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }

    template<class D, class U> 
    D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }

private:    
    std::shared_ptr<T> impl_;
};

template<class T, class U>
bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() == b.get();
}

template<class T, class U>
bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() != b.get();
}

template<class T, class U>
bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() < b.get();
}

template<class T, class U>
bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() > b.get();
}

template<class T, class U>
bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() >= b.get();
}

template<class T, class U>
bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)
{
    return a.get() <= b.get();
}

template<class E, class T, class U>
std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out, const safe_ptr<U>& p)
{
    return out << p.get();
}

template<class T> 
void swap(safe_ptr<T>& a, safe_ptr<T>& b)
{
    a.swap(b);
}

template<class T> 
T* get_pointer(safe_ptr<T> const& p)
{
    return p.get();
}

template <class T, class U>
safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)
{
    return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
}

template <class T, class U>
safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)
{
    return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
}

template <class T, class U>
safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
{
    auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
    if(!temp)
        throw std::bad_cast();
    return safe_ptr<T>(temp);
}

template<typename T>
safe_ptr<T> make_safe()
{
    return safe_ptr<T>();
}

template<typename T, typename P0>
safe_ptr<T> make_safe(P0&& p0)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
}

template<typename T, typename P0, typename P1>
safe_ptr<T> make_safe(P0&& p0, P1&& p1)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
}

template<typename T, typename P0, typename P1, typename P2>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
}

template<typename T, typename P0, typename P1, typename P2, typename P3>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
}

template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&)
{
    return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3), std::forward<P3>(p4)));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮萍、无处依 2024-10-14 12:33:53

您向后获取了 is_convertible 的参数。您想要检查 Y*T* ,其表示为:

std::is_convertible<Y*, T*>

关于您的编辑:是的,您需要一个友元声明

// Within the body of the class
template <typename> friend class safe_ptr; // the syntax is peculiar...

另外,您可能希望提供一个默认构造函数safe_ptr,它自然默认构造对象。我忘记了调用我对上一个问题的回答中指出的对象的默认构造函数:)

You got the arguments to is_convertible backward. You want to check Y* to T* which is expressed as:

std::is_convertible<Y*, T*>

Regarding your edit: yes, you need a friend declaration

// Within the body of the class
template <typename> friend class safe_ptr; // the syntax is peculiar...

Also, you might wish to provide a default constructor for safe_ptr, which naturally default constructs the object. I had forgotten about invoking the default constructor of the object pointed to in my answer to your previous question :)

一袭白衣梦中忆 2024-10-14 12:33:53

我认为您正在使用 is_convertible backwarks。尝试

template<typename Y>
safe_ptr(const safe_ptr<Y>& other, typename std::enable_if<std::is_convertible<Y*, T*>::value, void*>::type = 0) : impl_(other.impl_){}

I think you are using is_convertible backwarks. Try

template<typename Y>
safe_ptr(const safe_ptr<Y>& other, typename std::enable_if<std::is_convertible<Y*, T*>::value, void*>::type = 0) : impl_(other.impl_){}
無心 2024-10-14 12:33:53

为了访问私有成员 impl_,您必须与 safe_ptr 模板的不同实例建立友好关系。

template <class Y> friend class safe_ptr;

For the access of private member impl_, you have to befriend different instantiations of the safe_ptr template.

template <class Y> friend class safe_ptr;
何以心动 2024-10-14 12:33:53
template<class Y> friend class safe_ptr;
template<class Y> friend class safe_ptr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文