SundanceOrderedHandle.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #ifndef SUNDANCEORDEREDHANDLE_HPP
00032 #define SUNDANCEORDEREDHANDLE_HPP
00033 
00034 #include "SundanceDefs.hpp"
00035 #include "PlayaHandle.hpp"
00036 #include <typeinfo>
00037 
00038 
00039 #define ORDERED_HANDLE_CTORS(handle, contents) \
00040 /** Empty ctor */ \
00041 handle() : OrderedHandle<contents >() {;} \
00042 /** Construct a #handle with a raw pointer to a #contents */ \
00043 handle(Playa::Handleable<contents >* rawPtr) : OrderedHandle<contents >(rawPtr) {;} \
00044 /** Construct a #handle with a smart pointer to a #contents */ \
00045 handle(const RCP<contents >& smartPtr) : OrderedHandle<contents >(smartPtr){;}
00046 
00047 
00048 
00049 
00050 namespace Sundance
00051 {
00052   using namespace Teuchos;
00053 
00054   /**
00055    * Class OrderedHandle is an extension to Playa::Handle that 
00056    * includes a comparison operator ("<" operator) so that
00057    * the handle can be used in ordered containers such as STL maps and sets.
00058    */
00059   template <class PointerType>
00060   class OrderedHandle : public Playa::Handle<PointerType>
00061   {
00062   public:
00063     /** empty ctor */
00064     OrderedHandle() : Playa::Handle<PointerType>() {;}
00065 
00066     /** Construct from a raw ptr */
00067     OrderedHandle(Playa::Handleable<PointerType>* rawPtr) : Playa::Handle<PointerType>(rawPtr) {;}
00068 
00069     /** Construct from a smart ptr*/
00070     OrderedHandle(const RCP<PointerType>& smartPtr) 
00071       : Playa::Handle<PointerType>(smartPtr) {;}
00072 
00073     /** comparison operator */
00074     bool operator<(const OrderedHandle<PointerType>& other) const 
00075     {
00076       /* first compare types */
00077       const PointerType* me = this->ptr().get();
00078       const PointerType* you = other.ptr().get();
00079       if (me==0 && you==0) 
00080         {
00081           return false;
00082         }
00083       if (me==0) 
00084         {
00085           return true;
00086         }
00087       if (you==0) 
00088         {
00089           return false;
00090         }
00091 
00092       if (typeid(*me).before(typeid(*you))) 
00093         {
00094           return true;
00095         }
00096 
00097       if (typeid(*you).before(typeid(*me)))
00098         {
00099           return false;
00100         }
00101       
00102       /* if the types are equal, compare values of the contents using
00103        * the lessThan() method. */
00104       bool rtn = this->ptr()->lessThan(other.ptr().get());
00105       return rtn;
00106     }
00107 
00108     /** */
00109     bool operator!=(const OrderedHandle<PointerType>& other) const
00110       {
00111         return (*this < other) || (other < *this);
00112       }
00113 
00114 
00115     /** */
00116     bool operator==(const OrderedHandle<PointerType>& other) const
00117       {
00118         return !(*this != other);
00119       }
00120 
00121     
00122   };
00123 }
00124 
00125 namespace std
00126 {
00127   /** \relates OrderedHandle */
00128   template <class P> inline 
00129   std::ostream& operator<<(std::ostream& os, 
00130                            const Sundance::OrderedHandle<P>& h)
00131   {
00132     h.print(os);
00133     return os;
00134   }
00135 }
00136 
00137 
00138 
00139 
00140 #endif
00141 

Site Contact