vsg  1.0.4
VulkanSceneGraph library
SharedObjects.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2022 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/core/Inherit.h>
16 #include <vsg/core/compare.h>
17 #include <vsg/io/stream.h>
18 
19 #include <map>
20 #include <mutex>
21 #include <ostream>
22 #include <set>
23 
24 namespace vsg
25 {
26 
28  class VSG_DECLSPEC SharedObjects : public Inherit<Object, SharedObjects>
29  {
30  public:
31  SharedObjects();
32 
33  template<class T>
34  ref_ptr<T> shared_default()
35  {
36  std::scoped_lock<std::recursive_mutex> lock(_mutex);
37 
38  auto id = std::type_index(typeid(T));
39  auto& def = _defaults[id];
40  auto def_T = def.cast<T>(); // should be able to do a static cast
41  if (!def_T)
42  {
43  def_T = T::create();
44  auto& shared_objects = _sharedObjects[id];
45  if (auto itr = shared_objects.find(def_T); itr != shared_objects.end())
46  {
47  def_T = (static_cast<T*>(itr->get()));
48  }
49  else
50  {
51  shared_objects.insert(def_T);
52  }
53 
54  def = def_T;
55  }
56 
57  return def_T;
58  }
59 
60  template<class T>
61  void share(ref_ptr<T>& object)
62  {
63  std::scoped_lock<std::recursive_mutex> lock(_mutex);
64 
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())
68  {
69  object = ref_ptr<T>(static_cast<T*>(itr->get()));
70  return;
71  }
72 
73  shared_objects.insert(object);
74  }
75 
76  template<class T, typename Func>
77  void share(ref_ptr<T>& object, Func init)
78  {
79  std::scoped_lock<std::recursive_mutex> lock(_mutex);
80 
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())
84  {
85  object = ref_ptr<T>(static_cast<T*>(itr->get()));
86  return;
87  }
88 
89  init(object);
90 
91  shared_objects.insert(object);
92  }
93 
94  template<class C>
95  void share(C& container)
96  {
97  for (auto& object : container)
98  {
99  share(object);
100  }
101  }
102 
104  std::set<Path> excludedExtensions;
105 
107  virtual bool suitable(const Path& filename) const;
108 
110  virtual bool contains(const Path& filename, ref_ptr<const Options> options = {}) const;
111 
113  virtual void add(ref_ptr<Object> object, const Path& filename, ref_ptr<const Options> options = {});
114 
116  virtual bool remove(const Path& filename, ref_ptr<const Options> options = {});
117 
119  void clear();
120 
121  // clear all the singly referenced objects
122  void prune();
123 
125  void report(std::ostream& out);
126 
127  protected:
128  virtual ~SharedObjects();
129 
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;
133  };
134  VSG_type_name(vsg::SharedObjects);
135 
137  class LoadedObject : public Inherit<Object, LoadedObject>
138  {
139  public:
140  Path filename;
141  ref_ptr<const Options> options;
142  ref_ptr<Object> object;
143 
144  LoadedObject(const Path& in_filename, ref_ptr<const Options> in_options, ref_ptr<Object> in_object = {}) :
145  filename(in_filename),
146  options(in_options),
147  object(in_object) {}
148 
149  int compare(const Object& rhs_object) const override
150  {
151  int result = Object::compare(rhs_object);
152  if (result != 0) return result;
153 
154  auto& rhs = static_cast<decltype(*this)>(rhs_object);
155 
156  if ((result = filename.compare(rhs.filename))) return result;
157  return compare_pointer(options, rhs.options);
158  }
159  };
160  VSG_type_name(vsg::LoadedObject);
161 
162 } // namespace vsg
Definition: Inherit.h:28
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
Definition: Object.h:42
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
Definition: Path.h:32
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.
Definition: ref_ptr.h:22
less functor for comparing ref_ptr<Object> typically used with std::set<> etc.
Definition: compare.h:107