OpenSubdiv
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 //
60 public:
62  virtual ~SurfaceFactoryCache();
63 
64  SurfaceFactoryCache(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>
119 public:
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 */
Base class providing initialization of a Surface for each face of a mesh.
Container used internally by SurfaceFactory to store reusable information.
SurfaceFactoryCache & operator=(SurfaceFactoryCache const &)=delete
Template for declaring thread-safe subclasses of SurfaceFactoryCache.