libzypp 17.38.6
poolcomponents.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_SAT_COMPONENTS_POOLCOMPONENTS_H_INCLUDED
15#define ZYPP_NG_SAT_COMPONENTS_POOLCOMPONENTS_H_INCLUDED
16
17#include <algorithm>
18#include <memory>
19#include <typeindex>
20#include <unordered_map>
21#include <array>
22#include <vector>
24
25namespace zyppng::sat {
26
27 class Pool;
28 class PreparedPool;
29
35 enum class InitStage : size_t {
38 };
39
45 enum class PreparedStage : size_t {
50 };
51
52 namespace detail {
60 public:
65 virtual void checkDirty ( Pool & ) {}
67 virtual void onInvalidate ( Pool &, PoolInvalidation ) {}
68 virtual void onRepoAdded ( Pool &, RepoIdType ) {}
69 virtual void onRepoRemoved( Pool &, RepoIdType ) {}
70 virtual void onReset ( Pool & ) {}
71
72 virtual ~IBasicPoolComponent() = default;
78 };
79 } // namespace detail
80
92 public:
93 virtual void attach ( Pool & ) {}
94 virtual void prepare( Pool & ) {}
95 virtual InitStage stage() const { return InitStage::Environment; }
101 virtual int priority() const { return 0; }
102 };
103
114 public:
115 virtual void attach ( Pool & ) {}
116 virtual void prepare( PreparedPool & ) {}
117 virtual PreparedStage stage() const { return PreparedStage::Policy; }
123 virtual int priority() const { return 0; }
124 };
125
126
139 public:
140 PoolComponentSet() = default;
145
146 template <typename T>
147 T &assertComponent( std::unique_ptr<T> &&compPtr = {} ) {
148
149 std::type_index idx( typeid(T) );
150 if ( _components.count( idx ) )
151 return *static_cast<T*>( _components[idx]->get() );
152
153 auto ptr = std::move(compPtr);
154 if ( !ptr )
155 ptr = std::make_unique<T>();
156
157 std::unique_ptr<TypeErasure> component = std::make_unique<CompContainer<T>>( std::move(ptr) );
158 auto iter = _components.insert( { std::move(idx), std::move(component) } );
159 auto genericComp = static_cast<T *>( iter.first->second->get() );
160
161 // Unified basic-callbacks list — always registered when the type derives from IBasicPoolComponent.
163 _basicComponents.push_back( static_cast<detail::IBasicPoolComponent*>( genericComp ) );
164
165 // Pre-index component list (stage-bucketed, priority-sorted within bucket).
166 if constexpr ( std::is_base_of_v<IPoolComponent, T> ) {
167 auto iComp = static_cast<IPoolComponent*>( genericComp );
168 _initComponents[static_cast<size_t>( iComp->stage() )].push_back( iComp );
169 _initDirtyBuckets[static_cast<size_t>( iComp->stage() )] = true;
170 }
171
172 // Post-index component list (stage-bucketed, priority-sorted within bucket).
174 auto iComp = static_cast<IPreparedPoolComponent*>( genericComp );
175 _preparedComponents[static_cast<size_t>( iComp->stage() )].push_back( iComp );
176 _preparedDirtyBuckets[static_cast<size_t>( iComp->stage() )] = true;
177 }
178
179 return *genericComp;
180 }
181
182 template <typename T>
183 const T *findComponent() const {
184 const std::type_index idx( typeid(T) );
185 if ( _components.count( idx ) )
186 return static_cast<T*>( _components.at(idx)->get() );
187 return nullptr;
188 }
189
192 for ( auto *comp : _basicComponents )
193 comp->checkDirty( pool );
194 }
195
199 for ( auto & bucket : _initComponents )
200 for ( auto *comp : bucket )
201 comp->prepare( pool );
202 }
203
207 for ( auto & bucket : _preparedComponents )
208 for ( auto *comp : bucket )
209 comp->prepare( pp );
210 }
211
212 void notifyInvalidate( Pool & pool, PoolInvalidation invalidation ) {
213 for ( auto *comp : _basicComponents )
214 comp->onInvalidate( pool, invalidation );
215 }
216
218 for ( auto *comp : _basicComponents )
219 comp->onRepoAdded( pool, id );
220 }
221
223 for ( auto *comp : _basicComponents )
224 comp->onRepoRemoved( pool, id );
225 }
226
228 for ( auto *comp : _basicComponents )
229 comp->onReset( pool );
230 }
231
232 private:
233 struct TypeErasure {
234 TypeErasure() = default;
235 virtual ~TypeErasure() = default;
236 virtual void * get() = 0;
237 TypeErasure(const TypeErasure &) = delete;
241 };
242
243 template <typename T>
244 struct CompContainer : public TypeErasure {
245 CompContainer( std::unique_ptr<T> component ) : _ptr( std::move(component) ) {}
246 ~CompContainer() override = default;
247 CompContainer(const CompContainer &) = delete;
251
252 std::unique_ptr<T> _ptr;
253
254 std::type_index typeIndex() const {
255 return typeid(T);
256 }
257
258 void *get() override {
259 return _ptr.get();
260 }
261 };
262
264 for ( size_t i = 0; i < static_cast<size_t>(InitStage::COUNT); i++ ) {
265 if ( _initDirtyBuckets[i] ) {
266 auto & bucket = _initComponents[i];
267 _initDirtyBuckets[i] = false;
268 std::stable_sort( bucket.begin(), bucket.end(), []( const auto &a, const auto &b ) {
269 return a->priority() < b->priority();
270 });
271 }
272 }
273 }
274
276 for ( size_t i = 0; i < static_cast<size_t>(PreparedStage::COUNT); i++ ) {
277 if ( _preparedDirtyBuckets[i] ) {
278 auto & bucket = _preparedComponents[i];
279 _preparedDirtyBuckets[i] = false;
280 std::stable_sort( bucket.begin(), bucket.end(), []( const auto &a, const auto &b ) {
281 return a->priority() < b->priority();
282 });
283 }
284 }
285 }
286
287 private:
288 std::unordered_map<std::type_index, std::unique_ptr<TypeErasure>> _components;
289
291 std::vector<detail::IBasicPoolComponent*> _basicComponents;
292
294 std::array<std::vector<IPoolComponent*>, static_cast<size_t>(InitStage::COUNT)> _initComponents;
295 std::array<bool, static_cast<size_t>(InitStage::COUNT)> _initDirtyBuckets = {false,};
296
298 std::array<std::vector<IPreparedPoolComponent*>, static_cast<size_t>(PreparedStage::COUNT)> _preparedComponents;
299 std::array<bool, static_cast<size_t>(PreparedStage::COUNT)> _preparedDirtyBuckets = {false,};
300 };
301
302} // namespace zyppng::sat
303
304#endif
Interface for components that run BEFORE the whatprovides index is built.
virtual InitStage stage() const
virtual void prepare(Pool &)
virtual void attach(Pool &)
virtual int priority() const
Fine-grained ordering within a stage.
Interface for components that run AFTER the whatprovides index is built.
virtual PreparedStage stage() const
virtual int priority() const
Fine-grained ordering within a stage.
virtual void prepare(PreparedPool &)
std::unordered_map< std::type_index, std::unique_ptr< TypeErasure > > _components
void notifyRepoAdded(Pool &pool, detail::RepoIdType id)
void notifyPrepareWithIndex(PreparedPool &pp)
Pass 3 of prepare(): post-index component work (stage/priority order).
void notifyPrepare(Pool &pool)
Pass 2 of prepare(): pre-index component work (stage/priority order).
T & assertComponent(std::unique_ptr< T > &&compPtr={})
std::array< bool, static_cast< size_t >(PreparedStage::COUNT)> _preparedDirtyBuckets
PoolComponentSet(PoolComponentSet &&)=default
std::array< std::vector< IPoolComponent * >, static_cast< size_t >(InitStage::COUNT)> _initComponents
Pre-index components — stage-bucketed, priority-sorted within bucket.
PoolComponentSet & operator=(PoolComponentSet &&)=default
void notifyInvalidate(Pool &pool, PoolInvalidation invalidation)
void notifyCheckDirty(Pool &pool)
Pass 1 of prepare(): probe external state.
std::array< bool, static_cast< size_t >(InitStage::COUNT)> _initDirtyBuckets
void notifyRepoRemoved(Pool &pool, detail::RepoIdType id)
std::array< std::vector< IPreparedPoolComponent * >, static_cast< size_t >(PreparedStage::COUNT)> _preparedComponents
Post-index components — stage-bucketed, priority-sorted within bucket.
std::vector< detail::IBasicPoolComponent * > _basicComponents
Unified list for checkDirty / onInvalidate / onRepoAdded / onRepoRemoved.
PoolComponentSet & operator=(const PoolComponentSet &)=delete
PoolComponentSet(const PoolComponentSet &)=delete
Orchestrator for a libsolv pool instance.
Definition pool.h:37
A move-only, non-owning view of a Pool that guarantees the whatprovides index is valid.
Shared base for IPoolComponent and IPreparedPoolComponent.
IBasicPoolComponent(const IBasicPoolComponent &)=delete
virtual void onRepoRemoved(Pool &, RepoIdType)
IBasicPoolComponent(IBasicPoolComponent &&)=delete
IBasicPoolComponent & operator=(IBasicPoolComponent &&)=delete
IBasicPoolComponent & operator=(const IBasicPoolComponent &)=delete
virtual void checkDirty(Pool &)
Probe external state.
virtual void onInvalidate(Pool &, PoolInvalidation)
React to pool invalidation — clear internal caches.
virtual void onRepoAdded(Pool &, RepoIdType)
unsigned short a
unsigned short b
Definition ansi.h:855
constexpr bool is_base_of_v
Definition TypeTraits.h:27
zypp::sat::detail::RepoIdType RepoIdType
This file contains private API, this might break at any time between releases.
PreparedStage
Execution stages for IPreparedPoolComponent (post-index phase).
@ Policy
Blacklists, Reboot Specs, Storage Policy.
@ Logging
Diagnostic components.
@ Metadata
ID-indexed metadata stores.
InitStage
Execution stages for IPoolComponent (pre-index phase).
@ Environment
Arches, Locales, Namespace Callbacks (The Foundation).
PoolInvalidation
Defines the scope of an invalidation request for the Pool.
CompContainer(const CompContainer &)=delete
CompContainer(std::unique_ptr< T > component)
CompContainer & operator=(CompContainer &&)=delete
CompContainer & operator=(const CompContainer &)=delete
TypeErasure & operator=(const TypeErasure &)=delete
TypeErasure & operator=(TypeErasure &&)=delete
TypeErasure(const TypeErasure &)=delete