libzypp  13.10.6
HardLocksFile.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_TARGET_HARDLOCKSFILE_H
13 #define ZYPP_TARGET_HARDLOCKSFILE_H
14 
15 #include <iosfwd>
16 
17 #include "zypp/base/PtrTypes.h"
18 
19 #include "zypp/Pathname.h"
20 #include "zypp/pool/PoolTraits.h"
21 #include "zypp/PoolQuery.h"
22 
24 namespace zypp
25 {
26  namespace target
28  {
29 
31  //
32  // CLASS NAME : HardLocksFile
33  //
37  {
38  friend std::ostream & operator<<( std::ostream & str, const HardLocksFile & obj );
39  public:
40 
42 
43  public:
45  HardLocksFile( const Pathname & file_r )
46  : _file( file_r )
47  {}
48 
50  const Pathname & file() const
51  { return _file; }
52 
57  const Data & data() const
58  {
59  if ( !_dataPtr )
60  {
61  _dataPtr.reset( new Data );
62  Data & mydata( *_dataPtr );
63  load( _file, mydata );
64  }
65  return *_dataPtr;
66  }
67 
73  void setData( const Data & data_r )
74  {
75  if ( !_dataPtr )
76  _dataPtr.reset( new Data );
77 
78  if ( differs( *_dataPtr, data_r ) )
79  {
80  store( _file, data_r );
81  *_dataPtr = data_r;
82  }
83  }
84 
85  private:
87  bool differs( const Data & lhs, const Data & rhs ) const
88  {
89  if ( lhs.size() != rhs.size() )
90  return true;
91  // Complete diff is too expensive and not necessary here.
92  // Just check for the same sequence of items.
93  Data::const_iterator rit = rhs.begin();
94  for_( it, lhs.begin(), lhs.end() )
95  {
96  if ( *it != *rit )
97  return true;
98  ++rit;
99  }
100  return false;
101  }
103  static void load( const Pathname & file_r, Data & data_r );
105  static void store( const Pathname & file_r, const Data & data_r );
106 
107  private:
108  Pathname _file;
109  mutable scoped_ptr<Data> _dataPtr;
110  };
112 
114  std::ostream & operator<<( std::ostream & str, const HardLocksFile & obj );
115 
117  } // namespace target
120 } // namespace zypp
122 #endif // ZYPP_TARGET_HARDLOCKSFILE_H
const Data & data() const
Return the data.
Definition: HardLocksFile.h:57
static void store(const Pathname &file_r, const Data &data_r)
Write Data to file_r.
void setData(const Data &data_r)
Store new Data.
Definition: HardLocksFile.h:73
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
std::list< PoolQuery > HardLockQueries
hard locks from etc/zypp/locks
Definition: PoolTraits.h:92
static void load(const Pathname &file_r, Data &data_r)
Read Data from file_r.
std::ostream & operator<<(std::ostream &str, const CommitPackageCache &obj)
HardLocksFile(const Pathname &file_r)
Ctor taking the file to read/write.
Definition: HardLocksFile.h:45
pool::PoolTraits::HardLockQueries Data
Definition: HardLocksFile.h:41
bool differs(const Data &lhs, const Data &rhs) const
Helper testing whether two Data differ.
Definition: HardLocksFile.h:87
friend std::ostream & operator<<(std::ostream &str, const HardLocksFile &obj)
Save and restore hardlocks.
Definition: HardLocksFile.h:36
const Pathname & file() const
Return the file path.
Definition: HardLocksFile.h:50
scoped_ptr< Data > _dataPtr