Disk ARchive  2.4.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
statistics.hpp
Go to the documentation of this file.
1 //*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 // $Id: statistics.hpp,v 1.20 2011/01/09 17:25:58 edrusb Rel $
22 //
23 /*********************************************************************/
24 //
25 
29 
30 #ifndef STATISTICS_HPP
31 #define STATISTICS_HPP
32 
33 #include "../my_config.h"
34 
35 #include "infinint.hpp"
36 #include "user_interaction.hpp"
37 
38 extern "C"
39 {
40 #if MUTEX_WORKS
41 #if HAVE_PTHREAD_H
42 #include <pthread.h>
43 #endif
44 #endif
45 }
46 
49 
50 #if MUTEX_WORKS
51 #define LOCK_IN pthread_mutex_lock(&lock_mutex)
52 #define LOCK_OUT pthread_mutex_unlock(&lock_mutex)
53 #define LOCK_IN_CONST pthread_mutex_lock(const_cast<pthread_mutex_t *>(&lock_mutex))
54 #define LOCK_OUT_CONST pthread_mutex_unlock(const_cast<pthread_mutex_t *>(&lock_mutex))
55 #else
56 #define LOCK_IN //
57 #define LOCK_OUT //
58 #define LOCK_IN_CONST //
59 #define LOCK_OUT_CONST //
60 #endif
61 
62 namespace libdar
63 {
64 
66 
70  // are their meaning see the archive class constructor and methods documentation
72  class statistics
73  {
74  public:
76 
82  statistics(bool lock = true) { init(lock); clear(); };
83  statistics(const statistics & ref) { copy_from(ref); };
84  const statistics & operator = (const statistics & ref) { detruit(); copy_from(ref); return *this; };
85 
87  ~statistics() { detruit(); };
88 
90  void clear();
91 
93  infinint total() const;
94 
95  void incr_treated() { (this->*increment)(&treated); };
96  void incr_hard_links() { (this->*increment)(&hard_links); };
97  void incr_skipped() { (this->*increment)(&skipped); };
98  void incr_ignored() { (this->*increment)(&ignored); };
99  void incr_tooold() { (this->*increment)(&tooold); };
100  void incr_errored() { (this->*increment)(&errored); };
101  void incr_deleted() { (this->*increment)(&deleted); };
102  void incr_ea_treated() { (this->*increment)(&ea_treated); };
103 
104  void add_to_ignored(const infinint & val) { (this->*add_to)(&ignored, val); };
105  void add_to_errored(const infinint & val) { (this->*add_to)(&errored, val); };
106  void add_to_deleted(const infinint & val) { (this->*add_to)(&deleted, val); };
107  void add_to_byte_amount(const infinint & val) { (this->*add_to)(&byte_amount, val); };
108 
109  void sub_from_treated(const infinint & val) { (this->*sub_from)(&treated, val); };
110  void sub_from_ea_treated(const infinint & val) { (this->*sub_from)(&ea_treated, val); };
111  void sub_from_hard_links(const infinint & val) { (this->*sub_from)(&hard_links, val); };
112 
113  infinint get_treated() const { return (this->*returned)(&treated); };
114  infinint get_hard_links() const { return (this->*returned)(&hard_links); };
115  infinint get_skipped() const { return (this->*returned)(&skipped); };
116  infinint get_ignored() const { return (this->*returned)(&ignored); };
117  infinint get_tooold() const { return (this->*returned)(&tooold); };
118  infinint get_errored() const { return (this->*returned)(&errored); };
119  infinint get_deleted() const { return (this->*returned)(&deleted); };
120  infinint get_ea_treated() const { return (this->*returned)(&ea_treated); };
121  infinint get_byte_amount() const { return (this->*returned)(&byte_amount); };
122 
123  void decr_treated() { (this->*decrement)(&treated); };
124  void decr_hard_links() { (this->*decrement)(&hard_links); };
125  void decr_skipped() { (this->*decrement)(&skipped); };
126  void decr_ignored() { (this->*decrement)(&ignored); };
127  void decr_tooold() { (this->*decrement)(&tooold); };
128  void decr_errored() { (this->*decrement)(&errored); };
129  void decr_deleted() { (this->*decrement)(&deleted); };
130  void decr_ea_treated() { (this->*decrement)(&ea_treated); };
131 
132  void set_byte_amount(const infinint & val) { (this->*set_to)(&byte_amount, val); };
133 
134  // debuging method
135  void dump(user_interaction & dialog) const;
136 
137  private:
138 #if MUTEX_WORKS
139  pthread_mutex_t lock_mutex;
140 #endif
141  bool locking;
142 
143  infinint treated;
144  infinint hard_links;
145  infinint skipped;
146  infinint ignored;
147  infinint tooold;
148  infinint errored;
149  infinint deleted;
150  infinint ea_treated;
151  infinint byte_amount;
152 
153 
154  void (statistics::*increment)(infinint * var);
155  void (statistics::*add_to)(infinint * var, const infinint & val);
156  infinint (statistics::*returned)(const infinint * var) const;
157  void (statistics::*decrement)(infinint * var);
158  void (statistics::*set_to)(infinint * var, const infinint & val);
159  void (statistics::*sub_from)(infinint *var, const infinint & val);
160 
161  void increment_locked(infinint * var)
162  {
163  LOCK_IN;
164  (*var)++;
165  LOCK_OUT;
166  };
167 
168  void increment_unlocked(infinint * var)
169  {
170  (*var)++;
171  }
172 
173  void add_to_locked(infinint * var, const infinint & val)
174  {
175  LOCK_IN;
176  (*var) += val;
177  LOCK_OUT;
178  }
179 
180  void add_to_unlocked(infinint *var, const infinint & val)
181  {
182  (*var) += val;
183  }
184 
185  infinint returned_locked(const infinint * var) const
186  {
187  infinint ret;
188 
189  LOCK_IN_CONST;
190  ret = *var;
191  LOCK_OUT_CONST;
192 
193  return ret;
194  };
195 
196  infinint returned_unlocked(const infinint * var) const
197  {
198  return *var;
199  };
200 
201  void decrement_locked(infinint * var)
202  {
203  LOCK_IN;
204  (*var)--;
205  LOCK_OUT;
206  }
207 
208  void decrement_unlocked(infinint * var)
209  {
210  (*var)--;
211  }
212 
213  void set_to_locked(infinint *var, const infinint & val)
214  {
215  LOCK_IN;
216  (*var) = val;
217  LOCK_OUT;
218  }
219 
220  void set_to_unlocked(infinint *var, const infinint & val)
221  {
222  *var = val;
223  }
224 
225  void sub_from_unlocked(infinint *var, const infinint & val)
226  {
227  *var -= val;
228  }
229 
230  void sub_from_locked(infinint *var, const infinint & val)
231  {
232  LOCK_IN;
233  *var -= val;
234  LOCK_OUT;
235  }
236 
237 
238  void init(bool lock); // set locking & mutex
239  void detruit(); // release and free the mutex
240  void copy_from(const statistics & ref); // reset mutex and copy data from the object of reference
241 
242  };
243 
244 } // end of namespace
245 
247 
248 #endif