surfaceFactoryCache.h
Go to the documentation of this file.
//
// Copyright 2021 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef OPENSUBDIV3_BFR_SURFACE_FACTORY_CACHE_H
#define OPENSUBDIV3_BFR_SURFACE_FACTORY_CACHE_H
#include "../version.h"
#include "../bfr/irregularPatchType.h"
#include <map>
#include <cstdint>
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
namespace Bfr {
//
// Initial/expected use requires simple searches of and additions to the
// cache by the SurfaceFactory or its Builders. Longer term, with the
// possibility of instances of caches being shared between meshes and
// factories, additional options and/or public methods may be warranted
// to limit what is cached or to prune the cache if it gets too large.
//
class SurfaceFactoryCache {
public:
SurfaceFactoryCache();
virtual ~SurfaceFactoryCache();
SurfaceFactoryCache(SurfaceFactoryCache const &) = delete;
SurfaceFactoryCache & operator=(SurfaceFactoryCache const &) = delete;
protected:
// Access restricted to the Factory, its Builders, etc.
friend class SurfaceFactory;
typedef std::uint64_t KeyType;
typedef internal::IrregularPatchSharedPtr DataType;
protected:
size_t Size() const { return _map.size(); }
//
// Potential overrides by subclasses for thread-safety:
//
virtual DataType Find(KeyType const & key) const;
virtual DataType Add(KeyType const & key, DataType const & data);
//
// Common implementation used by all subclasses:
//
DataType find(KeyType const & key) const;
DataType add(KeyType const & key, DataType const & data);
private:
typedef std::map<KeyType, DataType> MapType;
MapType _map;
};
// Separate read and write locks are provided to support mutex types
// allowing shared (read) or exclusive (write) access.
//
template <class MUTEX_TYPE, class READ_LOCK_GUARD_TYPE,
class WRITE_LOCK_GUARD_TYPE>
class SurfaceFactoryCacheThreaded : public SurfaceFactoryCache {
public:
SurfaceFactoryCacheThreaded() : SurfaceFactoryCache() { }
~SurfaceFactoryCacheThreaded() override = default;
protected:
//
// Virtual overrides from base:
//
DataType Find(KeyType const & key) const override {
READ_LOCK_GUARD_TYPE lockGuard(_mutex);
return find(key);
}
DataType Add(KeyType const & key, DataType const & data) override {
WRITE_LOCK_GUARD_TYPE lockGuard(_mutex);
return add(key, data);
}
private:
MUTEX_TYPE mutable _mutex;
};
} // end namespace Bfr
} // end namespace OPENSUBDIV_VERSION
using namespace OPENSUBDIV_VERSION;
} // end namespace OpenSubdiv
#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