Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / target / SolvIdentFile.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/target/SolvIdentFile.h
10  *
11 */
12 #ifndef ZYPP_TARGET_SOLVIDENTFILE_H
13 #define ZYPP_TARGET_SOLVIDENTFILE_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/PtrTypes.h"
18
19 #include "zypp/IdString.h"
20 #include "zypp/Pathname.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace target
27   { /////////////////////////////////////////////////////////////////
28
29     ///////////////////////////////////////////////////////////////////
30     /// \class SolvIdentFile
31     /// \short Save and restore a list of solvable names (ident IdString)
32     ///////////////////////////////////////////////////////////////////
33     class SolvIdentFile
34     {
35       friend std::ostream & operator<<( std::ostream & str, const SolvIdentFile & obj );
36       public:
37         typedef std::unordered_set<IdString> Data;
38
39       public:
40         /** Ctor taking the file to read/write. */
41         SolvIdentFile( const Pathname & file_r )
42         : _file( file_r )
43         {}
44
45         /** Return the file path. */
46         const Pathname & file() const
47         { return _file; }
48
49         /** Return the data.
50          * The file is read once on demand. Returns empty \ref Data if
51          * the file does not exist or is not readable.
52         */
53         const Data & data() const
54         {
55           if ( !_dataPtr )
56           {
57             _dataPtr.reset( new Data );
58             Data & mydata( *_dataPtr );
59             load( _file, mydata );
60           }
61           return *_dataPtr;
62         }
63
64         /** Store new \ref Data.
65          * Write the new \ref Data to file, unless we know it
66          * did not change. The directory containing file must
67          * exist.
68         */
69         void setData( const Data & data_r )
70         {
71           if ( !_dataPtr )
72             _dataPtr.reset( new Data );
73
74           if ( differs( *_dataPtr, data_r ) )
75           {
76             store( _file, data_r );
77             *_dataPtr = data_r;
78           }
79         }
80
81       private:
82         /** Helper testing whether two \ref Data differ. */
83         bool differs( const Data & lhs, const Data & rhs ) const
84         {
85
86           if ( lhs.size() != rhs.size() )
87             return true;
88           for_( it, lhs.begin(), lhs.end() )
89           {
90             if ( rhs.find( *it ) == rhs.end() )
91               return true;
92           }
93           return false;
94         }
95         /** Read \ref Data from \c file_r. */
96         static void load( const Pathname & file_r, Data & data_r );
97         /** Write \ref Data to \c file_r. */
98         static void store( const Pathname & file_r, const Data & data_r );
99
100       private:
101         Pathname                 _file;
102         mutable scoped_ptr<Data> _dataPtr;
103     };
104     ///////////////////////////////////////////////////////////////////
105
106     /** \relates SolvIdentFile Stream output */
107     std::ostream & operator<<( std::ostream & str, const SolvIdentFile & obj );
108
109     /////////////////////////////////////////////////////////////////
110   } // namespace target
111   ///////////////////////////////////////////////////////////////////
112   /////////////////////////////////////////////////////////////////
113 } // namespace zypp
114 ///////////////////////////////////////////////////////////////////
115 #endif // ZYPP_TARGET_SOLVIDENTFILE_H