libzypp  17.31.31
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 
20 using std::endl;
21 
23 namespace zypp
24 {
26  namespace sat
27  {
33  {
34  public:
35  void addIdent( IdString ident_r )
36  {
37  if ( ! ident_r.empty() )
38  _idents.insert( ident_r );
39  }
40 
41  void addProvides( Capability provides_r )
42  {
43  if ( ! provides_r.empty() && _provides.insert( provides_r ).second )
44  setDirty();
45  }
46 
48  { return _addIdenticalInstalledToo; }
49 
50  void addIdenticalInstalledToo( bool yesno_r )
51  {
52  if ( yesno_r != _addIdenticalInstalledToo ) {
53  _addIdenticalInstalledToo = yesno_r;
54  if ( not _provides.empty() )
55  setDirty();
56  }
57  }
58 
59  void parse( const C_Str & spec_r )
60  {
61  if ( str::hasPrefix( spec_r, "provides:" ) )
62  addProvides( Capability(spec_r.c_str()+9) );
63  else
64  addIdent( IdString(spec_r) );
65  }
66 
67 
68  bool needed() const
69  { return !_provides.empty(); }
70 
71  bool dirty() const
72  { return needed() && !_cache; }
73 
74  void setDirty() const
75  { _cache.reset(); _cacheIdenticalInstalled.clear(); }
76 
77  const WhatProvides & cache() const
78  {
79  if ( !_cache )
80  {
81  _cache.reset( new WhatProvides( _provides ) );
83  for ( const auto & solv : *_cache ) {
84  if ( solv.isSystem() )
85  continue;
86  auto pi { ui::Selectable::get(solv)->identicalInstalledObj( PoolItem(solv) ) };
87  if ( pi )
89  }
90  }
91  }
92  return *_cache;
93  }
94 
95  bool contains( const sat::Solvable & solv_r ) const
96  {
97  if ( _idents.count( solv_r.ident() ) )
98  return true;
99  if ( needed() ) {
100  if ( cache().contains( solv_r ) )
101  return true;
103  return true;
104  }
105  return false;
106  }
107 
108 
109  const IdStringSet & idents() const
110  { return _idents; }
111 
112  const CapabilitySet & provides() const
113  { return _provides; }
114 
115  private:
119  mutable SolvableSet _cacheIdenticalInstalled; // follows _cache
120  mutable shared_ptr<WhatProvides> _cache;
121 
122  private:
123  friend Impl * rwcowClone<Impl>( const Impl * rhs );
125  Impl * clone() const
126  { return new Impl( *this ); }
127  };
128 
130  inline std::ostream & operator<<( std::ostream & str, const SolvableSpec::Impl & obj )
131  {
132  str << "SolvableSpec {" << endl
133  << " Idents " << obj.idents() << endl
134  << " Provides " << obj.provides() << endl
135  << "}";
136  return str;
137  }
138 
140  //
141  // CLASS NAME : SolvableSpec
142  //
144 
146  : _pimpl( new Impl )
147  {}
148 
150  {}
151 
153  { _pimpl->addIdent( ident_r ); }
154 
156  { _pimpl->addProvides( provides_r ); }
157 
159  { return _pimpl->addIdenticalInstalledToo(); }
161  { _pimpl->addIdenticalInstalledToo( yesno_r ); }
162 
163  void SolvableSpec::parse( const C_Str & spec_r )
164  { _pimpl->parse( spec_r ); }
165 
166  void SolvableSpec::parseFrom( const InputStream & istr_r )
167  {
168  iostr::simpleParseFile( istr_r,
169  [this]( int num_r, const std::string & line_r )->bool
170  {
171  this->parse( line_r );
172  return true;
173  });
174  }
175 
176  void SolvableSpec::splitParseFrom( const C_Str & multispec_r )
177  {
178  std::vector<std::string> v;
179  str::splitEscaped( multispec_r, std::back_inserter( v ), ", \t" );
180  parseFrom( v.begin(), v.end() );
181  }
182 
183  bool SolvableSpec::contains( const sat::Solvable & solv_r ) const
184  { return _pimpl->contains( solv_r ) && !solv_r.isKind( ResKind::srcpackage ); }
185 
186  bool SolvableSpec::dirty() const
187  { return _pimpl->dirty(); }
188 
190  { _pimpl->setDirty(); }
191 
192  bool SolvableSpec::empty() const
193  { return _pimpl->idents().empty() && _pimpl->provides().empty(); }
194 
195  bool SolvableSpec::containsIdent( const IdString & ident_r ) const
196  { return _pimpl->idents().count( ident_r ); }
197 
198  bool SolvableSpec::containsProvides( const Capability & provides_r ) const
199  { return _pimpl->provides().count( provides_r ); }
200 
201  std::ostream & operator<<( std::ostream & str, const SolvableSpec & obj )
202  { return str << *obj._pimpl; }
203 
204  } // namespace sat
206 } // namespace zypp
bool contains(const TSolv &solv_r) const
Definition: SolvableSet.h:68
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
const CapabilitySet & provides() const
const IdStringSet & idents() const
unsigned splitEscaped(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \, bool withEmpty=false)
Split line_r into words with respect to escape delimeters.
Definition: String.h:595
bool containsProvides(const Capability &provides_r) const
Whether provides_r has been added to the sepcs (mainly for parser tests).
std::ostream & operator<<(std::ostream &str, const FileConflicts &obj)
String related utilities and Regular expression matching.
void parse(const C_Str &spec_r)
Parse and add spec from a string (IDENT or provides:CAPABILITY`).
bool isKind(const ResKind &kind_r) const
Test whether a Solvable is of a certain ResKind.
Definition: Solvable.cc:302
SolvableSpec()
Default ctor.
static const ResKind srcpackage
Definition: ResKind.h:44
Access to the sat-pools string space.
Definition: IdString.h:42
Helper to create and pass std::istream.
Definition: inputstream.h:56
const char * c_str() const
Definition: String.h:116
bool addIdenticalInstalledToo() const
Definition: SolvableSpec.cc:47
void addIdent(IdString ident_r)
Definition: SolvableSpec.cc:35
void parse(const C_Str &spec_r)
Definition: SolvableSpec.cc:59
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
bool dirty() const
Whether the cache is needed and dirty.
const WhatProvides & cache() const
Definition: SolvableSpec.cc:77
constexpr bool empty() const
Whether the string is empty.
Definition: IdString.h:87
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
std::unordered_set< IdString > IdStringSet
Definition: IdString.h:27
void addIdenticalInstalledToo(bool yesno_r)
Definition: SolvableSpec.cc:50
void setDirty() const
Explicitly flag the cache as dirty, so it will be rebuilt on the next request.
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 its specs (one per line, #-comments).
bool contains(const sat::Solvable &solv_r) const
Test whether solv_r matches the spec.
SolvableSpec implementation.
Definition: SolvableSpec.cc:32
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
bool contains(const sat::Solvable &solv_r) const
Definition: SolvableSpec.cc:95
Impl * clone() const
clone for RWCOW_pointer
A sat capability.
Definition: Capability.h:62
bool addIdenticalInstalledToo() const
Extend the provides set to include idential installed items as well.
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
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
bool empty() const
Whether the Capability is empty.
Definition: Capability.h:160
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1027
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:41
IdString ident() const
The identifier.
Definition: Solvable.cc:269
bool contains(const TSolv &solv_r) const
Whether collection contains a specific Solvable.
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