Browse Source

Adding comparison matching that of shared_ptr.

Samuel Jaffe 9 years ago
parent
commit
ef2f31c5ef
2 changed files with 190 additions and 12 deletions
  1. 95 6
      maybe_null.hpp
  2. 95 6
      not_null.hpp

+ 95 - 6
maybe_null.hpp

@@ -66,15 +66,104 @@ public:
 #endif
     return *_ptr;
   }
-  bool operator==(maybe_null const&rhs) const { return _ptr == rhs._ptr; }
-  bool operator!=(maybe_null const&rhs) const { return _ptr != rhs._ptr; }
-  bool operator<=(maybe_null const&rhs) const { return _ptr <= rhs._ptr; }
-  bool operator>=(maybe_null const&rhs) const { return _ptr >= rhs._ptr; }
-  bool operator< (maybe_null const&rhs) const { return _ptr <  rhs._ptr; }
-  bool operator> (maybe_null const&rhs) const { return _ptr >  rhs._ptr; }
 private:
   T _ptr;
 #if defined( DEBUG )
   mutable bool tested_ = false;
 #endif
 };
+
+template <typename T, typename U>
+bool operator==(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  return lhs.get() == rhs.get();
+}
+
+template <typename T, typename U>
+bool operator!=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  return !(lhs == rhs);
+}
+
+template <typename T, typename U>
+bool operator< (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  typedef typename std::common_type<
+    typename maybe_null<T>::pointer,
+    typename maybe_null<U>::pointer>::type V;
+  return std::less<V>(lhs.get(), rhs.get());
+}
+
+template <typename T, typename U>
+bool operator> (maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  return rhs < lhs;
+}
+
+template <typename T, typename U>
+bool operator<=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  return !(rhs < lhs);
+}
+
+template <typename T, typename U>
+bool operator>=(maybe_null<T> const&lhs, maybe_null<U> const&rhs) {
+  return !(lhs < rhs);
+}
+
+template <typename T>
+bool operator==(maybe_null<T> const&lhs, std::nullptr_t) {
+  return !lhs;
+}
+
+template <typename T>
+bool operator==(std::nullptr_t, maybe_null<T> const&rhs) {
+  return !rhs;
+}
+
+template <typename T>
+bool operator!=(maybe_null<T> const&lhs, std::nullptr_t) {
+  return static_cast<bool>(lhs);
+}
+
+template <typename T>
+bool operator!=(std::nullptr_t, maybe_null<T> const&rhs) {
+  return static_cast<bool>(rhs);
+}
+
+template <typename T>
+bool operator< (maybe_null<T> const&lhs, std::nullptr_t) {
+  typedef typename maybe_null<T>::pointer V;
+  return std::less<V>(lhs.get(), nullptr);
+}
+
+template <typename T>
+bool operator< (std::nullptr_t, maybe_null<T> const&rhs) {
+  typedef typename maybe_null<T>::pointer V;
+  return std::less<V>(nullptr, rhs.get());
+}
+
+template <typename T>
+bool operator> (maybe_null<T> const&lhs, std::nullptr_t) {
+  return nullptr < lhs;
+}
+
+template <typename T>
+bool operator> (std::nullptr_t, maybe_null<T> const&rhs) {
+  return rhs < nullptr;
+}
+
+template <typename T>
+bool operator<=(maybe_null<T> const&lhs, std::nullptr_t) {
+  return !(nullptr < lhs);
+}
+
+template <typename T>
+bool operator<=(std::nullptr_t, maybe_null<T> const&rhs) {
+  return !(rhs < nullptr);
+}
+
+template <typename T>
+bool operator>=(maybe_null<T> const&lhs, std::nullptr_t) {
+  return !(lhs < nullptr);
+}
+
+template <typename T>
+bool operator>=(std::nullptr_t, maybe_null<T> const&rhs) {
+  return !(nullptr < rhs);
+}

+ 95 - 6
not_null.hpp

@@ -55,12 +55,6 @@ public:
   reference operator*() const { return *_ptr; }
   pointer operator->() const { return get(); }
   
-  bool operator==(not_null const&rhs) const { return _ptr == rhs._ptr; }
-  bool operator!=(not_null const&rhs) const { return _ptr != rhs._ptr; }
-  bool operator<=(not_null const&rhs) const { return _ptr <= rhs._ptr; }
-  bool operator>=(not_null const&rhs) const { return _ptr >= rhs._ptr; }
-  bool operator< (not_null const&rhs) const { return _ptr <  rhs._ptr; }
-  bool operator> (not_null const&rhs) const { return _ptr >  rhs._ptr; }
 private:
   void validate() {
     if (get() == nullptr) {
@@ -69,3 +63,98 @@ private:
   }
   T _ptr;
 };
+
+template <typename T, typename U>
+bool operator==(not_null<T> const&lhs, not_null<U> const&rhs) {
+  return lhs.get() == rhs.get();
+}
+
+template <typename T, typename U>
+bool operator!=(not_null<T> const&lhs, not_null<U> const&rhs) {
+  return !(lhs == rhs);
+}
+
+template <typename T, typename U>
+bool operator< (not_null<T> const&lhs, not_null<U> const&rhs) {
+  typedef typename std::common_type<
+  typename not_null<T>::pointer,
+  typename not_null<U>::pointer>::type V;
+  return std::less<V>(lhs.get(), rhs.get());
+}
+
+template <typename T, typename U>
+bool operator> (not_null<T> const&lhs, not_null<U> const&rhs) {
+  return rhs < lhs;
+}
+
+template <typename T, typename U>
+bool operator<=(not_null<T> const&lhs, not_null<U> const&rhs) {
+  return !(rhs < lhs);
+}
+
+template <typename T, typename U>
+bool operator>=(not_null<T> const&lhs, not_null<U> const&rhs) {
+  return !(lhs < rhs);
+}
+
+template <typename T>
+bool operator==(not_null<T> const&lhs, std::nullptr_t) {
+  return !lhs;
+}
+
+template <typename T>
+bool operator==(std::nullptr_t, not_null<T> const&rhs) {
+  return !rhs;
+}
+
+template <typename T>
+bool operator!=(not_null<T> const&lhs, std::nullptr_t) {
+  return static_cast<bool>(lhs);
+}
+
+template <typename T>
+bool operator!=(std::nullptr_t, not_null<T> const&rhs) {
+  return static_cast<bool>(rhs);
+}
+
+template <typename T>
+bool operator< (not_null<T> const&lhs, std::nullptr_t) {
+  typedef typename not_null<T>::pointer V;
+  return std::less<V>(lhs.get(), nullptr);
+}
+
+template <typename T>
+bool operator< (std::nullptr_t, not_null<T> const&rhs) {
+  typedef typename not_null<T>::pointer V;
+  return std::less<V>(nullptr, rhs.get());
+}
+
+template <typename T>
+bool operator> (not_null<T> const&lhs, std::nullptr_t) {
+  return nullptr < lhs;
+}
+
+template <typename T>
+bool operator> (std::nullptr_t, not_null<T> const&rhs) {
+  return rhs < nullptr;
+}
+
+template <typename T>
+bool operator<=(not_null<T> const&lhs, std::nullptr_t) {
+  return !(nullptr < lhs);
+}
+
+template <typename T>
+bool operator<=(std::nullptr_t, not_null<T> const&rhs) {
+  return !(rhs < nullptr);
+}
+
+template <typename T>
+bool operator>=(not_null<T> const&lhs, std::nullptr_t) {
+  return !(lhs < nullptr);
+}
+
+template <typename T>
+bool operator>=(std::nullptr_t, not_null<T> const&rhs) {
+  return !(nullptr < rhs);
+}