Any time you store an Object in a C++ program, you should use a Pointer, rather than a raw C++ pointer. Using a pointer manages the reference counting and makes sure that the object is not deleted prematurely when, for example, all Python references go away and that it is deleted properly if an exception is thrown during the function. Use the IMP_NEW() macro to aid creation of pointers to new objects.
For example, when implementing a Restraint that uses a PairScore, store the PairScore like this:
Pointer<PairScore> ps_;
IMP_NEW(IMP::core::Harmonic, h, (0,1)); IMP::Pointer<IMP::em::DensityMap> map= IMP::em::read_map("file_name.mrc"); IMP_NEW(IMP::core::DistancePairScore, dps, (h));
IMP::Pointer<IMP::core::Harmonic> h= new IMP::core::Harmonic(0,1); IMP::Pointer<IMP::em::DensityMap> map= IMP::em::read_map("file_name.mrc"); IMP::Pointer<IMP::core::DistancePairScore> dps = new IMP::core::DistancePairScore(h);
h
is deleted.The object being pointed to must inherit from IMP::RefCountedObject. Use an IMP::WeakPointer to break cycles or to point to non-ref-counted objects.
[in] | O | The type of IMP::RefCounted-derived object to point to |
Public Member Functions | |
Pointer< O > & | operator= (O *o) |
Set it from a possibly NULL pointer. | |
Pointer & | operator= (const Pointer &o) |
Pointer (O *o) | |
Pointer () | |
initialize to NULL | |
Pointer (const Pointer &o) | |
O * | release () |
Relinquish control of the pointer. | |
~Pointer () |
IMP::Pointer< O >::Pointer | ( | const Pointer< O > & | o | ) |
copy constructor
IMP::Pointer< O >::Pointer | ( | O * | o | ) |
initialize from a pointer
IMP::Pointer< O >::~Pointer | ( | ) |
drop control of the object
Pointer& IMP::Pointer< O >::operator= | ( | const Pointer< O > & | o | ) |
copy from another
O* IMP::Pointer< O >::release | ( | ) |
Relinquish control of the pointer.
This must be the only pointer pointing to the object. Its reference count will be 0 after the function is called, but the object will not be destroyed. Use this to safely return objects allocated within functions.