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