IMP  2.0.1
The Integrative Modeling Platform
base/WeakPointer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/WeakPointer.h
3  * \brief A nullptr-initialized pointer to an Object.
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_WEAK_POINTER_H
10 #define IMPBASE_WEAK_POINTER_H
11 #include <IMP/base/base_config.h>
12 #include "internal/PointerBase.h"
13 
14 
15 IMPBASE_BEGIN_NAMESPACE
16 
17 //! A weak pointer to an IMP::Object or IMP::RefCountedObject.
18 /** WeakPointers do not do reference counting and do not claim ownership
19  of the pointed object. As a result, they can be used to break cycles
20  in reference counted pointers. For example, since an IMP::Model
21  contains a reference counted pointer to an IMP::Particle, the
22  IMP::Particle has a WeakPointer back to the Model.
23 
24  The UncheckedWeakPointer can act on types that have only been
25  partially defined. You probably should use a WeakPointer instead
26  if you don't have problems with it.
27 
28  \see WeakPointer
29 
30  \param[in] O The type of IMP::Object-derived object to point to
31  */
32 template <class O>
34  internal::PointerBase<internal::WeakPointerTraits<O> > {
35  typedef internal::PointerBase<internal::WeakPointerTraits<O> > P;
36  template <class Any>
37  UncheckedWeakPointer(const Any &o): P(o){}
39  template <class OT>
40  UncheckedWeakPointer<O>& operator=( const internal::PointerBase<OT> &o){
41  P::operator=(o);
42  return *this;
43  }
44  template <class OT>
45  UncheckedWeakPointer<O>& operator=( OT* o){
46  P::operator=(o);
47  return *this;
48  }
49 #if !IMP_COMPILER_HAS_NULLPTR
50  UncheckedWeakPointer<O>& operator=(nullptr_t o) {
51  P::operator=(o);
52  return *this;
53  }
54 #endif
55  UncheckedWeakPointer<O>& operator=(const P &o) {
56  P::operator=(o);
57  return *this;
58  }
59 };
60 
61 /** WeakPointers do not do reference counting and do not claim ownership
62  of the pointed object. As a result, they can be used to break cycles
63  in reference counted pointers. For example, since an IMP::Model
64  contains a reference counted pointer to an IMP::Particle, the
65  IMP::Particle has a WeakPointer back to the Model.
66 
67  This version of a WeakPointer only works on complete types, but adds
68  additional checks of correct usage (eg that the Object has not bee
69  previously freed) compared to UncheckedWeakPointer.
70 
71  \see UncheckedWeakPointer
72  */
73 template <class O>
74 struct WeakPointer:
75  internal::PointerBase<internal::CheckedWeakPointerTraits<O> > {
76  typedef internal::PointerBase<internal::CheckedWeakPointerTraits<O> > P;
77  template <class Any>
78  WeakPointer(const Any &o): P(o){}
79  WeakPointer(){}
80  template <class OT>
81  WeakPointer<O>& operator=( const internal::PointerBase<OT> &o){
82  P::operator=(o);
83  return *this;
84  }
85  template <class OT>
86  WeakPointer<O>& operator=( OT* o){
87  P::operator=(o);
88  return *this;
89  }
90 #if !IMP_COMPILER_HAS_NULLPTR
91  WeakPointer<O>& operator=(nullptr_t o) {
92  P::operator=(o);
93  return *this;
94  }
95 #endif
96  WeakPointer<O>& operator=(const P &o) {
97  P::operator=(o);
98  return *this;
99  }
100 };
101 
102 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
103 template <class T>
104 inline std::ostream &operator<<(std::ostream &out, UncheckedWeakPointer<T> o) {
105  out << Showable(o.get());
106  return out;
107 }
108 template <class T>
109 inline std::ostream &operator<<(std::ostream &out, WeakPointer<T> o) {
110  out << Showable(o.get());
111  return out;
112 }
113 #endif
114 
115 
116 IMPBASE_END_NAMESPACE
117 
118 #endif /* IMPBASE_WEAK_POINTER_H */