libzypp 17.38.15

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>
18#include <zypp-core/ng/base/zyppglobal.h>
19#include <zypp-core/ng/base/signals.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>
32 struct MemberFunction;
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 {
62 NON_COPYABLE(Base);
63 ZYPP_DECLARE_PRIVATE(Base)
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
AutoDispose.h
zypp::AutoDispose
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
zyppng::BasePrivate
Definition base_p.h:14
zyppng::Base
Definition base.h:61
zyppng::Base::weak_this
std::weak_ptr< T > weak_this()
Definition base.h:129
zyppng::Base::removeChild
void removeChild(const Ptr &child)
Definition base.cc:52
zyppng::Base::connectFunc
std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > connectFunc(SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
Definition base.h:169
zyppng::Base::shared_this
std::shared_ptr< T > shared_this() const
Definition base.h:114
zyppng::Base::threadId
std::thread::id threadId() const
Definition base.cc:71
zyppng::Base::clearData
void clearData(uint32_t quark)
Definition base.cc:90
zyppng::Base::findChildren
std::vector< std::weak_ptr< T > > findChildren() const
Definition base.h:103
zyppng::Base::shared_this
std::shared_ptr< T > shared_this()
Definition base.h:119
zyppng::Base::WeakPtr
std::weak_ptr< Base > WeakPtr
Definition base.h:67
zyppng::Base::connectFunc
static auto connectFunc(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
Definition base.h:164
zyppng::Base::setData
void setData(uint32_t quark, zypp::AutoDispose< void * > data)
Definition base.cc:76
zyppng::Base::children
const std::unordered_set< Ptr > & children() const
Definition base.cc:66
zyppng::Base::data
void * data(uint32_t quark)
Definition base.cc:82
zyppng::Base::addChild
void addChild(const Base::Ptr &child)
Definition base.cc:31
zyppng::Base::make_base_slot
static decltype(auto) make_base_slot(Obj *o, Functor &&f)
Definition base.h:134
zyppng::Base::parent
WeakPtr parent() const
Definition base.cc:26
zyppng::Base::Ptr
std::shared_ptr< Base > Ptr
Definition base.h:66
zyppng::Base::connect
static auto connect(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
Definition base.h:143
zyppng::Base::weak_this
std::weak_ptr< T > weak_this() const
Definition base.h:124
zyppng::Base::Base
Base()
Definition base.cc:20
zyppng::Base::NON_COPYABLE
NON_COPYABLE(Base)
zyppng::Base::connect
auto connect(SenderFunc &&sFun, typename internal::MemberFunction< ReceiverFunc >::ClassType &recv, ReceiverFunc &&rFunc)
Definition base.h:155
zyppng::Base::d_ptr
std::unique_ptr< BasePrivate > d_ptr
Definition base.h:182
internal
Definition OptionalDownloadProgressReport.cc:12
std::enable_if_t
typename enable_if< B, T >::type enable_if_t
Definition TypeTraits.h:45
std::is_base_of_v
constexpr bool is_base_of_v
Definition TypeTraits.h:27
std::invoke
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition functional.h:32
zyppng::internal::locking_fun
decltype(auto) locking_fun(const Functor &f, const Obj &... o)
Definition signals.h:316
zyppng::internal::is_base_receiver
constexpr bool is_base_receiver
Definition base.h:40
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::connection
sigc::connection connection
Definition signals.h:180
zyppng::base_slot
decltype(auto) base_slot(Obj *o, Functor &&f)
Definition base.h:189
signals.h
zyppng::internal::MemberFunction< Ret(BType::*)(Args...)>::ClassType
BType ClassType
Definition base.h:36
zyppng::internal::MemberFunction
Definition base.h:32
zyppglobal.h
ZYPP_DECLARE_PRIVATE
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:80