Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//pool.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//pool.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with pool.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2021 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// silviana amethyst, university of wisconsin-eau claire
24//
25
29
30
31#ifndef BERTINI_GENERIC_POOL_HPP
32#define BERTINI_GENERIC_POOL_HPP
33
34namespace bertini {
35
36 namespace detail {
37
38
39 template<typename ObjT>
40 struct SharedPointerPolicy
41 {
42 using PointerType = std::shared_ptr<ObjT>;
43
44 static
45 PointerType FromObj(ObjT d)
46 {
47 return std::make_shared<ObjT>(std::move(d));
48 }
49
50 static
51 PointerType DefaultConstructed()
52 {
53 return std::make_shared<ObjT>();
54 }
55
56 };
57
58
59 template<typename ObjT>
60 using DefaultPointerPolicy = SharedPointerPolicy<ObjT>;
61
62
63
64 template<typename ObjT, class PointerPolicy = DefaultPointerPolicy<ObjT> >
65 class Pool
66 {
67
68
69
70
71 public:
72
73 using HeldType = typename PointerPolicy::PointerType;
74 using PoolHolderType = std::vector< HeldType >;
75
76 static
77 HeldType NonPtrAdd(ObjT d)
78 {
79 // held_data_.push_back(PointerPolicy::FromObj(d));
80 held_data_.push_back(PointerPolicy::FromObj(d));
81 return held_data_.back();
82 }
83
84 static
85 HeldType PtrAdd(HeldType d)
86 {
87 held_data_.push_back(d);
88 return held_data_.back();
89 }
90
91 template<typename ... Ts>
92 static
93 HeldType Make(Ts&& ...ts )
94 {
95 ObjT* obj = new ObjT(ts...);
96
97 // held_data_.push_back(PointerPolicy::DefaultConstructed());
98 auto thing = std::shared_ptr<ObjT>(new ObjT(ts...));
99 held_data_.push_back(thing);
100 return held_data_.back();
101 }
102
103 static
104 void PurgeCache()
105 {
106 std::remove_if(held_data_.begin(), held_data_.end(), [](HeldType const& h){return h.use_count()==1;});
107 }
108
109 private:
110 static
111 PoolHolderType held_data_;
112 };
113
114 template<typename ObjT, class Policy> typename Pool<ObjT,Policy>::PoolHolderType Pool<ObjT,Policy>::held_data_;
115 } // re: detail
116} // re: bertini
117
118#endif