option.h

Go to the documentation of this file.
00001 #ifndef option_HEADER
00002 #define option_HEADER
00003 
00004 /***************************************************************************
00005  * Benoit Hudson (C) 2007.
00006  ***************************************************************************/
00007 
00008 #include <assert.h>
00009 #include <boost/scoped_ptr.hpp>
00010 
00011 namespace hudson {
00012 using namespace boost;
00013 
00016 template <class T>
00017 struct option {
00018   option() { }
00019   option(const T& t) : t_(new T(t)) { }
00020   option(const option& other) { copy(other.t_); }
00021   option& operator=(const option& other) {
00022     copy(other.t_);
00023     return *this;
00024   }
00025 
00026   bool isSome() const { return t_.get() != NULL; }
00027   bool isNone() const { return t_.get() == NULL; }
00028   const T& valOf() const {
00029     assert(isSome());
00030     return *t_;
00031   }
00032   const T& operator*() const {
00033     return valOf();
00034   }
00035 
00036   private:
00037   void copy(const scoped_ptr<T>& other) {
00038     if (other.get()) {
00039       t_.reset(new T(*other));
00040     } else {
00041       t_.reset(0);
00042     }
00043   }
00044   scoped_ptr<T> t_;
00045 };
00046 
00047 } // namespace hudson
00048 #endif

Generated on Thu Mar 27 19:04:14 2008 by  doxygen 1.4.6