libzypp  17.22.0
base.h
Go to the documentation of this file.
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 
19 #include <memory>
20 #include <unordered_set>
21 #include <vector>
22 
23 namespace zyppng {
24 
25  class BasePrivate;
26 
32  class LIBZYPP_NG_EXPORT Base : public sigc::trackable, public std::enable_shared_from_this<Base>
33  {
36  public:
37 
38  using Ptr = std::shared_ptr<Base>;
39  using WeakPtr = std::weak_ptr<Base>;
40 
41  Base ();
42  virtual ~Base();
43 
48  WeakPtr parent() const;
49 
54  void addChild ( Base::Ptr child );
55 
59  void removeChild (Ptr child );
60 
64  const std::unordered_set<Ptr> &children() const;
65 
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
std::weak_ptr< Base > WeakPtr
Definition: base.h:39
std::shared_ptr< T > shared_this()
Definition: base.h:86
std::vector< std::weak_ptr< T > > findChildren() const
Definition: base.h:70
#define NON_COPYABLE(CLASS)
Delete copy ctor and copy assign.
Definition: Easy.h:59
std::weak_ptr< T > weak_this() const
Definition: base.h:91
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:21
#define LIBZYPP_NG_EXPORT
Definition: zyppglobal.h:7
std::unique_ptr< BasePrivate > d_ptr
Definition: base.h:102
std::weak_ptr< T > weak_this()
Definition: base.h:96
std::shared_ptr< T > shared_this() const
Definition: base.h:81
std::shared_ptr< Base > Ptr
Definition: base.h:38