1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \----------------------------------------------------------------------/
10 * This file contains private API, this might break at any time between releases.
11 * You have been warned!
14 #ifndef ZYPP_NG_BASE_BASE_H_INCLUDED
15 #define ZYPP_NG_BASE_BASE_H_INCLUDED
17 #include <zypp/zyppng/base/zyppglobal.h>
18 #include <zypp/zyppng/base/signals.h>
20 #include <unordered_set>
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.
32 class LIBZYPP_NG_EXPORT Base : public sigc::trackable, public std::enable_shared_from_this<Base>
35 ZYPP_DECLARE_PRIVATE(Base)
38 using Ptr = std::shared_ptr<Base>;
39 using WeakPtr = std::weak_ptr<Base>;
45 * Returns the parent object if there is one, otherwise
46 * returns a zero WeakPtr
48 WeakPtr parent() const;
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
54 void addChild ( Base::Ptr child );
57 * Removes a child object from the internal child list
59 void removeChild (Ptr child );
62 * Returns all child objects of this object
64 const std::unordered_set<Ptr> &children() const;
67 * Returns all children that can be casted to type 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);
75 result.push_back( std::weak_ptr<T>(casted) );
81 inline std::shared_ptr<T> shared_this () const {
82 return std::static_pointer_cast<T>( shared_from_this() );
86 inline std::shared_ptr<T> shared_this () {
87 return std::static_pointer_cast<T>( shared_from_this() );
91 inline std::weak_ptr<T> weak_this () const {
92 return std::static_pointer_cast<T>( weak_from_this().lock() );
96 inline std::weak_ptr<T> weak_this () {
97 return std::static_pointer_cast<T>( weak_from_this().lock() );
101 Base ( BasePrivate &dd );
102 std::unique_ptr<BasePrivate> d_ptr;
105 } // namespace zyppng
107 #endif // ZYPP_NG_CORE_BASE_H_INCLUDED