| OpenSubdiv OpenSubdiv

Loading...
Searching...
No Matches
surfaceFactoryCache.h
Go to the documentation of this file.
1//
2// Copyright 2021 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24
25#ifndef OPENSUBDIV3_BFR_SURFACE_FACTORY_CACHE_H
26#define OPENSUBDIV3_BFR_SURFACE_FACTORY_CACHE_H
27
28#include "../version.h"
29
30#include "../bfr/irregularPatchType.h"
31
32#include <map>
33#include <cstdint>
34
35 namespace OpenSubdiv {
36 namespace OPENSUBDIV_VERSION {
37
38 namespace Bfr {
39
52//
53// Initial/expected use requires simple searches of and additions to the
54// cache by the SurfaceFactory or its Builders. Longer term, with the
55// possibility of instances of caches being shared between meshes and
56// factories, additional options and/or public methods may be warranted
57// to limit what is cached or to prune the cache if it gets too large.
58//
59 class SurfaceFactoryCache {
60 public:
61 SurfaceFactoryCache();
62 virtual ~SurfaceFactoryCache();
63
64 SurfaceFactoryCache(SurfaceFactoryCache const &) = delete;
65 SurfaceFactoryCache & operator=(SurfaceFactoryCache const &) = delete;
66
67 protected:
69 // Access restricted to the Factory, its Builders, etc.
70 friend class SurfaceFactory;
71
72 typedef std::uint64_t KeyType;
73 typedef internal::IrregularPatchSharedPtr DataType;
75
76 protected:
78 size_t Size() const { return _map.size(); }
79
80 //
81 // Potential overrides by subclasses for thread-safety:
82 //
83 virtual DataType Find(KeyType const & key) const;
84 virtual DataType Add(KeyType const & key, DataType const & data);
85
86 //
87 // Common implementation used by all subclasses:
88 //
89 DataType find(KeyType const & key) const;
90 DataType add(KeyType const & key, DataType const & data);
92
93 private:
94 typedef std::map<KeyType, DataType> MapType;
95
96 MapType _map;
97};
98
113// Separate read and write locks are provided to support mutex types
114// allowing shared (read) or exclusive (write) access.
115//
116 template <class MUTEX_TYPE, class READ_LOCK_GUARD_TYPE,
117 class WRITE_LOCK_GUARD_TYPE>
118 class SurfaceFactoryCacheThreaded : public SurfaceFactoryCache {
119 public:
120 SurfaceFactoryCacheThreaded() : SurfaceFactoryCache() { }
121 ~SurfaceFactoryCacheThreaded() override = default;
122
123 protected:
125 //
126 // Virtual overrides from base:
127 //
128 DataType Find(KeyType const & key) const override {
129 READ_LOCK_GUARD_TYPE lockGuard(_mutex);
130 return find(key);
131 }
132
133 DataType Add(KeyType const & key, DataType const & data) override {
134 WRITE_LOCK_GUARD_TYPE lockGuard(_mutex);
135 return add(key, data);
136 }
138
139 private:
140 MUTEX_TYPE mutable _mutex;
141};
142
143} // end namespace Bfr
144
145} // end namespace OPENSUBDIV_VERSION
146 using namespace OPENSUBDIV_VERSION;
147
148} // end namespace OpenSubdiv
149
150#endif /* OPENSUBDIV3_BFR_SURFACE_FACTORY_CACHE_H */
OpenSubdiv
Definition limits.h:32
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactory
Base class providing initialization of a Surface for each face of a mesh.
Definition surfaceFactory.h:103
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCache
Container used internally by SurfaceFactory to store reusable information.
Definition surfaceFactoryCache.h:59
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCache::SurfaceFactoryCache
SurfaceFactoryCache()
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCache::~SurfaceFactoryCache
virtual ~SurfaceFactoryCache()
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCache::SurfaceFactoryCache
SurfaceFactoryCache(SurfaceFactoryCache const &)=delete
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCache::operator=
SurfaceFactoryCache & operator=(SurfaceFactoryCache const &)=delete
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCacheThreaded
Template for declaring thread-safe subclasses of SurfaceFactoryCache.
Definition surfaceFactoryCache.h:118
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCacheThreaded::SurfaceFactoryCacheThreaded
SurfaceFactoryCacheThreaded()
Definition surfaceFactoryCache.h:120
OpenSubdiv::OPENSUBDIV_VERSION::Bfr::SurfaceFactoryCacheThreaded::~SurfaceFactoryCacheThreaded
~SurfaceFactoryCacheThreaded() override=default