File tree Expand file tree Collapse file tree 2 files changed +15
-10
lines changed
Expand file tree Collapse file tree 2 files changed +15
-10
lines changed Original file line number Diff line number Diff line change 22#ifndef INC_SINGLETON_H_
33#define INC_SINGLETON_H_
44
5+ #include < cstdlib>
56#include < mutex>
67#include < typeindex>
78#include < memory>
@@ -44,15 +45,15 @@ class Singleton {
4445private:
4546 // Get the single instance
4647 static T *getInstancePrivate () {
47- static T *instance = nullptr ;
48- if (instance != nullptr )
48+ static T *instance = NULL ;
49+ if (instance != NULL )
4950 return instance;
5051
51- SingleTonHolder *singleTonHolder = nullptr ;
52+ SingleTonHolder *singleTonHolder = NULL ;
5253 {
5354 // Locks and get the global mutex
5455 std::lock_guard<std::mutex> myLock (getSingleTonMutex ());
55- if (instance != nullptr )
56+ if (instance != NULL )
5657 return instance;
5758
5859 singleTonHolder = getSingleTonType (std::type_index (typeid (T)));
@@ -72,7 +73,7 @@ class Singleton {
7273 static T *createInstanceFromHolder (SingleTonHolder *singleTonHolder) {
7374 // Locks class T and make sure to call construction only once
7475 std::lock_guard<std::mutex> myLock (*singleTonHolder->mutex_ );
75- if (singleTonHolder->object_ == nullptr ) {
76+ if (singleTonHolder->object_ == NULL ) {
7677 // construct the instance with static funciton
7778 singleTonHolder->object_ = reinterpret_cast <void *>(getStaticInstance ());
7879 }
Original file line number Diff line number Diff line change @@ -15,15 +15,19 @@ SINGLETON_API SingleTonHolder *getSingleTonType(const std::type_index &typeIndex
1515 static std::unordered_map<std::type_index, SingleTonHolder> s_singleObjects;
1616
1717 // Check the old value
18- auto itr = s_singleObjects.find (typeIndex);
18+ std::unordered_map<std::type_index, SingleTonHolder>::iterator itr = s_singleObjects.find (typeIndex);
1919 if (itr != s_singleObjects.end ())
2020 return &itr->second ;
2121
2222 // Create new one if no old value
23- std::pair<std::type_index, SingleTonHolder> singleHolder {
23+ std::pair<std::type_index, SingleTonHolder> singleHolder (
2424 typeIndex,
25- SingleTonHolder{ nullptr , std::shared_ptr<std::mutex>( new std::mutex ())}
26- } ;
25+ SingleTonHolder ()
26+ ) ;
2727 itr = s_singleObjects.insert (singleHolder).first ;
28- return &itr->second ;
28+ SingleTonHolder &singleTonHolder = itr->second ;
29+ singleTonHolder.object_ = NULL ;
30+ singleTonHolder.mutex_ = std::shared_ptr<std::mutex>(new std::mutex ());
31+
32+ return &singleTonHolder;
2933}
You can’t perform that action at this time.
0 commit comments