15#include <vsg/core/Inherit.h>
16#include <vsg/core/compare.h>
17#include <vsg/io/stream.h>
28 class SuitableForSharing;
42 template<
class T,
typename Func>
46 void share(C& container);
78 mutable std::recursive_mutex _mutex;
79 std::map<std::type_index, ref_ptr<Object>> _defaults;
80 std::map<std::type_index, std::set<ref_ptr<Object>,
DereferenceLess>> _sharedObjects;
91 std::set<const Object*> dynamicObjects;
95 void traverse(
Visitor& visitor)
override;
112 bool suitableForSharing =
true;
114 void apply(
const Object&
object)
override;
115 void apply(
const PagedLOD& plod)
override;
117 bool suitable(
const Object*
object)
119 suitableForSharing =
true;
120 if (
object)
object->accept(*
this);
121 return suitableForSharing;
130 std::scoped_lock<std::recursive_mutex> lock(_mutex);
132 auto id = std::type_index(
typeid(T));
133 auto& def = _defaults[id];
134 auto def_T = def.cast<T>();
138 auto& shared_objects = _sharedObjects[id];
139 if (
auto itr = shared_objects.find(def_T); itr != shared_objects.end())
141 def_T = (
static_cast<T*
>(itr->get()));
145 shared_objects.insert(def_T);
156 void SharedObjects::share(ref_ptr<T>&
object)
158 std::scoped_lock<std::recursive_mutex> lock(_mutex);
162 auto id = std::type_index(
typeid(T));
163 auto& shared_objects = _sharedObjects[id];
164 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
166 object = ref_ptr<T>(
static_cast<T*
>(itr->get()));
170 shared_objects.insert(
object);
174 template<
class T,
typename Func>
175 void SharedObjects::share(ref_ptr<T>&
object, Func init)
178 std::scoped_lock<std::recursive_mutex> lock(_mutex);
180 auto id = std::type_index(
typeid(T));
181 auto& shared_objects = _sharedObjects[id];
182 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
184 object = ref_ptr<T>(
static_cast<T*
>(itr->get()));
192 std::scoped_lock<std::recursive_mutex> lock(_mutex);
193 auto id = std::type_index(
typeid(T));
194 auto& shared_objects = _sharedObjects[id];
197 shared_objects.insert(
object);
204 void SharedObjects::share(C& container)
206 for (
auto&
object : container)
Definition ConstVisitor.h:172
Helper class for sharing of objects loaded from files.
Definition SharedObjects.h:86
int compare(const Object &rhs_object) const override
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
class for facilitating the sharing of instances of objects that have the same properties.
Definition SharedObjects.h:32
void report(std::ostream &out)
write out stats of objects held, types of objects and their reference counts
std::set< Path > excludedExtensions
set of lower case file extensions for file types that should not be included in this SharedObjects
Definition SharedObjects.h:52
virtual bool remove(const Path &filename, ref_ptr< const Options > options={})
remove entry associated with filename.
virtual bool suitable(const Path &filename) const
return true if the filename is of a type suitable for inclusion in this SharedObjects
void clear()
clear all the internal structures leaving no Objects cached.
ref_ptr< SuitableForSharing > suitableForSharing
visitor that checks a loaded object and its children for suitability for sharing in SharedObjects
Definition SharedObjects.h:49
virtual bool contains(const Path &filename, ref_ptr< const Options > options={}) const
check for an entry associated with filename.
virtual void add(ref_ptr< Object > object, const Path &filename, ref_ptr< const Options > options={})
add entry that matches filename and options.
Definition SharedObjects.h:110
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition compare.h:107