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;
94 void traverse(
Visitor& visitor)
override;
111 bool suitableForSharing =
true;
113 void apply(
const Object&
object)
override;
114 void apply(
const PagedLOD& plod)
override;
116 bool suitable(
const Object*
object)
118 suitableForSharing =
true;
119 if (
object)
object->accept(*
this);
120 return suitableForSharing;
129 std::scoped_lock<std::recursive_mutex> lock(_mutex);
131 auto id = std::type_index(
typeid(T));
132 auto& def = _defaults[id];
133 auto def_T = def.cast<T>();
137 auto& shared_objects = _sharedObjects[id];
138 if (
auto itr = shared_objects.find(def_T); itr != shared_objects.end())
140 def_T = (
static_cast<T*
>(itr->get()));
144 shared_objects.insert(def_T);
155 void SharedObjects::share(ref_ptr<T>&
object)
157 std::scoped_lock<std::recursive_mutex> lock(_mutex);
161 auto id = std::type_index(
typeid(T));
162 auto& shared_objects = _sharedObjects[id];
163 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
165 object = ref_ptr<T>(
static_cast<T*
>(itr->get()));
169 shared_objects.insert(
object);
173 template<
class T,
typename Func>
174 void SharedObjects::share(ref_ptr<T>&
object, Func init)
177 std::scoped_lock<std::recursive_mutex> lock(_mutex);
179 auto id = std::type_index(
typeid(T));
180 auto& shared_objects = _sharedObjects[id];
181 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
183 object = ref_ptr<T>(
static_cast<T*
>(itr->get()));
191 std::scoped_lock<std::recursive_mutex> lock(_mutex);
192 auto id = std::type_index(
typeid(T));
193 auto& shared_objects = _sharedObjects[id];
196 shared_objects.insert(
object);
203 void SharedObjects::share(C& container)
205 for (
auto&
object : container)
Definition: ConstVisitor.h:147
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,...
Definition: PagedLOD.h:36
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:109
Definition: Visitor.h:147
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition: compare.h:107