- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Utils/Containers/SharedPointer.h

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00010
    00011 #ifndef MERS_UTILS_CONTAINERS_SHAREDPOINTER_H
    00012 #define MERS_UTILS_CONTAINERS_SHAREDPOINTER_H
    00013
    00014 #include <boost/shared_ptr.hpp>
    00015 #include <ostream>
    00016
    00017 namespace Mers {
    00018 namespace Utils {
    00019 namespace Containers {
    00020
    00026         class DynamicCastTag {};
    00027
    00035         template< class t_Object >
    00036         class SharedPointer
    00037         {
    00038         private:
    00040                 template< class to_Object > friend class SharedPointer;
    00041
    00042         public:
    00044                 typedef ::boost::shared_ptr< t_Object > t_Implementation;
    00046                 typedef typename t_Implementation::pointer t_Pointer;
    00048                 typedef typename t_Implementation::reference t_Reference;
    00049
    00050         public:
    00055                 inline SharedPointer() : m_Pointer() {
    00056                 }
    00057
    00066                 template< class to_Object >
    00067                 inline explicit SharedPointer( to_Object * obj )
    00068                         : m_Pointer( obj ) {
    00069                 }
    00070
    00084                 template< class to_Object, class t_Deleter >
    00085                 inline SharedPointer( to_Object * obj, t_Deleter deleter )
    00086                         : m_Pointer( obj, deleter ) {
    00087                 }
    00088
    00089                 // Default copy constructor, operator=, and destructor are fine.
    00090
    00099                 template< class to_Object >
    00100                 inline SharedPointer( SharedPointer< to_Object > const & other )
    00101                         : m_Pointer( other.m_Pointer ) {
    00102                 }
    00103
    00112                 template< class to_Object >
    00113                 inline SharedPointer( SharedPointer< to_Object > const & other,
    00114                         DynamicCastTag )
    00115                         : m_Pointer( other.m_Pointer, ::boost::detail::dynamic_cast_tag() ) {
    00116                 }
    00117
    00125                 template< class to_Object > inline
    00126                 SharedPointer & operator = ( SharedPointer< to_Object > const & other )
    00127                 {
    00128                         m_Pointer = other.m_Pointer;
    00129                         return *this;
    00130                 }
    00131
    00136                 inline void reset() {
    00137                         m_Pointer.reset();
    00138                 }
    00139
    00146                 template< class to_Object >
    00147                 inline void set( to_Object * obj ) {
    00148                         m_Pointer.reset( obj );
    00149                 }
    00150
    00161                 template< class to_Object, class t_Deleter >
    00162                 inline void set( to_Object * obj, t_Deleter deleter ) {
    00163                         m_Pointer.reset( obj, deleter );
    00164                 }
    00165
    00171                 inline t_Reference operator * () const {
    00172                         return *m_Pointer;
    00173                 }
    00174
    00180                 inline t_Pointer operator -> () const {
    00181                         return m_Pointer.get();
    00182                 }
    00183
    00191                 inline t_Pointer get() const {
    00192                         return m_Pointer.get();
    00193                 }
    00194
    00201                 inline operator t_Pointer () const {
    00202                         return m_Pointer.get();
    00203                 }
    00204
    00211                 inline operator bool () const {
    00212                         return m_Pointer.get() != NULL;
    00213                 }
    00214
    00221                 inline bool operator !() const {
    00222                         return m_Pointer.get() == NULL;
    00223                 }
    00224
    00233                 inline bool unique() const {
    00234                         return m_Pointer.unique();
    00235                 }
    00236
    00241                 inline size_t use_count() const {
    00242                         return m_Pointer.use_count();
    00243                 }
    00244
    00251                 inline void swap( SharedPointer< t_Object > & other ) {
    00252                         m_Pointer.swap( other.m_Pointer );
    00253                 }
    00254
    00258                 template< class to_Object >
    00259                 inline bool operator ==( SharedPointer< to_Object > const & other ) const {
    00260                         return m_Pointer == other.m_Pointer;
    00261                 }
    00262
    00266                 template< class to_Object >
    00267                 inline bool operator !=( SharedPointer< to_Object > const & other ) const {
    00268                         return m_Pointer != other.m_Pointer;
    00269                 }
    00270
    00274                 template< class to_Object >
    00275                 inline bool operator <( SharedPointer< to_Object > const & other ) const {
    00276                         return m_Pointer < other.m_Pointer;
    00277                 }
    00278
    00286                 template< class E, class T >
    00287                 friend inline std::basic_ostream< E, T > &
    00288                 operator << (std::basic_ostream< E, T > & os,
    00289                         SharedPointer< t_Object > const & pointer )
    00290                 {
    00291                         return (os << pointer.m_Pointer);
    00292                 }
    00293
    00294         protected:
    00297                 t_Implementation m_Pointer;
    00298         };
    00299
    00309         template< class t_Object > inline
    00310         void swap( SharedPointer< t_Object > & p1, SharedPointer< t_Object > & p2 ) {
    00311                 p1.swap( p2 );
    00312         }
    00313
    00328         template< class t_NewObject, class t_OldObject >
    00329         SharedPointer< t_NewObject >
    00330   dynamic_pointer_cast( SharedPointer< t_OldObject > const & oldPointer ) {
    00331                 return SharedPointer< t_NewObject >( oldPointer,
    00332                         DynamicCastTag() );
    00333         }
    00334
    00335 } // namespace Containers
    00336 } // namespace Utils
    00337 } // namespace Mers
    00338
    00339
    00340 #endif