IMP logo
IMP Reference Guide  2.10.1
The Integrative Modeling Platform
WeakPointer.h
Go to the documentation of this file.
1 /**
2  * \file IMP/WeakPointer.h
3  * \brief A nullptr-initialized pointer to an Object.
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPKERNEL_WEAK_POINTER_H
10 #define IMPKERNEL_WEAK_POINTER_H
11 #include <IMP/kernel_config.h>
12 #include "internal/PointerBase.h"
13 
14 IMPKERNEL_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::Model
20  contains a reference counted pointer to an IMP::Particle, the
21  IMP::Particle has a WeakPointer back to the IMP::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  : IMP::internal::PointerBase<IMP::internal::WeakPointerTraits<O> > {
34  typedef IMP::internal::PointerBase<IMP::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 IMP::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  !defined(nullptr)
51  UncheckedWeakPointer<O>& operator=(nullptr_t o) {
52  P::operator=(o);
53  return *this;
54  }
55 #endif
56  UncheckedWeakPointer<O>& operator=(const P& o) {
57  P::operator=(o);
58  return *this;
59  }
60 };
61 
62 //! Smart pointer to Object-derived classes that does not refcount.
63 /** WeakPointers do not do reference counting and do not claim ownership
64  of the pointed object. As a result, they can be used to break cycles
65  in reference counted pointers. For example, since an IMP::Model
66  contains a reference counted pointer to an IMP::Particle, the
67  IMP::Particle has a WeakPointer back to the Model.
68 
69  This version of a WeakPointer only works on complete types, but adds
70  additional checks of correct usage (eg that the Object has not been
71  previously freed) compared to UncheckedWeakPointer.
72 
73  \see UncheckedWeakPointer
74  */
75 template <class O>
77  : IMP::internal::PointerBase<IMP::internal::CheckedWeakPointerTraits<O> > {
78  typedef IMP::internal::PointerBase<IMP::internal::CheckedWeakPointerTraits<O> > P;
79  template <class Any>
80  WeakPointer(const Any& o)
81  : P(o) {}
82  WeakPointer() {}
83  template <class OT>
84  WeakPointer<O>& operator=(const IMP::internal::PointerBase<OT>& o) {
85  P::operator=(o);
86  return *this;
87  }
88  template <class OT>
89  WeakPointer<O>& operator=(OT* o) {
90  P::operator=(o);
91  return *this;
92  }
93 #if(defined(BOOST_NO_CXX11_NULLPTR) || defined(BOOST_NO_NULLPTR)) && \
94  !defined(nullptr)
95  WeakPointer<O>& operator=(nullptr_t o) {
96  P::operator=(o);
97  return *this;
98  }
99 #endif
100  WeakPointer<O>& operator=(const P& o) {
101  P::operator=(o);
102  return *this;
103  }
104 };
105 
106 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
107 template <class T>
108 inline std::ostream& operator<<(std::ostream& out, UncheckedWeakPointer<T> o) {
109  out << Showable(o.get());
110  return out;
111 }
112 template <class T>
113 inline std::ostream& operator<<(std::ostream& out, WeakPointer<T> o) {
114  out << Showable(o.get());
115  return out;
116 }
117 #endif
118 
119 IMPKERNEL_END_NAMESPACE
120 
121 #endif /* IMPKERNEL_WEAK_POINTER_H */
Smart pointer to Object-derived classes that does not refcount.
Definition: WeakPointer.h:76
A weak pointer to an Object or RefCountedObject.
Definition: WeakPointer.h:32