Guest User

safe_ptr

a guest
Dec 21st, 2016
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #define CATCH_CONFIG_MAIN
  2. #include "catch.hpp"
  3. #include <memory>
  4. #include <utility>
  5. #include <exception>
  6. #include <type_traits>
  7.  
  8. namespace mem {
  9.    
  10.     class null_dereference_exception : public std::exception { };
  11.    
  12.     template<typename T, typename Container = std::unique_ptr<T>>
  13.     class safe_ptr {
  14.         public:
  15.             //template<typename std::enable_if< std::is_pointer<T*>{}, int >::type = 0>
  16.             safe_ptr() { }
  17.            
  18.             safe_ptr(T* ptr) : data_(ptr) { }
  19.            
  20.             // to support make_safe_*
  21.             safe_ptr(Container && src) : data_(std::move(src)) { }
  22.            
  23.             // only enabled if container supports it
  24.             safe_ptr(safe_ptr && o) : data_(std::move(o.data_)) {
  25.                
  26.             }
  27.            
  28.             // only enabled if container supports it
  29.             safe_ptr(safe_ptr const & o) : data_(o.data_) {
  30.  
  31.             }
  32.            
  33.             T & operator*() {
  34.                 throw_if_null();
  35.                 return *data_;
  36.             }
  37.            
  38.             T const & operator*() const {
  39.                 throw_if_null();
  40.                 return *data_;
  41.             }
  42.                        
  43.             T const * operator->() const {
  44.                 throw_if_null();
  45.                 return get();
  46.             }
  47.  
  48.             T * operator->() {
  49.                 throw_if_null();
  50.                 return get();
  51.             }
  52.            
  53.             T* get() { return data_.get(); }
  54.             T const * get() const { return data_.get(); }
  55.            
  56.             void reset() { data_.reset(); }
  57.            
  58.             operator bool() const { return (bool)data_; }
  59.            
  60.         private:
  61.        
  62.             void throw_if_null() {
  63.                 if ( !data_ ) throw null_dereference_exception();
  64.             }
  65.        
  66.             Container data_;
  67.     };
  68.    
  69.     template<typename T, typename... Args>
  70.     auto make_safe_unique(Args&&... a) -> safe_ptr<T, std::unique_ptr<T>> {
  71.         return safe_ptr<T, std::unique_ptr<T>>(std::make_unique<T>(std::forward<Args>(a)...));
  72.     }
  73.    
  74.     template<typename T, typename... Args>
  75.     auto make_safe_shared(Args&&... a) -> safe_ptr<T, std::shared_ptr<T>> {
  76.         return safe_ptr<T, std::shared_ptr<T>>(std::make_shared<T>(std::forward<Args>(a)...));
  77.     }    
  78. }
  79.  
  80. TEST_CASE("safe_ptr can be instantiated with a pointer", "[constructor]") {
  81.     auto str = new std::string();
  82.     mem::safe_ptr<std::string> sptr(str);
  83.     REQUIRE(str == sptr.get());
  84. }
  85.  
  86. TEST_CASE("safe_ptr can be instantiated by moving a unique pointer", "[constructor]") {
  87.     auto str = std::make_unique<std::string>("hello world!");
  88.     mem::safe_ptr<std::string> sptr(std::move(str));
  89.     REQUIRE(!str);
  90.     REQUIRE(*sptr == std::string("hello world!"));
  91. }
  92.  
  93. TEST_CASE("safe_ptr can be instantiated by moving a shared pointer", "[constructor]") {
  94.     auto str = std::make_shared<std::string>("hello world!");
  95.     mem::safe_ptr<std::string, std::shared_ptr<std::string>> sptr(std::move(str));
  96.     REQUIRE(!str);
  97.     REQUIRE(*sptr == std::string("hello world!"));
  98. }
  99.  
  100. TEST_CASE("safe_ptr can be instantiated with the make_safe_unique idiom", "[constructor]") {
  101.     auto sptr = mem::make_safe_unique<std::string>("hello world!");
  102.     REQUIRE(*sptr == std::string("hello world!"));
  103. }
  104.  
  105. TEST_CASE("safe_ptr can be instantiated with the make_safe_shared idiom", "[constructor]") {
  106.     auto sptr = mem::make_safe_shared<std::string>("hello world!");
  107.     REQUIRE(*sptr == std::string("hello world!"));
  108. }
  109.  
  110. TEST_CASE("safe_ptr can be copied if the container supports it", "[constructor]") {
  111.     auto sptr = mem::make_safe_shared<std::string>("hello world!");
  112.     auto sptr2 = sptr;
  113.     REQUIRE(*sptr == *sptr2);
  114.    
  115.     // this fails to compile because unique_ptr is not copyable
  116.     // auto sptr3 = mem::make_safe_unique<std::string>("hello world!");
  117.     // auto sptr4 = sptr3;
  118. }
  119.  
  120. TEST_CASE("safe_ptr supports simple bool operator", "[operators]") {
  121.     mem::safe_ptr<std::string> strp1;
  122.     auto strp2 = mem::make_safe_unique<std::string>("Hello world!");
  123.    
  124.     REQUIRE_FALSE(strp1);
  125.     REQUIRE(strp2);
  126. }
  127.  
  128. TEST_CASE("safe_ptr throw exception if deferencing nullptr", "[exceptions]") {
  129.     mem::safe_ptr<std::string> strp1;
  130.     auto strp2 = mem::make_safe_unique<std::string>("Hello world!");
  131.    
  132.     REQUIRE_THROWS_AS(*strp1, mem::null_dereference_exception);
  133.     REQUIRE_NOTHROW(*strp2);
  134.    
  135.     REQUIRE_THROWS_AS(strp1->c_str(), mem::null_dereference_exception);
  136.     REQUIRE_NOTHROW(strp2->c_str());
  137. }
Advertisement
Add Comment
Please, Sign In to add comment