libzypp  17.38.7
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 
17 #include <zypp-core/AutoDispose.h>
20 #include <memory>
21 #include <unordered_set>
22 #include <vector>
23 #include <thread>
24 
25 namespace zyppng {
26 
27  class Base;
28  class BasePrivate;
29 
30  namespace internal {
31  template <typename Func>
33 
34  template <typename BType, typename Ret, typename ...Args >
35  struct MemberFunction<Ret (BType::*)( Args... )> {
36  using ClassType = BType;
37  };
38 
39  template <typename T>
40  inline constexpr bool is_base_receiver = std::is_base_of_v<Base, T> || std::is_base_of_v<BasePrivate, T>;
41 
42  }
43 
60  class Base : public sigc::trackable, public std::enable_shared_from_this<Base>
61  {
64  public:
65 
66  using Ptr = std::shared_ptr<Base>;
67  using WeakPtr = std::weak_ptr<Base>;
68 
69  Base ();
70  virtual ~Base();
71 
76  WeakPtr parent() const;
77 
82  void addChild ( const Base::Ptr& child );
83 
87  void removeChild (const Ptr& child );
88 
92  const std::unordered_set<Ptr> &children() const;
93 
97  std::thread::id threadId () const;
98 
102  template<typename T>
103  std::vector< std::weak_ptr<T> > findChildren () const {
104  std::vector< std::weak_ptr<T> > result;
105  for ( const Ptr& p : children() ) {
106  std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(p);
107  if ( casted )
108  result.push_back( std::weak_ptr<T>(casted) );
109  }
110  return result;
111  }
112 
113  template<typename T>
114  inline std::shared_ptr<T> shared_this () const {
115  return std::static_pointer_cast<T>( shared_from_this() );
116  }
117 
118  template<typename T>
119  inline std::shared_ptr<T> shared_this () {
120  return std::static_pointer_cast<T>( shared_from_this() );
121  }
122 
123  template<typename T>
124  inline std::weak_ptr<T> weak_this () const {
125  return std::static_pointer_cast<T>( weak_from_this().lock() );
126  }
127 
128  template<typename T>
129  inline std::weak_ptr<T> weak_this () {
130  return std::static_pointer_cast<T>( weak_from_this().lock() );
131  }
132 
133  template<typename Obj, typename Functor >
134  static decltype (auto) make_base_slot( Obj *o, Functor &&f ) {
135  //static_assert ( !internal::is_base_receiver<Obj>, "Can not make a slot for a Object that does not derive from Base or BasePrivate.");
136  return internal::locking_fun( sigc::mem_fun( o, std::forward<Functor>(f) ), *o );
137  }
138 
142  template< typename SenderFunc, typename ReceiverFunc >
143  static auto connect ( typename internal::MemberFunction<SenderFunc>::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction<ReceiverFunc>::ClassType &recv, ReceiverFunc &&rFunc ) {
144  return std::invoke( std::forward<SenderFunc>(sFun), &s ).connect( make_base_slot( &recv, std::forward<ReceiverFunc>(rFunc) ) );
145  }
146 
147 
154  template< typename SenderFunc, typename ReceiverFunc >
155  auto connect ( SenderFunc &&sFun, typename internal::MemberFunction<ReceiverFunc>::ClassType &recv, ReceiverFunc &&rFunc ) {
156  return connect( static_cast<typename internal::MemberFunction<SenderFunc>::ClassType &>(*this), std::forward<SenderFunc>(sFun), recv, std::forward<ReceiverFunc>(rFunc) );
157  }
158 
163  template< typename SenderFunc, typename ReceiverFunc, typename ...Tracker >
164  static auto connectFunc ( typename internal::MemberFunction<SenderFunc>::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker&...trackers ) {
165  return std::invoke( std::forward<SenderFunc>(sFun), &s ).connect( internal::locking_fun( std::forward<ReceiverFunc>(rFunc), trackers... ) );
166  }
167 
168  template< typename SenderFunc, typename ReceiverFunc, typename ...Tracker >
169  std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > connectFunc ( SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker&...trackers ) {
170  return connectFunc( static_cast<typename internal::MemberFunction<SenderFunc>::ClassType &>(*this), std::forward<SenderFunc>(sFun), std::forward<ReceiverFunc>(rFunc), trackers... );
171  }
172 
176  void setData ( uint32_t quark, zypp::AutoDispose<void *> data );
177  void* data ( uint32_t quark );
178  void clearData ( uint32_t quark );
179 
180  protected:
181  Base ( BasePrivate &dd );
182  std::unique_ptr<BasePrivate> d_ptr;
183  };
184 
185 
186  template<typename Obj, typename Functor,
187  std::enable_if_t< std::is_base_of_v< Base, Obj> || std::is_base_of_v<BasePrivate, Obj>, bool> = true
188  >
189  inline decltype(auto) base_slot( Obj *o, Functor &&f )
190  {
191  return Base::make_base_slot(o, std::forward<Functor>(f) );
192  }
193 
194  template<typename Obj, typename Functor,
195  std::enable_if_t< std::is_base_of_v< Base, Obj> || std::is_base_of_v<BasePrivate, Obj>, bool> = true
196  >
197  inline decltype(auto) base_slot( Obj &o, Functor &&f )
198  {
199  return Base::make_base_slot(&o, std::forward<Functor>(f) );
200  }
201 
202 } // namespace zyppng
203 
204 #endif // ZYPP_NG_CORE_BASE_H_INCLUDED
decltype(auto) base_slot(Obj *o, Functor &&f)
Definition: base.h:189
std::weak_ptr< Base > WeakPtr
Definition: base.h:67
NON_COPYABLE(Base)
void removeChild(const Ptr &child)
Definition: base.cc:52
std::thread::id threadId() const
Definition: base.cc:71
std::enable_if< std::is_member_pointer< typename std::decay< Functor >::type >::value, typename std::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition: functional.h:32
void addChild(const Base::Ptr &child)
Definition: base.cc:31
std::shared_ptr< T > shared_this()
Definition: base.h:119
void clearData(uint32_t quark)
Definition: base.cc:90
virtual ~Base()
Definition: base.cc:23
WeakPtr parent() const
Definition: base.cc:26
std::vector< std::weak_ptr< T > > findChildren() const
Definition: base.h:103
typename enable_if< B, T >::type enable_if_t
Definition: TypeTraits.h:45
static auto connect(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
Definition: base.h:143
std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > connectFunc(SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
Definition: base.h:169
std::weak_ptr< T > weak_this() const
Definition: base.h:124
const std::unordered_set< Ptr > & children() const
Definition: base.cc:66
static decltype(auto) make_base_slot(Obj *o, Functor &&f)
Definition: base.h:134
auto connect(SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
Definition: base.h:155
sigc::connection connection
Definition: signals.h:180
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:80
static auto connectFunc(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
Definition: base.h:164
constexpr bool is_base_receiver
Definition: base.h:40
void * data(uint32_t quark)
Definition: base.cc:82
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition: AutoDispose.h:94
void setData(uint32_t quark, zypp::AutoDispose< void *> data)
Definition: base.cc:76
decltype(auto) locking_fun(const Functor &f, const Obj &... o)
Definition: signals.h:316
std::unique_ptr< BasePrivate > d_ptr
Definition: base.h:182
sigc::trackable trackable
Definition: signals.h:181
std::weak_ptr< T > weak_this()
Definition: base.h:129
std::shared_ptr< T > shared_this() const
Definition: base.h:114
std::shared_ptr< Base > Ptr
Definition: base.h:66