Disk ARchive  2.7.15
Full featured and portable backup and archiving tool
heap.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2024 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // to contact the author, see the AUTHOR file
20 /*********************************************************************/
21 
26 
27 #ifndef HEAP_HPP
28 #define HEAP_HPP
29 
30 #include "../my_config.h"
31 #include <string>
32 
33 #include "integers.hpp"
34 #include <memory>
35 #include <deque>
36 #if HAVE_LIBTHREADAR_LIBTHREADAR_HPP
37 #include <libthreadar/libthreadar.hpp>
38 #endif
39 
40 namespace libdar
41 {
42 
45 
47 
48  template <class T> class heap
49  {
50  public:
51  heap() {}; // start with an empty heap
52  heap(const heap & ref) = delete;
53  heap(heap && ref) = default;
54  heap & operator = (const heap & ref) = delete;
55  heap & operator = (heap && ref) noexcept = default;
56 
57  std::unique_ptr<T> get();
58  void put(std::unique_ptr<T> && obj);
59  void put(std::deque<std::unique_ptr<T> > & list);
60  U_I get_size() const { return tas.size(); };
61 
62  private:
63  std::deque<std::unique_ptr<T> > tas;
64 #ifdef LIBTHREADAR_AVAILABLE
65  libthreadar::mutex access;
66 #endif
67  };
68 
69  template <class T> std::unique_ptr<T> heap<T>::get()
70  {
71  std::unique_ptr<T> ret;
72 
73 #ifdef LIBTHREADAR_AVAILABLE
74  access.lock();
75  try
76  {
77 #endif
78 
79  if(tas.empty())
80  throw Erange("heap::get", "heap is empty, it should have be set larger");
81 
82  ret = std::move(tas.back()); // moving the object pointed to by tas.back() to ret
83  tas.pop_back(); // removing the now empty pointer at the end of 'tas'
84 
85 #ifdef LIBTHREADAR_AVAILABLE
86  }
87  catch(...)
88  {
89  access.unlock();
90  throw;
91  }
92  access.unlock();
93 #endif
94 
95  return ret;
96  }
97 
98  template <class T> void heap<T>::put(std::unique_ptr<T> && obj)
99  {
100 #ifdef LIBTHREADAR_AVAILABLE
101  access.lock();
102  try
103  {
104 #endif
105 
106  tas.push_back(std::move(obj));
107 
108 #ifdef LIBTHREADAR_AVAILABLE
109  }
110  catch(...)
111  {
112  access.unlock();
113  throw;
114  }
115  access.unlock();
116 #endif
117  }
118 
119  template <class T> void heap<T>::put(std::deque<std::unique_ptr<T> > & list)
120  {
121  typename std::deque<std::unique_ptr<T> >::iterator it = list.begin();
122 
123 #ifdef LIBTHREADAR_AVAILABLE
124  access.lock();
125  try
126  {
127 #endif
128 
129  while(it != list.end())
130  {
131  tas.push_back(std::move(*it));
132  ++it;
133  }
134 
135 #ifdef LIBTHREADAR_AVAILABLE
136  }
137  catch(...)
138  {
139  access.unlock();
140  throw;
141  }
142  access.unlock();
143 #endif
144  }
145 
147 
148 } // end of namespace
149 
150 #endif
exception used to signal range error
Definition: erreurs.hpp:220
the class heap is nothing related to the common heap datastructure this is just a "heap" in the sense...
Definition: heap.hpp:49
are defined here basic integer types that tend to be portable
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47