Simple_object_pool.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): Marc Glisse
4  *
5  * Copyright (C) 2015 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef SIMPLE_OBJECT_POOL_H_
12 #define SIMPLE_OBJECT_POOL_H_
13 
14 #include <boost/pool/pool.hpp>
15 #include <utility>
16 
17 namespace Gudhi {
18 
25 template <class T>
26 class Simple_object_pool : protected boost::pool<boost::default_user_allocator_malloc_free> {
27  protected:
28  typedef boost::pool<boost::default_user_allocator_malloc_free> Base;
29  typedef T* pointer;
30 
31  Base& base() {
32  return *this;
33  }
34 
35  Base const& base()const {
36  return *this;
37  }
38 
39  public:
40  typedef T element_type;
41  typedef boost::default_user_allocator_malloc_free user_allocator;
42  typedef typename Base::size_type size_type;
43  typedef typename Base::difference_type difference_type;
44 
45  template<class...U>
46  Simple_object_pool(U&&...u) : Base(sizeof (T), std::forward<U>(u)...) { }
47 
48  template<class...U>
49  pointer construct(U&&...u) {
50  void* p = base().malloc BOOST_PREVENT_MACRO_SUBSTITUTION();
51  assert(p);
52  try {
53  new(p) T(std::forward<U>(u)...);
54  } catch (...) {
55  base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p);
56  throw;
57  }
58  return static_cast<pointer> (p);
59  }
60 
61  void destroy(pointer p) {
62  p->~T();
63  base().free BOOST_PREVENT_MACRO_SUBSTITUTION(p);
64  }
65 };
66 
67 } // namespace Gudhi
68 
69 #endif // SIMPLE_OBJECT_POOL_H_
GUDHI  Version 3.3.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : MIT Generated on Tue Aug 11 2020 11:58:59 for GUDHI by Doxygen 1.8.18