VTK  9.1.0
vtkCGNSCache.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkCGNSCache.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
15
26#ifndef vtkCGNSCache_h
27#define vtkCGNSCache_h
28
29#include "vtkSmartPointer.h"
30
31#include <iterator>
32#include <sstream>
33#include <string>
34#include <unordered_map>
35
36namespace CGNSRead
37{
38// No priority and no size limit right now
39
40template <typename CacheDataType>
42{
43public:
45
46 vtkSmartPointer<CacheDataType> Find(const std::string& query);
47
48 void Insert(const std::string& key, const vtkSmartPointer<CacheDataType>& data);
49
50 void ClearCache();
51
52 void SetCacheSizeLimit(int size);
54
55private:
56 vtkCGNSCache(const vtkCGNSCache&) = delete;
57 void operator=(const vtkCGNSCache&) = delete;
58
59 typedef std::unordered_map<std::string, vtkSmartPointer<CacheDataType>> CacheMapper;
60 CacheMapper CacheData;
61
62 typename CacheMapper::iterator LastCacheAccess;
63
64 int cacheSizeLimit;
65};
66
67template <typename CacheDataType>
69 : CacheData()
70{
71 this->cacheSizeLimit = -1;
72 this->LastCacheAccess = CacheData.end();
73}
74
75template <typename CacheDataType>
77{
78 this->cacheSizeLimit = size;
79}
80
81template <typename CacheDataType>
83{
84 return this->cacheSizeLimit;
85}
86
87template <typename CacheDataType>
89{
90 typename CacheMapper::iterator iter;
91 iter = this->CacheData.find(query);
92 if (iter == this->CacheData.end())
93 return vtkSmartPointer<CacheDataType>(nullptr);
94 this->LastCacheAccess = iter;
95 return iter->second;
96}
97
98template <typename CacheDataType>
100 const std::string& key, const vtkSmartPointer<CacheDataType>& data)
101{
102
103 if (this->cacheSizeLimit > 0 &&
104 this->CacheData.size() >= static_cast<size_t>(this->cacheSizeLimit))
105 {
106 // Make some room by removing last accessed/inserted item
107 this->CacheData.erase(this->LastCacheAccess);
108 }
109 this->CacheData[key] = data;
110 this->LastCacheAccess = this->CacheData.find(key);
111}
112
113template <typename CacheDataType>
115{
116 this->CacheData.clear();
117}
118}
119#endif // vtkCGNSCache_h
120// VTK-HeaderTest-Exclude: vtkCGNSCache.h
vtkSmartPointer< CacheDataType > Find(const std::string &query)
Definition: vtkCGNSCache.h:88
void Insert(const std::string &key, const vtkSmartPointer< CacheDataType > &data)
Definition: vtkCGNSCache.h:99
void SetCacheSizeLimit(int size)
Definition: vtkCGNSCache.h:76
Hold a reference to a vtkObjectBase instance.