YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WeakPtrCache.h
Go to the documentation of this file.
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (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, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #ifndef yap_WeakPtrCache_h
22 #define yap_WeakPtrCache_h
23 
24 #include <algorithm>
25 #include <memory>
26 #include <ostream>
27 #include <set>
28 #include <string>
29 
30 namespace yap {
31 
35 
36 template <class T>
38 {
39 public:
40 
43 
45  using type = T;
46 
49  using shared_ptr_type = std::shared_ptr<T>;
50 
53  using weak_ptr_type = std::weak_ptr<T>;
54 
57  using cache_type = std::set<weak_ptr_type, std::owner_less<weak_ptr_type> >;
58 
60 
62  virtual bool equal(const shared_ptr_type& A, const shared_ptr_type& B) const = 0;
63 
66 
68  WeakPtrCache() = default;
69 
71  WeakPtrCache(const WeakPtrCache&) = default;
72 
74  WeakPtrCache(WeakPtrCache&&) = default;
75 
77  WeakPtrCache(std::vector<shared_ptr_type> V)
78  { for (auto& v : V) operator[](v); }
79 
81  virtual ~WeakPtrCache() = default;
82 
84  WeakPtrCache& operator=(const WeakPtrCache&) = default;
85 
87  WeakPtrCache& operator=(WeakPtrCache&&) = default;
88 
90 
94  {
95  if (!t)
96  return weak_ptr_type();
97 
98  // search for equivalent
99  auto it = std::find_if(Cache_.begin(), Cache_.end(), [&](const weak_ptr_type & w) {return !w.expired() and equal(w.lock(), t);});
100 
101  if (it == Cache_.end())
102  // if not found
103  return weak_ptr_type();
104 
105  return *it;
106  }
107 
111  {
112  auto w = find(t);
113 
114  // if w is valid (t is found in cache)
115  if (!w.expired())
116  return w.lock();
117 
118  // else add to cache
119  addToCache(t);
120  return t;
121  }
122 
124  bool empty() const
125  { return Cache_.empty(); }
126 
128  size_t size() const
129  { return Cache_.size(); }
130 
132  size_t count_expired() const
133  { return std::count_if(Cache_.begin(), Cache_.end(), [](const weak_ptr_type & w) {return w.expired();}); }
134 
137  {
138  for (auto it = Cache_.begin(); it != Cache_.end(); ) {
139  if (it->expired())
140  it = Cache_.erase(it);
141  else
142  it++;
143  }
144  }
145 
148 
150  typename cache_type::iterator begin()
151  { return Cache_.begin(); }
152 
154  typename cache_type::const_iterator begin() const
155  { return Cache_.begin(); }
156 
158  typename cache_type::iterator end()
159  { return Cache_.end(); }
160 
162  typename cache_type::const_iterator end() const
163  { return Cache_.end(); }
164 
166 
168  virtual std::ostream& print(std::ostream& os) const
169  {
170  for (auto& w : *this)
171  if (!w.expired())
172  os << *w.lock() << std::endl;
173  return os;
174  }
175 
176 protected:
177 
179  virtual void addToCache(shared_ptr_type t)
180  { Cache_.emplace(t); }
181 
182 private:
183 
186 
187 };
188 
190 template <class T>
191 inline std::ostream& operator<<(std::ostream& os, const WeakPtrCache<T>& C)
192 {
193  os << "contains " << C.size() << " elements, of which "
194  << C.count_expired() << " have expired" << std::endl;
195 
196  return C.print(os);
197 }
198 
199 }
200 
201 #endif
std::shared_ptr< SpinAmplitude > shared_ptr_type
std::shared_ptr to T
Definition: WeakPtrCache.h:49
Template for a cache of weak_ptr's to objects.
Definition: WeakPtrCache.h:37
WeakPtrCache()=default
Default constructor (defaulted)
WeakPtrCache(std::vector< shared_ptr_type > V)
Construct cache from vector.
Definition: WeakPtrCache.h:77
std::set< weak_ptr_type, std::owner_less< weak_ptr_type > > cache_type
A std::set of weak_ptr_type.
Definition: WeakPtrCache.h:57
size_t size() const
Definition: WeakPtrCache.h:128
WeakPtrCache & operator=(const WeakPtrCache &)=default
copy assignment operator (defaulted)
virtual std::ostream & print(std::ostream &os) const
stream the cache elements as a table
Definition: WeakPtrCache.h:168
cache_type Cache_
set of weak pointers to objects
Definition: WeakPtrCache.h:185
bool empty() const
Definition: WeakPtrCache.h:124
virtual bool equal(const shared_ptr_type &A, const shared_ptr_type &B) const =0
override to implement equality checking
void removeExpired()
remove expired Cache_ elements
Definition: WeakPtrCache.h:136
cache_type::iterator begin()
Definition: WeakPtrCache.h:150
cache_type::const_iterator begin() const
Definition: WeakPtrCache.h:154
std::weak_ptr< SpinAmplitude > weak_ptr_type
std::weak_ptr to T
Definition: WeakPtrCache.h:53
cache_type::iterator end()
Definition: WeakPtrCache.h:158
shared_ptr_type operator[](shared_ptr_type t)
Definition: WeakPtrCache.h:110
virtual ~WeakPtrCache()=default
virtual desctructor (defaulted)
cache_type::const_iterator end() const
Definition: WeakPtrCache.h:162
virtual void addToCache(shared_ptr_type t)
add element to cache
Definition: WeakPtrCache.h:179
weak_ptr_type find(shared_ptr_type t) const
Definition: WeakPtrCache.h:93
size_t count_expired() const
Definition: WeakPtrCache.h:132
Abstract base class implementing a spin amplitude.
Definition: SpinAmplitude.h:46