VTK  9.4.20241117
vtkCGNSCache.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3
14#ifndef vtkCGNSCache_h
15#define vtkCGNSCache_h
16
17#include "vtkSmartPointer.h"
18
19#include <iterator>
20#include <sstream>
21#include <string>
22#include <unordered_map>
23
24namespace CGNSRead
25{
26VTK_ABI_NAMESPACE_BEGIN
27// No priority and no size limit right now
28
29template <typename CacheDataType>
31{
32public:
34
35 vtkSmartPointer<CacheDataType> Find(const std::string& query);
36
37 void Insert(const std::string& key, const vtkSmartPointer<CacheDataType>& data);
38
39 void ClearCache();
40
41 void SetCacheSizeLimit(int size);
43
44private:
45 vtkCGNSCache(const vtkCGNSCache&) = delete;
46 void operator=(const vtkCGNSCache&) = delete;
47
48 typedef std::unordered_map<std::string, vtkSmartPointer<CacheDataType>> CacheMapper;
49 CacheMapper CacheData;
50
51 typename CacheMapper::iterator LastCacheAccess;
52
53 int cacheSizeLimit;
54};
55
56template <typename CacheDataType>
58 : CacheData()
59{
60 this->cacheSizeLimit = -1;
61 this->LastCacheAccess = CacheData.end();
62}
63
64template <typename CacheDataType>
66{
67 this->cacheSizeLimit = size;
68}
69
70template <typename CacheDataType>
72{
73 return this->cacheSizeLimit;
74}
75
76template <typename CacheDataType>
78{
79 typename CacheMapper::iterator iter;
80 iter = this->CacheData.find(query);
81 if (iter == this->CacheData.end())
82 return vtkSmartPointer<CacheDataType>(nullptr);
83 this->LastCacheAccess = iter;
84 return iter->second;
85}
86
87template <typename CacheDataType>
89 const std::string& key, const vtkSmartPointer<CacheDataType>& data)
90{
91
92 if (this->cacheSizeLimit > 0 &&
93 this->CacheData.size() >= static_cast<size_t>(this->cacheSizeLimit))
94 {
95 // Make some room by removing last accessed/inserted item
96 this->CacheData.erase(this->LastCacheAccess);
97 }
98 this->CacheData[key] = data;
99 this->LastCacheAccess = this->CacheData.find(key);
100}
101
102template <typename CacheDataType>
104{
105 this->CacheData.clear();
106}
107VTK_ABI_NAMESPACE_END
108}
109#endif // vtkCGNSCache_h
110// VTK-HeaderTest-Exclude: vtkCGNSCache.h
vtkSmartPointer< CacheDataType > Find(const std::string &query)
void Insert(const std::string &key, const vtkSmartPointer< CacheDataType > &data)
void SetCacheSizeLimit(int size)
Hold a reference to a vtkObjectBase instance.
This file defines functions used by vtkCGNSReader and vtkCGNSReaderInternal.