vsg  1.1.0
VulkanSceneGraph library
Auxiliary.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 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/Object.h>
16 #include <vsg/core/ref_ptr.h>
17 
18 #include <map>
19 #include <mutex>
20 
21 namespace vsg
22 {
23 
25  class VSG_DECLSPEC Auxiliary
26  {
27  public:
28  std::mutex& getMutex() const { return _mutex; }
29 
30  Object* getConnectedObject() { return _connectedObject; }
31  const Object* getConnectedObject() const { return _connectedObject; }
32 
33  virtual std::size_t getSizeOf() const { return sizeof(Auxiliary); }
34 
35  void ref() const;
36  void unref() const;
37  void unref_nodelete() const;
38  inline unsigned int referenceCount() const { return _referenceCount.load(); }
39 
40  virtual int compare(const Auxiliary& rhs) const;
41 
42  void setObject(const std::string& key, ref_ptr<Object> object)
43  {
44  userObjects[key] = object;
45  }
46 
47  Object* getObject(const std::string& key)
48  {
49  if (auto itr = userObjects.find(key); itr != userObjects.end())
50  return itr->second.get();
51  else
52  return nullptr;
53  }
54 
55  const Object* getObject(const std::string& key) const
56  {
57  if (auto itr = userObjects.find(key); itr != userObjects.end())
58  return itr->second.get();
59  else
60  return nullptr;
61  }
62 
63  ref_ptr<Object> getRefObject(const std::string& key)
64  {
65  if (auto itr = userObjects.find(key); itr != userObjects.end())
66  return itr->second;
67  else
68  return {};
69  }
70 
71  ref_ptr<const Object> getRefObject(const std::string& key) const
72  {
73  if (auto itr = userObjects.find(key); itr != userObjects.end())
74  return itr->second;
75  else
76  return {};
77  }
78 
79  using ObjectMap = std::map<std::string, vsg::ref_ptr<Object>>;
80 
82  ObjectMap userObjects;
83 
84  protected:
85  explicit Auxiliary(Object* object);
86 
87  virtual ~Auxiliary();
88 
92 
93  void resetConnectedObject();
94 
95  friend class Object;
96  friend class Allocator;
97 
98  mutable std::atomic_uint _referenceCount;
99 
100  mutable std::mutex _mutex;
101  Object* _connectedObject;
102  };
103 
104 } // namespace vsg
Definition: Allocator.h:41
Definition: Auxiliary.h:26
bool signalConnectedObjectToBeDeleted()
ObjectMap userObjects
container for all user objects
Definition: Auxiliary.h:82
Definition: Object.h:42
Definition: ref_ptr.h:22