libzypp 17.37.10

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/zyppng/base/zyppglobal.h>
18#include <zypp-core/zyppng/base/signals.h>
19#include <memory>
20#include <unordered_set>
21#include <vector>
22#include <thread>
23
24 namespace zyppng {
25
26 class Base;
27 class BasePrivate;
28
29 namespace internal {
30 template <typename Func>
31 struct MemberFunction;
32
33 template <typename BType, typename Ret, typename ...Args >
34 struct MemberFunction<Ret (BType::*)( Args... )> {
35 using ClassType = BType;
36 };
37
38 template <typename T>
39 inline constexpr bool is_base_receiver = std::is_base_of_v<Base, T> || std::is_base_of_v<BasePrivate, T>;
40
41 }
42
59 class LIBZYPP_NG_EXPORT Base : public sigc::trackable, public std::enable_shared_from_this<Base>
60 {
61 NON_COPYABLE(Base);
62 ZYPP_DECLARE_PRIVATE(Base)
63 public:
64
65 using Ptr = std::shared_ptr<Base>;
66 using WeakPtr = std::weak_ptr<Base>;
67
68 Base ();
69 virtual ~Base();
70
75 WeakPtr parent() const;
76
81 void addChild ( const Base::Ptr& child );
82
86 void removeChild (const Ptr& child );
87
91 const std::unordered_set<Ptr> &children() const;
92
96 std::thread::id threadId () const;
97
101 template<typename T>
102 std::vector< std::weak_ptr<T> > findChildren () const {
103 std::vector< std::weak_ptr<T> > result;
104 for ( const Ptr& p : children() ) {
105 std::shared_ptr<T> casted = std::dynamic_pointer_cast<T>(p);
106 if ( casted )
107 result.push_back( std::weak_ptr<T>(casted) );
108 }
109 return result;
110 }
111
112 template<typename T>
113 inline std::shared_ptr<T> shared_this () const {
114 return std::static_pointer_cast<T>( shared_from_this() );
115 }
116
117 template<typename T>
118 inline std::shared_ptr<T> shared_this () {
119 return std::static_pointer_cast<T>( shared_from_this() );
120 }
121
122 template<typename T>
123 inline std::weak_ptr<T> weak_this () const {
124 return std::static_pointer_cast<T>( weak_from_this().lock() );
125 }
126
127 template<typename T>
128 inline std::weak_ptr<T> weak_this () {
129 return std::static_pointer_cast<T>( weak_from_this().lock() );
130 }
131
132 template<typename Obj, typename Functor >
133 static decltype (auto) make_base_slot( Obj *o, Functor &&f ) {
134 //static_assert ( !internal::is_base_receiver<Obj>, "Can not make a slot for a Object that does not derive from Base or BasePrivate.");
135 return internal::locking_fun( sigc::mem_fun( o, std::forward<Functor>(f) ), *o );
136 }
137
141 template< typename SenderFunc, typename ReceiverFunc >
142 static auto connect ( typename internal::MemberFunction<SenderFunc>::ClassType &s, SenderFunc &&sFun, typename internal::MemberFunction<ReceiverFunc>::ClassType &recv, ReceiverFunc &&rFunc ) {
143 return std::invoke( std::forward<SenderFunc>(sFun), &s ).connect( make_base_slot( &recv, std::forward<ReceiverFunc>(rFunc) ) );
144 }
145
146
153 template< typename SenderFunc, typename ReceiverFunc >
154 auto connect ( SenderFunc &&sFun, typename internal::MemberFunction<ReceiverFunc>::ClassType &recv, ReceiverFunc &&rFunc ) {
155 return connect( static_cast<typename internal::MemberFunction<SenderFunc>::ClassType &>(*this), std::forward<SenderFunc>(sFun), recv, std::forward<ReceiverFunc>(rFunc) );
156 }
157
162 template< typename SenderFunc, typename ReceiverFunc, typename ...Tracker >
163 static auto connectFunc ( typename internal::MemberFunction<SenderFunc>::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker&...trackers ) {
164 return std::invoke( std::forward<SenderFunc>(sFun), &s ).connect( internal::locking_fun( std::forward<ReceiverFunc>(rFunc), trackers... ) );
165 }
166
167 template< typename SenderFunc, typename ReceiverFunc, typename ...Tracker >
168 std::enable_if_t< std::is_member_function_pointer_v< SenderFunc >, connection > connectFunc ( SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker&...trackers ) {
169 return connectFunc( static_cast<typename internal::MemberFunction<SenderFunc>::ClassType &>(*this), std::forward<SenderFunc>(sFun), std::forward<ReceiverFunc>(rFunc), trackers... );
170 }
171
172 protected:
173 Base ( BasePrivate &dd );
174 std::unique_ptr<BasePrivate> d_ptr;
175 };
176
177
178 template<typename Obj, typename Functor,
179 std::enable_if_t< std::is_base_of_v< Base, Obj> || std::is_base_of_v<BasePrivate, Obj>, bool> = true
180 >
181 inline decltype(auto) base_slot( Obj *o, Functor &&f )
182 {
183 return Base::make_base_slot(o, std::forward<Functor>(f) );
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} // namespace zyppng
195
196#endif // ZYPP_NG_CORE_BASE_H_INCLUDED
zyppng::BasePrivate
Definition base_p.h:14
zyppng::Base
Definition base.h:60
zyppng::Base::weak_this
std::weak_ptr< T > weak_this()
Definition base.h:128
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:168
zyppng::Base::shared_this
std::shared_ptr< T > shared_this() const
Definition base.h:113
zyppng::Base::threadId
std::thread::id threadId() const
Definition base.cc:71
zyppng::Base::findChildren
std::vector< std::weak_ptr< T > > findChildren() const
Definition base.h:102
zyppng::Base::shared_this
std::shared_ptr< T > shared_this()
Definition base.h:118
zyppng::Base::WeakPtr
std::weak_ptr< Base > WeakPtr
Definition base.h:66
zyppng::Base::connectFunc
static auto connectFunc(typename internal::MemberFunction< SenderFunc >::ClassType &s, SenderFunc &&sFun, ReceiverFunc &&rFunc, const Tracker &...trackers)
Definition base.h:163
zyppng::Base::children
const std::unordered_set< Ptr > & children() const
Definition base.cc:66
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:133
zyppng::Base::parent
WeakPtr parent() const
Definition base.cc:26
zyppng::Base::Ptr
std::shared_ptr< Base > Ptr
Definition base.h:65
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:142
zyppng::Base::weak_this
std::weak_ptr< T > weak_this() const
Definition base.h:123
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:154
zyppng::Base::d_ptr
std::unique_ptr< BasePrivate > d_ptr
Definition base.h:174
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:39
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:181
signals.h
zyppng::internal::MemberFunction< Ret(BType::*)(Args...)>::ClassType
BType ClassType
Definition base.h:35
zyppng::internal::MemberFunction
Definition base.h:31
zyppglobal.h
ZYPP_DECLARE_PRIVATE
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:87
LIBZYPP_NG_EXPORT
#define LIBZYPP_NG_EXPORT
Definition zyppglobal.h:8