2010-12-13 6 views
5

"안전하지 않은"것을 보장하는 "safe_ptr"이라는 안전한 버전의 std :: shared_ptr을 구현하려고합니다.safe_ptr implementation

편집 : 삭제 된 질문. 관심있는 경우 편집을 참조하십시오. 관심있는 사람에게 최종 솔루션 게시 :

이 코드는 현재 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))); 
} 
+1

+1 좋은 일. 당신은 어떤 종류의 면허하에 이것을 풀어 주겠습니까? – KitsuneYMG

+0

GNU General Public License 3 또는 그 이상이 될 것 같습니다. 어떤 프로젝트에서 사용한다면 메일을 보내 주시면 감사하겠습니다. – ronag

+0

mail : [email protected] – ronag

답변

4

당신은 인수가 is_convertible으로 되겠습니다. 검사 할로 표현된다 T*-Y* : 당신의 편집에 관한

std::is_convertible<Y*, T*> 

: 예, 당신은 당신이 safe_ptr의 기본 생성자를 제공 할 수있는, 친구 선언

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

필요 기본적으로 객체가 기본적으로 생성됩니다. 이전 질문에 대한 대답에서 가리킨 개체의 기본 생성자를 호출하는 것을 잊었습니다.

1

나는 is_convertible 뒷벽을 사용하고 있다고 생각합니다. 시도하십시오

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_){} 
+0

그러나 이제는 새로운 문제가 있습니다 ... 편집을 참조하십시오. – ronag

+0

@ronag : 예, 접근성 문제입니다. 친구의 선언으로 해결됩니다. Matthieu는 그의 대답에 모범을 보였습니다. –

1

전용 멤버 impl_에 액세스하려면 safe_ptr 템플릿의 다른 인스턴스를 작성해야합니다.

template <class Y> friend class safe_ptr; 
1
template<class Y> friend class safe_ptr; 
관련 문제