clone_cache.h
Go to the documentation of this file.
1 
27 #ifndef TESSERACT_COMMON_OBJECT_CACHE_H
28 #define TESSERACT_COMMON_OBJECT_CACHE_H
29 
32 #include <deque>
33 #include <memory>
34 #include <algorithm>
35 #include <mutex>
36 #include <console_bridge/console.h>
38 
40 
41 namespace tesseract_common
42 {
50 template <typename CacheType>
52 {
53 public:
54  CREATE_MEMBER_FUNC_SIGNATURE_CHECK(update, bool, const std::shared_ptr<const CacheType>&)
56  CREATE_MEMBER_FUNC_SIGNATURE_NOARGS_CHECK(clone, std::shared_ptr<CacheType>)
57 
58  CloneCache(std::shared_ptr<CacheType> original, const long& cache_size = 5)
59  : supports_update(has_member_func_signature_update<CacheType>::value)
60  , original_(std::move(original))
61  , cache_size_(static_cast<std::size_t>(cache_size))
62 
63  {
64  // These methods are required
65  static_assert(has_member_func_signature_getRevision<CacheType>::value,
66  "Class 'getRevision' function has incorrect signature");
67  static_assert(has_member_func_signature_clone<CacheType>::value, "Class 'clone' function has incorrect signature");
68 
69  for (long i = 0; i < cache_size; i++)
70  createClone();
71  }
72 
73  const std::shared_ptr<CacheType>& operator->() { return original_; }
74 
79  std::shared_ptr<CacheType> clone()
80  {
81  if (!original_)
82  return nullptr;
83 
84  if (cache_.empty())
85  {
86  std::shared_ptr<CacheType> cache = getClone();
87  if (cache == nullptr)
88  return nullptr;
89 
90  return cache;
91  }
92 
93  // If the cache needs updating, then update it
94  std::unique_lock<std::mutex> lock(cache_mutex_);
95  if (cache_.back()->getRevision() != original_->getRevision())
96  {
97  // Update if possible
98  std::shared_ptr<CacheType> t;
99  if constexpr (has_member_func_signature_update<CacheType>::value)
100  {
101  cache_.back()->update(original_);
102  }
103  else
104  {
105  std::shared_ptr<CacheType> cache = getClone();
106  if (cache == nullptr)
107  return nullptr;
108 
109  cache_.back() = cache;
110  }
111  t = cache_.back();
112  cache_.pop_back();
113  return t;
114  }
115 
116  std::shared_ptr<CacheType> t;
117  t = cache_.back();
118  cache_.pop_back();
119  return t;
120  }
121 
126  void setCacheSize(long size)
127  {
128  {
129  std::unique_lock<std::mutex> lock(cache_mutex_);
130  cache_size_ = static_cast<std::size_t>(size);
131  }
132  updateCache();
133  }
134 
139  long getCacheSize() const { return static_cast<long>(cache_size_); }
140 
145  long getCurrentCacheSize() const
146  {
147  std::unique_lock<std::mutex> lock(cache_mutex_);
148  return static_cast<long>(cache_.size());
149  }
150 
152  void updateCache()
153  {
154  std::unique_lock<std::mutex> lock(cache_mutex_);
155  if (!original_)
156  return;
157 
158  std::shared_ptr<CacheType> t;
159 
160  // Update all cached objects
161  for (auto& cache : cache_)
162  {
163  if (cache->getRevision() != original_->getRevision())
164  {
165  // Update if possible
166  if constexpr (has_member_func_signature_update<CacheType>::value)
167  {
168  cache->update(original_);
169  }
170  else
171  { // Update is not available to assign new clone
172  cache = getClone();
173  }
174  }
175  }
176 
177  // Prune nullptr
178  cache_.erase(std::remove_if(cache_.begin(),
179  cache_.end(),
180  [](const std::shared_ptr<CacheType>& cache) { return (cache == nullptr); }),
181  cache_.end());
182 
183  while (cache_.size() < cache_size_)
184  {
185  CONSOLE_BRIDGE_logDebug("Adding clone to the cache. Current cache size: %i", cache_.size());
186  std::shared_ptr<CacheType> clone = getClone();
187  if (clone != nullptr)
188  cache_.push_back(clone);
189  }
190  }
191 
192  const bool supports_update; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
193 
194 protected:
195  void createClone()
196  {
197  if (original_ == nullptr)
198  return;
199 
200  std::shared_ptr<CacheType> clone = getClone();
201  if (clone == nullptr)
202  return;
203 
204  // Lock cache
205  std::unique_lock<std::mutex> lock(cache_mutex_);
206  cache_.push_back(clone);
207  }
208 
209  std::shared_ptr<CacheType> getClone() const
210  {
211  std::shared_ptr<CacheType> clone;
212  try
213  {
214  clone = original_->clone();
215  }
216  catch (std::exception& e)
217  {
218  CONSOLE_BRIDGE_logError("Clone Cache failed to update cache with the following exception: %s", e.what());
219  return nullptr;
220  }
221  return clone;
222  }
223 
224  std::shared_ptr<CacheType> original_;
225 
227  std::size_t cache_size_{ 5 };
228 
230  std::deque<std::shared_ptr<CacheType>> cache_;
231 
233  mutable std::mutex cache_mutex_;
234 };
235 
236 } // namespace tesseract_common
237 #endif
tesseract_common::CloneCache::clone
std::shared_ptr< CacheType > clone()
Gets a clone of original_.
Definition: clone_cache.h:79
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::CloneCache::cache_mutex_
std::mutex cache_mutex_
The mutex used when reading and writing to cache_.
Definition: clone_cache.h:233
CREATE_MEMBER_FUNC_SIGNATURE_NOARGS_CHECK
#define CREATE_MEMBER_FUNC_SIGNATURE_NOARGS_CHECK(func_name, return_type)
Check if a member function has a given return type that takes no arguments.
Definition: sfinae_utils.h:193
CREATE_MEMBER_FUNC_SIGNATURE_CHECK
CREATE_MEMBER_FUNC_SIGNATURE_CHECK(add, double, double, double)
sfinae_utils.h
macros.h
Common Tesseract Macros.
tesseract_common::CloneCache::cache_
std::deque< std::shared_ptr< CacheType > > cache_
A vector of cached objects.
Definition: clone_cache.h:230
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::CloneCache::getCacheSize
long getCacheSize() const
Get the set cache size.
Definition: clone_cache.h:139
tesseract_common::CloneCache::setCacheSize
void setCacheSize(long size)
Set the cache size.
Definition: clone_cache.h:126
tesseract_common::CloneCache::updateCache
void updateCache()
If original_ has changed it will update or rebuild the cache of objects.
Definition: clone_cache.h:152
tesseract_common::CloneCache
Used to create a cache of objects.
Definition: clone_cache.h:51
tesseract_common::CloneCache::supports_update
const bool supports_update
Definition: clone_cache.h:192
tesseract_common::CloneCache::original_
std::shared_ptr< CacheType > original_
Definition: clone_cache.h:224
tesseract_common::CloneCache::getCurrentCacheSize
long getCurrentCacheSize() const
Get the current size of the cache.
Definition: clone_cache.h:145
tesseract_common::CloneCache::createClone
void createClone()
Definition: clone_cache.h:195
tesseract_common::CloneCache::cache_size_
std::size_t cache_size_
The assigned cache size.
Definition: clone_cache.h:227
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::CloneCache::operator->
const std::shared_ptr< CacheType > & operator->()
Definition: clone_cache.h:73
tesseract_common::CloneCache::getClone
std::shared_ptr< CacheType > getClone() const
Definition: clone_cache.h:209


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40