Browse Source

Auto-generating comparison functions for smart pointers and tag pointers.
Fixing const-ness of const_ptr's methods.

Samuel Jaffe 9 years ago
parent
commit
fdd673b45e
5 changed files with 131 additions and 295 deletions
  1. 7 99
      const_propogating_ptr.hpp
  2. 11 8
      const_ptr.hpp
  3. 2 94
      maybe_null.hpp
  4. 2 94
      not_null.hpp
  5. 109 0
      ptr_compare.hpp

+ 7 - 99
const_propogating_ptr.hpp

@@ -9,19 +9,20 @@
 #include <memory>
 
 #include "pointer_fwd.hpp"
+#include "ptr_compare.hpp"
 
-template <typename T>
+template <typename P>
 class const_propogating_ptr {
 public:
-  using element_type = typename std::pointer_traits<T>::element_type;
+  using element_type = typename std::pointer_traits<P>::element_type;
   using pointer = element_type  *;
   using reference = element_type &;
   using const_pointer = element_type const *;
   using const_reference = element_type const &;
 
   const_propogating_ptr() : _ptr(nullptr) {}
-  const_propogating_ptr(T const & p) : _ptr(p) {}
-  const_propogating_ptr(T && p) : _ptr(std::move(p)) {}
+  const_propogating_ptr(P const & p) : _ptr(p) {}
+  const_propogating_ptr(P && p) : _ptr(std::move(p)) {}
   
   const_propogating_ptr(const_propogating_ptr & other) : _ptr(other._ptr) {}
   const_propogating_ptr(const_propogating_ptr &&) = default;
@@ -56,100 +57,7 @@ public:
   const_pointer operator->() const { return get(); }
   
 private:
-  T _ptr;
+  P _ptr;
 };
 
-template <typename T, typename U>
-bool operator==(const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  return lhs.get() == rhs.get();
-}
-
-template <typename T, typename U>
-bool operator!=(const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  return !(lhs == rhs);
-}
-
-template <typename T, typename U>
-bool operator< (const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  typedef typename std::common_type<
-  typename const_propogating_ptr<T>::pointer,
-  typename const_propogating_ptr<U>::pointer>::type V;
-  return std::less<V>(lhs.get(), rhs.get());
-}
-
-template <typename T, typename U>
-bool operator> (const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  return rhs < lhs;
-}
-
-template <typename T, typename U>
-bool operator<=(const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  return !(rhs < lhs);
-}
-
-template <typename T, typename U>
-bool operator>=(const_propogating_ptr<T> const&lhs, const_propogating_ptr<U> const&rhs) {
-  return !(lhs < rhs);
-}
-
-template <typename T>
-bool operator==(const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  return !lhs;
-}
-
-template <typename T>
-bool operator==(std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  return !rhs;
-}
-
-template <typename T>
-bool operator!=(const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  return static_cast<bool>(lhs);
-}
-
-template <typename T>
-bool operator!=(std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  return static_cast<bool>(rhs);
-}
-
-template <typename T>
-bool operator< (const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  typedef typename const_propogating_ptr<T>::pointer V;
-  return std::less<V>(lhs.get(), nullptr);
-}
-
-template <typename T>
-bool operator< (std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  typedef typename const_propogating_ptr<T>::pointer V;
-  return std::less<V>(nullptr, rhs.get());
-}
-
-template <typename T>
-bool operator> (const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  return nullptr < lhs;
-}
-
-template <typename T>
-bool operator> (std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  return rhs < nullptr;
-}
-
-template <typename T>
-bool operator<=(const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  return !(nullptr < lhs);
-}
-
-template <typename T>
-bool operator<=(std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  return !(rhs < nullptr);
-}
-
-template <typename T>
-bool operator>=(const_propogating_ptr<T> const&lhs, std::nullptr_t) {
-  return !(lhs < nullptr);
-}
-
-template <typename T>
-bool operator>=(std::nullptr_t, const_propogating_ptr<T> const&rhs) {
-  return !(nullptr < rhs);
-}
+POINTER_TEMPLATE_COMPARE( const_propogating_ptr )

+ 11 - 8
const_ptr.hpp

@@ -9,26 +9,29 @@
 #include <memory>
 
 #include "pointer_fwd.hpp"
+#include "ptr_compare.hpp"
 
 template <typename P>
 class const_ptr {
 public:
-  using element_type = typename std::pointer_traits<T>::element_type;
+  using element_type = typename std::pointer_traits<P>::element_type;
   using pointer = element_type const *;
   using reference = element_type const &;
 
   const_ptr() : _ptr(nullptr) {}
-  const_ptr(T const & p) : _ptr(p) {}
-  const_ptr(T && p) : _ptr(std::move(p)) {}
+  const_ptr(P const & p) : _ptr(p) {}
+  const_ptr(P && p) : _ptr(std::move(p)) {}
   
   template <typename Y>
-  explicit operator const_ptr<Y>() & {
+  explicit operator const_ptr<Y>() const {
     return _ptr;
   }
 
-  reference operator*() { return *_ptr; }
-  pointer get() { return std::addressof(operator*()); }
-  pointer operator->() { return get(); }
+  reference operator*() const { return *_ptr; }
+  pointer get() const { return std::addressof(operator*()); }
+  pointer operator->() const { return get(); }
 private:
   P _ptr;
-};
+};
+
+POINTER_TEMPLATE_COMPARE( const_ptr )

+ 2 - 94
maybe_null.hpp

@@ -12,6 +12,7 @@
 #include <memory>
 
 #include "pointer_fwd.hpp"
+#include "ptr_compare.hpp"
 
 class unchecked_pointer_exception : public std::logic_error {
   using std::logic_error::logic_error;
@@ -73,97 +74,4 @@ private:
 #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);
-}
+POINTER_TEMPLATE_COMPARE( maybe_null )

+ 2 - 94
not_null.hpp

@@ -12,6 +12,7 @@
 #include <memory>
 
 #include "pointer_fwd.hpp"
+#include "ptr_compare.hpp"
 #include "maybe_null.hpp"
 
 class null_pointer_exception : public std::invalid_argument {
@@ -64,97 +65,4 @@ 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);
-}
+POINTER_TEMPLATE_COMPARE( not_null )

+ 109 - 0
ptr_compare.hpp

@@ -0,0 +1,109 @@
+//
+//  ptr_compare.hpp
+//  pointers
+//
+//  Created by Sam Jaffe on 1/5/17.
+//
+
+#pragma once
+
+#define POINTER_TEMPLATE_COMPARE( ptr_t ) \
+  POINTER_TEMPLATE_COMPARE_FULL( ptr_t, (typename T), (T), (typename U), (U) )
+
+#define EXPAND( ... ) __VA_ARGS__
+
+#define POINTER_TEMPLATE_COMPARE_FULL( ptr_t, T_tname, T, U_tname, U ) \
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator==(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  return lhs.get() == rhs.get(); \
+} \
+\
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator!=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  return !(lhs == rhs); \
+} \
+\
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator< (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  typedef typename std::common_type< \
+  typename ptr_t< EXPAND T >::pointer, \
+  typename ptr_t< EXPAND U >::pointer>::type V; \
+  return std::less<V>(lhs.get(), rhs.get()); \
+} \
+\
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator> (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  return rhs < lhs; \
+} \
+\
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator<=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  return !(rhs < lhs); \
+} \
+\
+template <EXPAND T_tname, EXPAND U_tname> \
+bool operator>=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+  return !(lhs < rhs); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator==(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  return !lhs; \
+} \
+\
+template <EXPAND T_tname> \
+bool operator==(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  return !rhs; \
+} \
+\
+template <EXPAND T_tname> \
+bool operator!=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  return static_cast<bool>(lhs); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator!=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  return static_cast<bool>(rhs); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator< (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  typedef typename ptr_t< EXPAND T >::pointer V; \
+  return std::less<V>(lhs.get(), nullptr); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator< (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  typedef typename ptr_t< EXPAND T >::pointer V; \
+  return std::less<V>(nullptr, rhs.get()); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator> (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  return nullptr < lhs; \
+} \
+\
+template <EXPAND T_tname> \
+bool operator> (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  return rhs < nullptr; \
+} \
+\
+template <EXPAND T_tname> \
+bool operator<=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  return !(nullptr < lhs); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator<=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  return !(rhs < nullptr); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator>=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+  return !(lhs < nullptr); \
+} \
+\
+template <EXPAND T_tname> \
+bool operator>=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+  return !(nullptr < rhs); \
+}