Simple_object_pool.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): Marc Glisse
6  *
7  * Copyright (C) 2015 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef SIMPLE_OBJECT_POOL_H_
24 #define SIMPLE_OBJECT_POOL_H_
25 
26 #include <boost/pool/pool.hpp>
27 #include <utility>
28 
29 namespace Gudhi {
30 
37 template <class T>
38 class Simple_object_pool : protected boost::pool<boost::default_user_allocator_malloc_free> {
39  protected:
40  typedef boost::pool<boost::default_user_allocator_malloc_free> Base;
41  typedef T* pointer;
42 
43  Base& base() {
44  return *this;
45  }
46 
47  Base const& base()const {
48  return *this;
49  }
50 
51  public:
52  typedef T element_type;
53  typedef boost::default_user_allocator_malloc_free user_allocator;
54  typedef typename Base::size_type size_type;
55  typedef typename Base::difference_type difference_type;
56 
57  template<class...U>
58  Simple_object_pool(U&&...u) : Base(sizeof (T), std::forward<U>(u)...) { }
59 
60  template<class...U>
61  pointer construct(U&&...u) {
62  void* p = base().malloc BOOST_PREVENT_MACRO_SUBSTITUTION();
63  assert(p);
64  try {
65  new(p) T(std::forward<U>(u)...);
66  } catch (...) {
67  base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p);
68  throw;
69  }
70  return static_cast<pointer> (p);
71  }
72 
73  void destroy(pointer p) {
74  p->~T();
75  base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p);
76  }
77 };
78 
79 } // namespace Gudhi
80 
81 #endif // SIMPLE_OBJECT_POOL_H_
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Fri Oct 18 2019 18:40:54 for GUDHI by Doxygen 1.8.13