Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r49164 - sandbox/SOC/2008/graphs/trunk/boost
From: asutton_at_[hidden]
Date: 2008-10-07 10:53:07


Author: asutton
Date: 2008-10-07 10:53:06 EDT (Tue, 07 Oct 2008)
New Revision: 49164
URL: http://svn.boost.org/trac/boost/changeset/49164

Log:
- Added specialization on T*
- Rewrote operator bool() to use unspecified-bool idiom.
- Implemented equality operators that are probably wrong.

Text files modified:
   sandbox/SOC/2008/graphs/trunk/boost/optional.hpp | 104 ++++++++++++++++++++++++++++++++++++---
   1 files changed, 96 insertions(+), 8 deletions(-)

Modified: sandbox/SOC/2008/graphs/trunk/boost/optional.hpp
==============================================================================
--- sandbox/SOC/2008/graphs/trunk/boost/optional.hpp (original)
+++ sandbox/SOC/2008/graphs/trunk/boost/optional.hpp 2008-10-07 10:53:06 EDT (Tue, 07 Oct 2008)
@@ -1,6 +1,6 @@
 
-#ifndef BOOST_WEAK_OPTIONAL_HPP
-#define BOOST_WEAK_OPTIONAL_HPP
+#ifndef BOOST_OPTIONAL_HPP
+#define BOOST_OPTIONAL_HPP
 
 #include <utility>
 
@@ -13,8 +13,13 @@
  * isn't nearly as graceful.
  */
 template <typename T>
-struct optional
+class optional
 {
+ typedef optional<T> this_type;
+ typedef T* this_type::* unspecified_bool_type;
+public:
+ typedef T value_type;
+
     // Does not initialize the optional.
     optional()
         : _ptr(0)
@@ -42,12 +47,18 @@
     optional& operator=(optional const& x)
     { return swap(optional(x)); }
 
- optional& operator=(optional&& x)
+ inline optional& operator=(optional&& x)
     { return swap(optional().swap(x)); }
 
- optional& operator=(T const& x)
+ inline optional& operator=(T const& x)
     { return swap(optional(x)); }
 
+ inline bool operator==(optional const& x) const
+ { return get() != x.get(); }
+
+ inline bool operator!=(optional const& x) const
+ { return !(*this == x); }
+
     T& operator*()
     { return get(); }
 
@@ -60,8 +71,8 @@
     T const* operator->() const
     { return _ptr; }
 
- operator bool() const
- { return _ptr != 0; }
+ operator unspecified_bool_type() const
+ { return _ptr ? &this_type::_ptr : 0; }
 
     bool valid() const
     { return _ptr != 0; }
@@ -115,6 +126,83 @@
     T* _ptr;
 };
 
-}
+/**
+ * Specialize the optional type for pointers. This basically removes the
+ * encoding of the object into the character buffer. The reason that this is
+ * specilized on pointers is that the null pointer is a built-in indicator of
+ * optionality. If null, the pointer does not exist.
+ *
+ * The purpose of this overload is to make optional ptrs work mostly just like
+ * regular pointers.
+ *
+ * @todo Should this include ptr math to make it look like an iterable ptr?
+ */
+template <typename T>
+class optional<T*>
+{
+ typedef optional<T*> this_type;
+ typedef T* this_type::* unspecified_bool_type;
+public:
+ typedef T* value_type;
+
+ optional() : _ptr(0) { }
+ optional(optional const& x) : _ptr(x._ptr) { }
+ optional(optional&& x) : _ptr(std::move(x._ptr)) { }
+ optional(T* x) : _ptr(x) { }
+
+ optional& operator=(optional const& x)
+ { return swap(optional(x)); }
+
+ optional& operator=(optional&& x)
+ { return swap(x); }
+
+ optional& operator=(T* x)
+ { return swap(optional(x)); }
+
+ inline bool operator==(optional const& x) const
+ { return _ptr == x._ptr; }
+
+ inline bool operator!=(optional const& x) const
+ { return _ptr != x._ptr; }
+
+ T& operator*()
+ { return get(); }
+
+ T const& operator*() const
+ { return get(); }
+
+ T* operator->()
+ { return _ptr; }
+
+ T const* operator->() const
+ { return _ptr; }
+
+ operator unspecified_bool_type() const
+ { return _ptr ? &this_type::_ptr : 0; }
+
+ bool valid() const
+ { return _ptr != 0; }
+
+ T& get()
+ { return *_ptr; }
+
+ T const& get() const
+ { return *_ptr; }
+
+ // Reset the internal pointer to indicate uninitialized.
+ void reset()
+ { _ptr = 0; }
+
+ optional& swap(optional&& x)
+ {
+ using std::swap;
+ swap(_ptr, x._ptr);
+ return *this;
+ }
+
+ T* _ptr;
+};
+
+} /* namespace boost */
 
 #endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk