IMP  2.1.0
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 IMPBASE_BEGIN_NAMESPACE
15 
16 //! A weak pointer to an Object or RefCountedObject.
17 /** WeakPointers do not do reference counting and do not claim ownership
18  of the pointed object. As a result, they can be used to break cycles
19  in reference counted pointers. For example, since an IMP::kernel::Model
20  contains a reference counted pointer to an IMP::kernel::Particle, the
21  IMP::kernel::Particle has a WeakPointer back to the IMP::kernel::Model.
22 
23  The UncheckedWeakPointer can act on types that have only been
24  partially defined. You probably should use a WeakPointer instead
25  if you don't have problems with it.
26 
27  \see WeakPointer
28 
29  \param[in] O The type of IMP::Object-derived object to point to
30  */
31 template <class O>
33  : internal::PointerBase<internal::WeakPointerTraits<O> > {
34  typedef internal::PointerBase<internal::WeakPointerTraits<O> > P;
35  template <class Any>
36  UncheckedWeakPointer(const Any& o)
37  : 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 defined(BOOST_NO_CXX11_NULLPTR) || defined(BOOST_NO_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::kernel::Model
64  contains a reference counted pointer to an IMP::kernel::Particle, the
65  IMP::kernel::Particle has a WeakPointer back to the kernel::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>
75  : internal::PointerBase<internal::CheckedWeakPointerTraits<O> > {
76  typedef internal::PointerBase<internal::CheckedWeakPointerTraits<O> > P;
77  template <class Any>
78  WeakPointer(const Any& o)
79  : P(o) {}
80  WeakPointer() {}
81  template <class OT>
82  WeakPointer<O>& operator=(const internal::PointerBase<OT>& o) {
83  P::operator=(o);
84  return *this;
85  }
86  template <class OT>
87  WeakPointer<O>& operator=(OT* o) {
88  P::operator=(o);
89  return *this;
90  }
91 #if defined(BOOST_NO_CXX11_NULLPTR) || defined(BOOST_NO_NULLPTR)
92  WeakPointer<O>& operator=(nullptr_t o) {
93  P::operator=(o);
94  return *this;
95  }
96 #endif
97  WeakPointer<O>& operator=(const P& o) {
98  P::operator=(o);
99  return *this;
100  }
101 };
102 
103 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
104 template <class T>
105 inline std::ostream& operator<<(std::ostream& out, UncheckedWeakPointer<T> o) {
106  out << Showable(o.get());
107  return out;
108 }
109 template <class T>
110 inline std::ostream& operator<<(std::ostream& out, WeakPointer<T> o) {
111  out << Showable(o.get());
112  return out;
113 }
114 #endif
115 
116 IMPBASE_END_NAMESPACE
117 
118 #endif /* IMPBASE_WEAK_POINTER_H */
A weak pointer to an Object or RefCountedObject.