libzypp  16.22.5
SolvableSpec.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
11 #include <iostream>
12 
13 #include <zypp/base/LogTools.h>
14 #include <zypp/base/IOStream.h>
15 
16 #include <zypp/sat/SolvableSpec.h>
17 #include <zypp/sat/SolvableSet.h>
18 #include <zypp/sat/WhatProvides.h>
19 #include "zypp/ui/Selectable.h"
20 
21 using std::endl;
22 
24 namespace zypp
25 {
27  namespace sat
28  {
34  {
35  public:
36  void addIdent( IdString ident_r )
37  {
38  if ( ! ident_r.empty() )
39  _idents.insert( ident_r );
40  }
41 
42  void addProvides( Capability provides_r )
43  {
44  if ( ! provides_r.empty() && _provides.insert( provides_r ).second )
45  setDirty();
46  }
47 
49  { return _addIdenticalInstalledToo; }
50 
51  void addIdenticalInstalledToo( bool yesno_r )
52  {
53  if ( yesno_r != _addIdenticalInstalledToo ) {
54  _addIdenticalInstalledToo = yesno_r;
55  if ( not _provides.empty() )
56  setDirty();
57  }
58  }
59 
60  void parse( const C_Str & spec_r )
61  {
62  if ( str::hasPrefix( spec_r, "provides:" ) )
63  addProvides( Capability(spec_r.c_str()+9) );
64  else
65  addIdent( IdString(spec_r) );
66  }
67 
68 
69  bool needed() const
70  { return !_provides.empty(); }
71 
72  bool dirty() const
73  { return needed() && !_cache; }
74 
75  void setDirty() const
76  { _cache.reset(); _cacheIdenticalInstalled.clear(); }
77 
78  const WhatProvides & cache() const
79  {
80  if ( !_cache )
81  {
82  _cache.reset( new WhatProvides( _provides ) );
84  for ( const auto & solv : *_cache ) {
85  if ( solv.isSystem() )
86  continue;
87  auto pi = ui::Selectable::get(solv)->identicalInstalledObj( PoolItem(solv) );
88  if ( pi )
90  }
91  }
92  }
93  return *_cache;
94  }
95 
96  bool contains( const sat::Solvable & solv_r ) const
97  {
98  if ( _idents.count( solv_r.ident() ) )
99  return true;
100  if ( needed() ) {
101  if ( cache().contains( solv_r ) )
102  return true;
104  return true;
105  }
106  return false;
107  }
108 
109 
110  const IdStringSet & idents() const
111  { return _idents; }
112 
113  const CapabilitySet & provides() const
114  { return _provides; }
115 
116  private:
120  mutable SolvableSet _cacheIdenticalInstalled; // follows _cache
121  mutable shared_ptr<WhatProvides> _cache;
122 
123  private:
124  friend Impl * rwcowClone<Impl>( const Impl * rhs );
126  Impl * clone() const
127  { return new Impl( *this ); }
128  };
129 
131  inline std::ostream & operator<<( std::ostream & str, const SolvableSpec::Impl & obj )
132  {
133  str << "SolvableSpec {" << endl
134  << " Idents " << obj.idents() << endl
135  << " Provides " << obj.provides() << endl
136  << "}";
137  return str;
138  }
139 
141  //
142  // CLASS NAME : SolvableSpec
143  //
145 
147  : _pimpl( new Impl )
148  {}
149 
151  {}
152 
154  { _pimpl->addIdent( ident_r ); }
155 
157  { _pimpl->addProvides( provides_r ); }
158 
160  { return _pimpl->addIdenticalInstalledToo(); }
162  { _pimpl->addIdenticalInstalledToo( yesno_r ); }
163 
164  void SolvableSpec::parse( const C_Str & spec_r )
165  { _pimpl->parse( spec_r ); }
166 
167  void SolvableSpec::parseFrom( const InputStream & istr_r )
168  {
169  iostr::simpleParseFile( istr_r,
170  [this]( int num_r, const std::string & line_r )->bool
171  {
172  this->parse( line_r );
173  return true;
174  });
175  }
176 
177  void SolvableSpec::splitParseFrom( const C_Str & multispec_r )
178  {
179  std::vector<std::string> v;
180  str::splitEscaped( multispec_r, std::back_inserter( v ), ", \t" );
181  parseFrom( v.begin(), v.end() );
182  }
183 
184  bool SolvableSpec::contains( const sat::Solvable & solv_r ) const
185  { return _pimpl->contains( solv_r ) && !solv_r.isKind( ResKind::srcpackage ); }
186 
187  bool SolvableSpec::dirty() const
188  { return _pimpl->dirty(); }
189 
191  { _pimpl->setDirty(); }
192 
193  bool SolvableSpec::empty() const
194  { return _pimpl->idents().empty() && _pimpl->provides().empty(); }
195 
196  bool SolvableSpec::containsIdent( const IdString & ident_r ) const
197  { return _pimpl->idents().count( ident_r ); }
198 
199  bool SolvableSpec::containsProvides( const Capability & provides_r ) const
200  { return _pimpl->provides().count( provides_r ); }
201 
202  std::ostream & operator<<( std::ostream & str, const SolvableSpec & obj )
203  { return str << *obj._pimpl; }
204 
205  } // namespace sat
207 } // namespace zypp
bool dirty() const
Whether the cache is needed and dirty.
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t", bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:578
void splitParseFrom(const C_Str &multispec_r)
Convenience using str::splitEscaped(", \t") to parse multiple specs from one line.
A Solvable object within the sat Pool.
Definition: Solvable.h:53
Container of Solvable providing a Capability (read only).
Definition: WhatProvides.h:87
IdString ident() const
The identifier.
Definition: Solvable.cc:269
const IdStringSet & idents() const
bool contains(const TSolv &solv_r) const
Definition: SolvableSet.h:68
std::ostream & operator<<(std::ostream &str, const FileConflicts &obj)
bool addIdenticalInstalledToo() const
Extend the provides set to include idential installed items as well.
const WhatProvides & cache() const
Definition: SolvableSpec.cc:78
void parse(const C_Str &spec_r)
Parse and add spec from a string (IDENT or provides:CAPABILITY`).
SolvableSpec()
Default ctor.
static const ResKind srcpackage
Definition: ResKind.h:44
Access to the sat-pools string space.
Definition: IdString.h:41
Helper to create and pass std::istream.
Definition: InputStream.h:56
bool addIdenticalInstalledToo() const
Definition: SolvableSpec.cc:48
void addIdent(IdString ident_r)
Definition: SolvableSpec.cc:36
void parse(const C_Str &spec_r)
Definition: SolvableSpec.cc:60
bool contains(const sat::Solvable &solv_r) const
Test whether solv_r matches the spec.
void addIdent(IdString ident_r)
Add all sat::Solvable with this ident_r.
Define a set of Solvables by ident and provides.
Definition: SolvableSpec.h:44
void setDirty() const
Explicitly flag the cache as dirty, so it will be rebuilt on the next request.
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
Definition: IOStream.cc:124
Impl * clone() const
clone for RWCOW_pointer
std::unordered_set< IdString > IdStringSet
Definition: IdString.h:26
void addIdenticalInstalledToo(bool yesno_r)
Definition: SolvableSpec.cc:51
Convenience char* constructible from std::string and char*, it maps (char*)0 to an empty string...
Definition: String.h:90
void parseFrom(const InputStream &istr_r)
Parse file istr_r and add it's specs (one per line, #-comments).
bool empty() const
Whether the Capability is empty.
Definition: Capability.h:150
SolvableSpec implementation.
Definition: SolvableSpec.cc:33
bool contains(const sat::Solvable &solv_r) const
Definition: SolvableSpec.cc:96
constexpr bool empty() const
Whether the string is empty.
Definition: IdString.h:80
void addProvides(Capability provides_r)
A all sat::Solvable matching this provides_r.
bool empty() const
Whether neither idents nor provides are set.
shared_ptr< WhatProvides > _cache
std::unordered_set< Capability > CapabilitySet
Definition: Capability.h:33
static Ptr get(const pool::ByIdent &ident_r)
Get the Selctable.
Definition: Selectable.cc:28
const CapabilitySet & provides() const
bool contains(const TSolv &solv_r) const
Whether collection contains a specific Solvable.
A sat capability.
Definition: Capability.h:59
const char * c_str() const
Definition: String.h:113
bool containsProvides(const Capability &provides_r) const
Whether provides_r has been added to the sepcs (mainly for parser tests).
void clear()
Clear the container.
Definition: SolvableSet.h:81
std::ostream & operator<<(std::ostream &str, const SolvableSpec::Impl &obj)
Combining sat::Solvable and ResStatus.
Definition: PoolItem.h:50
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition: Solvable.cc:302
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1037
bool containsIdent(const IdString &ident_r) const
Whether ident_r has been added to the specs (mainly for parser tests).
RWCOW_pointer< Impl > _pimpl
Implementation class.
Definition: SolvableSpec.h:115
void addProvides(Capability provides_r)
Definition: SolvableSpec.cc:42
bool insert(const TSolv &solv_r)
Insert a Solvable.
Definition: SolvableSet.h:88
Solvable set wrapper to allow adding additional convenience iterators.
Definition: SolvableSet.h:35