throwing_ptr
Smart pointers that throw on dereference if null
null_ptr_exception.hpp
Go to the documentation of this file.
1 // Copyright Claudio Bantaloukas 2017-2018.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 /** \file throwing/null_ptr_exception.hpp
7  * \brief Exceptions thrown by smart pointers in namespace throwing
8  */
9 
10 #pragma once
11 #include <exception>
12 #include <string>
13 #include <typeinfo>
14 
15 namespace throwing {
16 
17 /** \brief Base class thrown upon dereferencing a null shared_ptr.
18  *
19  * Use to catch all such errors
20  */
21 class base_null_ptr_exception : public std::logic_error {
22 public:
23  base_null_ptr_exception() : std::logic_error("Dereference of nullptr") {}
24  virtual std::string what_type() const { return ""; }
25 };
26 
27 /** \brief Concrete class thrown upon dereferencing a null shared_ptr.
28  *
29  * Use to catch dereferencing of specific types
30  */
31 template <typename T>
33 public:
34  virtual std::string what_type() const {
35  std::string result("Dereferenced nullptr of type ");
36  result.append(typeid(T).name());
37  return result;
38  }
39 };
40 
41 } // namespace throwing
Base class thrown upon dereferencing a null shared_ptr.
virtual std::string what_type() const
virtual std::string what_type() const
Implementations of throwing::shared_ptr, throwing::unique_ptr and related.
Concrete class thrown upon dereferencing a null shared_ptr.