2ccce3f835c74584a0e10647b1485cda69ee015c
[platform/upstream/libzypp.git] / zypp / detail / ResObjectFactory.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/detail/ResObjectFactory.h
10  *
11 */
12 #ifndef ZYPP_DETAIL_RESOBJECTFACTORY_H
13 #define ZYPP_DETAIL_RESOBJECTFACTORY_H
14
15 #include "zypp/base/PtrTypes.h"
16
17 ///////////////////////////////////////////////////////////////////
18 namespace zypp
19 { /////////////////////////////////////////////////////////////////
20
21   class Edition;
22   class Arch;
23
24   ///////////////////////////////////////////////////////////////////
25   namespace detail
26   { /////////////////////////////////////////////////////////////////
27     ///////////////////////////////////////////////////////////////////
28     namespace _resobjectfactory_detail
29     { /////////////////////////////////////////////////////////////////
30
31       template<class _Res>
32         class ResImplConnect : public _Res
33         {
34         public:
35           typedef ResImplConnect                  Self;
36           typedef typename _Res::Impl             Impl;
37           typedef base::shared_ptr<Impl>          ImplPtr;
38           typedef base::intrusive_ptr<Self>       Ptr;
39           typedef base::intrusive_ptr<const Self> constPtr;
40         public:
41           /** \todo protect against NULL Impl. */
42           ResImplConnect( const std::string & name_r,
43                           const Edition & edition_r,
44                           const Arch & arch_r,
45                           ImplPtr impl_r )
46           : _Res( name_r, edition_r, arch_r )
47           , _impl( impl_r )
48           { _impl->_backRef = this; }
49           virtual ~ResImplConnect()
50           { _impl->_backRef = 0; }
51         private:
52           ImplPtr _impl;
53           virtual Impl &       pimpl()       { return *_impl; }
54           virtual const Impl & pimpl() const { return *_impl; }
55         };
56
57     /////////////////////////////////////////////////////////////////
58     } // namespace _resobjectfactory
59     ///////////////////////////////////////////////////////////////////
60
61     template<class _Impl>
62       typename _Impl::ResType::Ptr
63       makeResolvableAndImpl( const std::string & name_r,
64                              const Edition & edition_r,
65                              const Arch & arch_r,
66                              base::shared_ptr<_Impl> & impl_r )
67       {
68         impl_r.reset( new _Impl );
69         return new
70                _resobjectfactory_detail::ResImplConnect<typename _Impl::ResType>
71                ( name_r, edition_r, arch_r, impl_r );
72       }
73
74     template<class _Impl>
75       typename _Impl::ResType::Ptr
76       makeResolvableFromImpl( const std::string & name_r,
77                               const Edition & edition_r,
78                               const Arch & arch_r,
79                               base::shared_ptr<_Impl> impl_r )
80       {
81         if ( ! impl_r )
82           throw ( "makeResolvableFromImpl: NULL Impl " );
83         if ( impl_r->self() )
84           throw ( "makeResolvableFromImpl: Impl already managed" );
85         return new
86                _resobjectfactory_detail::ResImplConnect<typename _Impl::ResType>
87                ( name_r, edition_r, arch_r, impl_r );
88       }
89
90     /////////////////////////////////////////////////////////////////
91   } // namespace detail
92   ///////////////////////////////////////////////////////////////////
93   /////////////////////////////////////////////////////////////////
94 } // namespace zypp
95 ///////////////////////////////////////////////////////////////////
96 #endif // ZYPP_DETAIL_RESOBJECTFACTORY_H