SharedInstance-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_SHAREDINSTANCE_INL_H
17 #define SURGSIM_FRAMEWORK_SHAREDINSTANCE_INL_H
18 
19 #include <memory>
20 #include <functional>
21 
22 #include <boost/thread/mutex.hpp>
23 #include <boost/thread/locks.hpp>
24 
26 
27 
28 namespace SurgSim
29 {
30 namespace Framework
31 {
32 
33 template <typename T>
35  m_instanceCreator(defaultInstanceCreator())
36 {
37 }
38 
39 template <typename T>
41  m_instanceCreator(instanceCreator)
42 {
43 }
44 
45 template <typename T>
47 {
48 }
49 
50 template <typename T>
51 std::shared_ptr<T> SharedInstance<T>::get()
52 {
53  boost::lock_guard<boost::mutex> lock(m_mutex);
54  std::shared_ptr<T> instance = m_weakInstance.lock();
55  if (! instance)
56  {
57  instance = createInstance();
58  m_weakInstance = instance;
59  }
60  return std::move(instance);
61 }
62 
63 template <typename T>
65 {
66  std::shared_ptr<T> instance = m_instanceCreator();
67  SURGSIM_ASSERT(instance);
68  return std::move(instance);
69 }
70 
71 template <typename T>
73 {
74  return []() { return std::make_shared<T>(); }; // NOLINT(readability/braces)
75 }
76 
77 
78 }; // namespace Framework
79 }; // namespace SurgSim
80 
81 #endif // SURGSIM_FRAMEWORK_SHAREDINSTANCE_INL_H
Definition: CompoundShapeToGraphics.cpp:29
std::weak_ptr< T > m_weakInstance
A weak reference to the shared instance, if any.
Definition: SharedInstance.h:155
~SharedInstance()
Destroy the container and the data it contains.
Definition: SharedInstance-inl.h:46
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
boost::mutex m_mutex
Mutex for synchronization of object creation.
Definition: SharedInstance.h:158
static InstanceCreator defaultInstanceCreator()
Creates a function that can create an instance using std::make_shared<T>().
Definition: SharedInstance-inl.h:72
SharedInstance()
Create the SharedInstance object used to manage the shared instance.
Definition: SharedInstance-inl.h:34
InstanceCreator m_instanceCreator
A creator function used to construct the shared instance.
Definition: SharedInstance.h:152
The header that provides the assertion API.
std::shared_ptr< T > get()
Gets the shared object instance.
Definition: SharedInstance-inl.h:51
std::function< std::shared_ptr< T >)> InstanceCreator
A type that can hold a function object or lambda that takes no arguments and returns std::shared_ptr<...
Definition: SharedInstance.h:109
std::shared_ptr< T > createInstance()
Creates an object instance.
Definition: SharedInstance-inl.h:64