ignore
[platform/upstream/libzypp.git] / zypp / DiskUsage.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/DiskUsage.h
10  *
11 */
12 #ifndef ZYPP_DISKUSAGE_H
13 #define ZYPP_DISKUSAGE_H
14
15 #include <set>
16 #include <string>
17
18 ///////////////////////////////////////////////////////////////////
19 namespace zypp
20 { /////////////////////////////////////////////////////////////////
21
22   class DiskUsage {
23   public:
24     /**
25     * @short Holds data about how much space will be needed per directory
26     **/
27     struct Entry {
28       Entry() : _size(0), _files(0) {};
29       Entry(const std::string& path_r,
30               const unsigned size_r = 0,
31               const unsigned files_r = 0)
32       : path(path_r), _size(size_r), _files(files_r)
33       {}
34       const std::string path;
35       mutable unsigned _size;
36       mutable unsigned _files;
37       friend std::ostream & operator<<( std::ostream & str, const Entry & obj );
38       /**
39        * Test for equality based on directory name.
40        **/
41       bool operator==( const Entry & rhs ) const {
42         return  path == rhs.path;
43       }
44       /**
45        * Order based on directory name.
46        **/
47       bool operator<( const Entry & rhs ) const {
48         return  path < rhs.path;
49       }
50       /**
51        * Return true if this entry denotes a directory equal to or below rhs._dirname.
52        **/
53       bool isBelow( const Entry & rhs ) const {
54         // whether _dirname has prefix rhs._dirname
55         return( path.compare( 0, rhs.path.size(), rhs.path ) == 0 );
56       }
57       /**
58        * Return true if this entry denotes a directory equal to or below dirname_r.
59        **/
60       bool isBelow( const std::string & dirname_r ) const {
61         return  isBelow( Entry( dirname_r ) );
62       }
63
64       /**
65        * 
66        **/
67       const Entry & operator=( const Entry & rhs ) const {
68         return rhs;
69       }
70       /**
71        * Numerical operation based on size and files values.
72        **/
73       const Entry & operator+=( const Entry & rhs ) const {
74         _size  += rhs._size;
75         _files += rhs._files;
76         return *this;
77       }
78       /**
79        * Numerical operation based on size and files values.
80        **/
81       const Entry & operator-=( const Entry & rhs ) const {
82         _size  -= rhs._size;
83         _files -= rhs._files;
84         return *this;
85       }
86     };
87   private:
88     typedef std::set<Entry> EntrySet;
89     EntrySet _dirs;
90   public:
91
92     DiskUsage() {};
93    /**
94      * Add an entry. If already present, sum up the new entries size and files value.
95      **/
96     void add( const Entry & newent_r ) {
97       std::pair<EntrySet::iterator,bool> res = _dirs.insert( newent_r );
98       if ( !res.second ) {
99         *res.first += newent_r;
100       }
101     }
102    /**
103      * Add an entry. If already present, sum up the new entries size and files value.
104      **/
105     void add( const std::string & dirname_r, const unsigned & size_r = 0, const unsigned & files_r = 0 ) {
106       add( Entry( dirname_r, size_r, files_r ) );
107     }
108     /**
109      * Number of entries
110      **/
111     unsigned size() const { return _dirs.size(); }
112     /**
113      * Clear EntrySet
114      **/
115     void clear() { _dirs.clear(); }
116     /**
117      * Sum up any entries for dirname_r and its descendants and remove them
118      * on the fly. Return the result.
119      **/
120     Entry extract( const std::string & dirname_r );
121
122   public:
123
124     typedef EntrySet::iterator               iterator;
125     typedef EntrySet::reverse_iterator       reverse_iterator;
126  
127     /**
128      * Forward iterator pointing to the first entry (if any)
129      **/
130     iterator begin() { return _dirs.begin(); }
131     /**
132      * Forward iterator pointing behind the last entry.
133      **/
134     iterator end() { return _dirs.end(); }
135     /**
136      * Reverse iterator pointing to the last entry (if any)
137      **/
138     reverse_iterator rbegin() { return _dirs.rbegin(); }
139     /**
140      * Reverse iterator pointing before the first entry.
141      **/
142     reverse_iterator rend() { return _dirs.rend(); }
143  
144     typedef EntrySet::const_iterator         const_iterator;
145     typedef EntrySet::const_reverse_iterator const_reverse_iterator;
146  
147     /**
148      * Forward const iterator pointing to the first entry (if any)
149      **/
150     const_iterator begin() const { return _dirs.begin(); }
151     /**
152      * Forward const iterator pointing behind the last entry.
153      **/
154     const_iterator end() const { return _dirs.end(); }
155     /**
156      * Reverse const iterator pointing to the last entry (if any)
157      **/
158     const_reverse_iterator rbegin() const { return _dirs.rbegin(); }
159     /**
160      * Reverse const iterator pointing before the first entry.
161      **/
162     const_reverse_iterator rend()const { return _dirs.rend(); }
163
164   public:
165
166     friend std::ostream & operator<<( std::ostream & str, const DiskUsage & obj );
167
168   };
169   ///////////////////////////////////////////////////////////////////
170   /////////////////////////////////////////////////////////////////
171 } // namespace zypp
172 ///////////////////////////////////////////////////////////////////
173 #endif // ZYPP_DISKUSAGE_H