Disk ARchive  2.4.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
pile.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 
25 
26 
27 #ifndef PILE_HPP
28 #define PILE_HPP
29 
30 #include "../my_config.h"
31 
32 #include <vector>
33 #include <list>
34 #include "generic_file.hpp"
35 
36 namespace libdar
37 {
38 
41 
42  class pile : public generic_file
43  {
44  public:
49 
50  pile() : generic_file(gf_read_only) { stack.clear(); };
51  pile(const pile & ref) : generic_file(ref) { copy_from(ref); };
52  const pile & operator = (const pile & ref) { detruit(); copy_from(ref); return *this; };
53  ~pile() { detruit(); };
54 
64  void push(generic_file *f, const std::string & label = "");
65 
70  generic_file *pop();
71 
75  template <class T> bool pop_and_close_if_type_is(T *ptr);
76 
78  generic_file *top() { if(stack.empty()) return NULL; else return stack.back().ptr; };
79 
81  generic_file *bottom() { if(stack.empty()) return NULL; else return stack[0].ptr; };
82 
84  U_I size() const { return stack.size(); };
85 
87  bool is_empty() const { return stack.empty(); };
88 
90  void clear() { detruit(); };
91 
95  template<class T> void find_first_from_top(T * & ref);
96 
98  template<class T> void find_first_from_bottom(T * & ref);
99 
100 
102  generic_file *get_below(const generic_file *ref);
103 
105  generic_file *get_above(const generic_file *ref);
106 
107 
112  generic_file *get_by_label(const std::string & label);
113 
114 
115 
120  void clear_label(const std::string & label);
121 
122 
128  void add_label(const std::string & label);
129 
130 
131 
132  // inherited methods from generic_file
133  // they all apply to the top generic_file object, they fail by Erange() exception if the stack is empty
134 
135  bool skip(const infinint & pos);
136  bool skip_to_eof();
137  bool skip_relative(S_I x);
138  infinint get_position();
139  void copy_to(generic_file & ref);
140  void copy_to(generic_file & ref, const infinint & crc_size, crc * & value);
141 
142  protected:
143  U_I inherited_read(char *a, U_I size);
144  void inherited_write(const char *a, U_I size);
145  void inherited_sync_write();
146  void inherited_terminate();
147 
148  private:
149  struct face
150  {
151  generic_file * ptr;
152  std::list<std::string> labels;
153  };
154 
155  std::vector<face> stack;
156 
157  void copy_from(const pile & ref)
158  {
159  throw SRC_BUG; // it is not possible to copy an object to its another of the exact same type when only a pure virtual pointer pointing on it is available, or when no virtual "clone'-like method is available from the root pure virtual class (generic_file here).
160  };
161  void detruit();
162  std::vector<face>::iterator look_for_label(const std::string & label);
163  };
164 
165 
166  template <class T> bool pile::pop_and_close_if_type_is(T *ptr)
167  {
168  generic_file *top = NULL;
169 
170  if(!stack.empty())
171  {
172  top = stack.back().ptr;
173  ptr = dynamic_cast<T *>(top);
174  if(ptr != NULL)
175  {
176  stack.pop_back();
177  delete top;
178  return true;
179  }
180  else
181  return false;
182  }
183  else
184  return false;
185  }
186 
187  template <class T> void pile::find_first_from_top(T * & ref)
188  {
189  ref = NULL;
190  for(std::vector<face>::reverse_iterator it = stack.rbegin(); it != stack.rend() && ref == NULL; ++it)
191  ref = dynamic_cast<T *>(it->ptr);
192  }
193 
194 
195  template <class T> void pile::find_first_from_bottom(T * & ref)
196  {
197  ref = NULL;
198  for(std::vector<face>::iterator it = stack.begin(); it != stack.end() && ref == NULL; ++it)
199  ref = dynamic_cast<T *>(it->ptr);
200  }
201 
203 
204 } // end of namespace
205 
206 #endif