15 #include <vsg/core/Inherit.h>
16 #include <vsg/core/compare.h>
17 #include <vsg/io/stream.h>
36 std::scoped_lock<std::recursive_mutex> lock(_mutex);
38 auto id = std::type_index(
typeid(T));
39 auto& def = _defaults[id];
40 auto def_T = def.cast<T>();
44 auto& shared_objects = _sharedObjects[id];
45 if (
auto itr = shared_objects.find(def_T); itr != shared_objects.end())
47 def_T = (
static_cast<T*
>(itr->get()));
51 shared_objects.insert(def_T);
63 std::scoped_lock<std::recursive_mutex> lock(_mutex);
65 auto id = std::type_index(
typeid(T));
66 auto& shared_objects = _sharedObjects[id];
67 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
69 object =
ref_ptr<T>(
static_cast<T*
>(itr->get()));
73 shared_objects.insert(
object);
76 template<
class T,
typename Func>
79 std::scoped_lock<std::recursive_mutex> lock(_mutex);
81 auto id = std::type_index(
typeid(T));
82 auto& shared_objects = _sharedObjects[id];
83 if (
auto itr = shared_objects.find(
object); itr != shared_objects.end())
85 object =
ref_ptr<T>(
static_cast<T*
>(itr->get()));
91 shared_objects.insert(
object);
95 void share(C& container)
97 for (
auto&
object : container)
130 mutable std::recursive_mutex _mutex;
131 std::map<std::type_index, ref_ptr<Object>> _defaults;
132 std::map<std::type_index, std::set<ref_ptr<Object>,
DereferenceLess>> _sharedObjects;
145 filename(in_filename),
152 if (result != 0)
return result;
154 auto& rhs =
static_cast<decltype(*this)
>(rhs_object);
156 if ((result = filename.compare(rhs.filename)))
return result;
157 return compare_pointer(options, rhs.options);
Helper class for support sharing of objects loaded from files.
Definition: SharedObjects.h:138
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: SharedObjects.h:149
virtual int compare(const Object &rhs) const
compare two objects, return -1 if this object is less than rhs, return 0 if it's equal,...
Definition: Object.h:77
class for facilitating the share of instances of objects that have the same properties.
Definition: SharedObjects.h:29
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:104
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 this SharedObjects
void clear()
clear all the internal structures leaving no Objects cached.
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 option.
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition: compare.h:107