IMP  2.4.0
The Integrative Modeling Platform
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-2015 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  !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 /** WeakPointers do not do reference counting and do not claim ownership
63  of the pointed object. As a result, they can be used to break cycles
64  in reference counted pointers. For example, since an IMP::kernel::Model
65  contains a reference counted pointer to an IMP::kernel::Particle, the
66  IMP::kernel::Particle has a WeakPointer back to the kernel::Model.
67 
68  This version of a WeakPointer only works on complete types, but adds
69  additional checks of correct usage (eg that the Object has not bee
70  previously freed) compared to UncheckedWeakPointer.
71 
72  \see UncheckedWeakPointer
73  */
74 template <class O>
76  : internal::PointerBase<internal::CheckedWeakPointerTraits<O> > {
77  typedef internal::PointerBase<internal::CheckedWeakPointerTraits<O> > P;
78  template <class Any>
79  WeakPointer(const Any& o)
80  : P(o) {}
81  WeakPointer() {}
82  template <class OT>
83  WeakPointer<O>& operator=(const internal::PointerBase<OT>& o) {
84  P::operator=(o);
85  return *this;
86  }
87  template <class OT>
88  WeakPointer<O>& operator=(OT* o) {
89  P::operator=(o);
90  return *this;
91  }
92 #if(defined(BOOST_NO_CXX11_NULLPTR) || defined(BOOST_NO_NULLPTR)) && \
93  !defined(nullptr)
94  WeakPointer<O>& operator=(nullptr_t o) {
95  P::operator=(o);
96  return *this;
97  }
98 #endif
99  WeakPointer<O>& operator=(const P& o) {
100  P::operator=(o);
101  return *this;
102  }
103 };
104 
105 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
106 template <class T>
107 inline std::ostream& operator<<(std::ostream& out, UncheckedWeakPointer<T> o) {
108  out << Showable(o.get());
109  return out;
110 }
111 template <class T>
112 inline std::ostream& operator<<(std::ostream& out, WeakPointer<T> o) {
113  out << Showable(o.get());
114  return out;
115 }
116 #endif
117 
118 IMPBASE_END_NAMESPACE
119 
120 #endif /* IMPBASE_WEAK_POINTER_H */
A weak pointer to an Object or RefCountedObject.
Definition: WeakPointer.h:32