Libzypp/Design/Pointer
From openSUSE
Pointers
The pointer handling in ZYPP is based on the smart pointer library of boost.
Boost pointers
(Taken from the boost documentation)
Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners.
Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed.
The smart pointer library provides five smart pointer class templates:
| scoped_ptr | <boost/scoped_ptr.hpp> | Simple sole ownership of single objects. Noncopyable. | |
| scoped_array | <boost/scoped_array.hpp> | Simple sole ownership of arrays. Noncopyable. | |
| shared_ptr | <boost/shared_ptr.hpp> | Object ownership shared among multiple pointers. | |
| shared_array | <boost/shared_array.hpp> | Array ownership shared among multiple pointers. | |
| weak_ptr | <boost/weak_ptr.hpp> | Non-owning observers of an object owned by shared_ptr. | |
| intrusive_ptr | <boost/intrusive_ptr.hpp> | Shared ownership of objects with an embedded reference count. |
These templates are designed to complement the std::auto_ptr template.
Zypp pointers
Each class defines pointer to it as typedefs. For class Foo the following typedefs are available
Foo_Ptr Foo_constPtr
Using a smart_pointer or Ptr-type as link to an implementation has the flaw, that both types in fact provide an Impl*.
* class Foo
* {
* ...
* private:
* // Implementation class
* struct Impl;
* // Pointer to implementation; actually a shared_ptr<Impl>
* base::RW_pointer<Impl> _pimpl;
*
* void baa() { _pimpl->... } // is Impl *
* void baa() const { _pimpl->... } // is Impl const *
Within a const method, a e.g. shared_pointer<Impl> _pimpl; will behave as Impl *const. I.e. the pointer is const, not the object it points to.
RW_pointer corrects this. Note that it's not really a pointer type, but a wrapper. Per default
RW_pointer<Impl> is RW_pointer<Impl,shared_pointer<Impl> >
but you can use it as well with an Impl_Ptr (an intrusive_pointer<Impl>). You just have to state this:
RW_pointer<Impl,Impl_Ptr>
Last edit in Trac '11/24/05 18:22:16' by 'kkaempf'
Last edit in Trac '11/24/05 18:22:16' by 'kkaempf'
Last edit in Trac '11/24/05 18:22:16' by 'kkaempf'
Last edit in Trac '11/24/05 18:22:16' by 'kkaempf'
Last edit in Trac '11/24/05 18:22:16' by 'kkaempf'

