Program Listing for File not_null.hpp
↰ Return to documentation for file (src/navtk/not_null.hpp)
#pragma once
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <navtk/errors.hpp>
// Both forward declarations necessary here to allow a template class in one namespace to have a
// friend function in a separate namespace.
namespace navtk {
template <typename T>
class not_null;
}
namespace std {
template <typename T, typename U>
std::shared_ptr<T> dynamic_pointer_cast(const navtk::not_null<U>&) noexcept;
}
namespace navtk {
template <typename T>
class not_null {
public:
static_assert(std::is_convertible<decltype(std::declval<T>() == nullptr), bool>::value,
"T cannot be compared to nullptr");
template <typename = std::enable_if_t<!std::is_null_pointer<T>::value>>
constexpr not_null(T p) : ptr(std::move(p)) {
if (ptr == nullptr) log_or_throw<std::invalid_argument>("Pointer must be non-null.");
}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(U&& p) : ptr(std::forward<U>(p)) {
if (ptr == nullptr) log_or_throw<std::invalid_argument>("Pointer must be non-null.");
}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(const not_null<U>& other) : not_null(other.get()) {}
not_null(const not_null& other) = default;
not_null(not_null&& other) = default;
not_null& operator=(const not_null& other) = default;
not_null& operator=(not_null&& other) = default;
constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const {
if (ptr == nullptr) log_or_throw("Held pointer is no longer non-null.");
return ptr;
}
constexpr operator T() const { return get(); }
constexpr decltype(auto) operator->() const { return get(); }
constexpr decltype(auto) operator*() const { return *get(); }
void operator[](std::ptrdiff_t) const = delete;
not_null(std::nullptr_t) = delete;
not_null& operator=(std::nullptr_t) = delete;
not_null& operator++() = delete;
not_null& operator--() = delete;
not_null operator++(int) = delete;
not_null operator--(int) = delete;
not_null& operator+=(std::ptrdiff_t) = delete;
not_null& operator-=(std::ptrdiff_t) = delete;
template <typename U, typename V>
friend std::shared_ptr<U> std::dynamic_pointer_cast(const navtk::not_null<V>&) noexcept;
private:
T ptr;
};
template <class T>
auto make_not_null(T&& p) noexcept {
return not_null<std::remove_cv_t<std::remove_reference_t<T>>>{std::forward<T>(p)};
}
template <class T>
std::ostream& operator<<(std::ostream& os, const not_null<T>& val) {
os << val.get();
return os;
}
template <class T, class U>
auto operator==(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() == rhs.get())) -> decltype(lhs.get() == rhs.get()) {
return lhs.get() == rhs.get();
}
template <class T, class U>
auto operator!=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() != rhs.get())) -> decltype(lhs.get() != rhs.get()) {
return lhs.get() != rhs.get();
}
template <class T, class U>
auto operator<(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() < rhs.get())) -> decltype(lhs.get() < rhs.get()) {
return lhs.get() < rhs.get();
}
template <class T, class U>
auto operator<=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() <= rhs.get())) -> decltype(lhs.get() <= rhs.get()) {
return lhs.get() <= rhs.get();
}
template <class T, class U>
auto operator>(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() > rhs.get())) -> decltype(lhs.get() > rhs.get()) {
return lhs.get() > rhs.get();
}
template <class T, class U>
auto operator>=(const not_null<T>& lhs, const not_null<U>& rhs) noexcept(
noexcept(lhs.get() >= rhs.get())) -> decltype(lhs.get() >= rhs.get()) {
return lhs.get() >= rhs.get();
}
#ifndef NEED_DOXYGEN_EXHALE_WORKAROUND
// Disable not_null pointer arithmetic addition/subtraction.
template <class T, class U>
std::ptrdiff_t operator-(const not_null<T>&, const not_null<U>&) = delete;
template <class T>
not_null<T> operator-(const not_null<T>&, std::ptrdiff_t) = delete;
template <class T>
not_null<T> operator+(const not_null<T>&, std::ptrdiff_t) = delete;
template <class T>
not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
#endif
} // namespace navtk
namespace std {
// Implementation of overload for `std::dynamic_pointer_cast` declared in `navtk::not_null`.
template <typename T, typename U>
std::shared_ptr<T> dynamic_pointer_cast(const navtk::not_null<U>& p) noexcept {
return dynamic_pointer_cast<T>(p.ptr);
}
} // namespace std