Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / base / base.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
12 *
13 */
14 #ifndef ZYPP_NG_BASE_BASE_H_INCLUDED
15 #define ZYPP_NG_BASE_BASE_H_INCLUDED
16
17 #include <zypp/zyppng/base/zyppglobal.h>
18 #include <zypp/zyppng/base/signals.h>
19 #include <memory>
20 #include <unordered_set>
21 #include <vector>
22
23 namespace zyppng {
24
25   class BasePrivate;
26
27   /*!
28    * The Base class is used as a common base class for objects that emit signals,
29    * it also supports a parent/child relationship where the parent object tracks keeps
30    * a reference for all its children.
31    */
32   class LIBZYPP_NG_EXPORT Base : public sigc::trackable, public std::enable_shared_from_this<Base>
33   {
34     NON_COPYABLE(Base);
35     ZYPP_DECLARE_PRIVATE(Base)
36   public:
37
38     using Ptr = std::shared_ptr<Base>;
39     using WeakPtr = std::weak_ptr<Base>;
40
41     Base ();
42     virtual ~Base();
43
44     /*!
45      * Returns the parent object if there is one, otherwise
46      * returns a zero WeakPtr
47      */
48     WeakPtr parent() const;
49
50     /*!
51      * Adds a new object to the child list, the object
52      * will keep a reference for its entire lifetime or until the object is removed
53      */
54     void addChild ( Base::Ptr child );
55
56     /*!
57      * Removes a child object from the internal child list
58      */
59     void removeChild (Ptr child );
60
61     /*!
62      * Returns all child objects of this object
63      */
64     const std::unordered_set<Ptr> &children() const;
65
66     /*!
67      * Returns all children that can be casted to type T
68      */
69     template<typename T>
70     std::vector< std::weak_ptr<T> > findChildren () const {
71       std::vector< std::weak_ptr<T> > result;
72       for ( Ptr p : children() ) {
73         std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(p);
74         if ( casted )
75           result.push_back( std::weak_ptr<T>(casted) );
76       }
77       return result;
78     }
79
80     template<typename T>
81     inline std::shared_ptr<T> shared_this () const {
82       return std::static_pointer_cast<T>( shared_from_this() );
83     }
84
85     template<typename T>
86     inline std::shared_ptr<T> shared_this () {
87       return std::static_pointer_cast<T>( shared_from_this() );
88     }
89
90     template<typename T>
91     inline std::weak_ptr<T> weak_this () const {
92       return std::static_pointer_cast<T>( weak_from_this().lock() );
93     }
94
95     template<typename T>
96     inline std::weak_ptr<T> weak_this () {
97       return std::static_pointer_cast<T>( weak_from_this().lock() );
98     }
99
100   protected:
101     Base ( BasePrivate &dd );
102     std::unique_ptr<BasePrivate> d_ptr;
103   };
104
105 } // namespace zyppng
106
107 #endif // ZYPP_NG_CORE_BASE_H_INCLUDED