Changeset abab7e for molecuilder/src
- Timestamp:
- Mar 11, 2010, 11:32:17 AM (15 years ago)
- Children:
- 8a4f12
- Parents:
- ff1812
- Location:
- molecuilder/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/Patterns/Singleton.hpp
rff1812 rabab7e 87 87 88 88 private: 89 static boost:: mutex instanceLock;89 static boost::recursive_mutex instanceLock; 90 90 static ptr_t theInstance; 91 91 }; -
molecuilder/src/Patterns/Singleton_impl.hpp
rff1812 rabab7e 10 10 11 11 #include "Patterns/Singleton.hpp" 12 13 /****** Static instance Variables of the template *******/ 14 15 template <class T,bool _may_create> 16 typename Singleton<T,_may_create>::ptr_t Singleton<T,_may_create>::theInstance(0); 17 18 template <class T,bool _may_create> 19 boost::recursive_mutex Singleton<T,_may_create>::instanceLock; 20 21 /****** templates singleton creation and destruction functions ******/ 22 23 template <class T,bool _may_create> 24 T& Singleton<T,_may_create>::getInstance(){ 25 // boost supports RAII-Style locking, so we don't need to unlock 26 boost::recursive_mutex::scoped_lock guard(instanceLock); 27 if(!theInstance.get()) { 28 theInstance.reset(creator::make()); 29 } 30 return *theInstance; 31 } 32 33 template <class T,bool _may_create> 34 T* Singleton<T,_may_create>::getPointer(){ 35 // boost supports RAII-Style locking, so we don't need to unlock 36 boost::recursive_mutex::scoped_lock guard(instanceLock); 37 if(!theInstance.get()) { 38 theInstance.reset(creator::make()); 39 } 40 return theInstance.get(); 41 42 } 43 44 template <class T,bool _may_create> 45 void Singleton<T,_may_create>::purgeInstance(){ 46 // boost supports RAII-Style locking, so we don't need to unlock 47 boost::recursive_mutex::scoped_lock guard(instanceLock); 48 theInstance.reset(); 49 } 50 51 template <class T,bool _may_create> 52 T& Singleton<T,_may_create>::resetInstance(){ 53 ptr_t oldInstance; 54 { 55 // boost supports RAII-Style locking, so we don't need to unlock 56 boost::recursive_mutex::scoped_lock guard(instanceLock); 57 58 oldInstance = theInstance; 59 theInstance.reset(creator::make()); 60 // oldworld does not need protection any more, 61 // since we should have the only reference 62 63 // worldLock handles access to the pointer, 64 // not to the object 65 } // scope-end releases the lock 66 67 // oldInstance goes out of scope at the End of this function. The carried object will then be destroyed by the auto_ptr 68 return *theInstance; 69 } 70 71 72 template <class T,bool _may_create> 73 void Singleton<T,_may_create>::setInstance(T* newInstance){ 74 assert(!theInstance.get() && "Trying to set the instance of an already created singleton"); 75 boost::recursive_mutex::scoped_lock guard(instanceLock); 76 theInstance.reset(newInstance); 77 } 78 79 /** 80 * This define allows simple instantiation of the necessary singleton functions 81 * at a chosen place. 82 */ 83 #define CONSTRUCT_SINGLETON(name) \ 84 template name& Singleton< name , name::may_create >::getInstance(); \ 85 template name* Singleton< name , name::may_create >::getPointer(); \ 86 template void Singleton< name , name::may_create >::purgeInstance(); \ 87 template name& Singleton< name , name::may_create >::resetInstance(); \ 88 template void Singleton< name , name::may_create >::setInstance( name* ); 89 90 /************ Internal Pointer Wrapper to allow automatic purging *************/ 12 91 13 92 template <class T,bool _may_create> … … 51 130 } 52 131 53 template <class T,bool _may_create>54 typename Singleton<T,_may_create>::ptr_t Singleton<T,_may_create>::theInstance(0);55 56 template <class T,bool _may_create>57 boost::mutex Singleton<T,_may_create>::instanceLock;58 59 template <class T,bool _may_create>60 T& Singleton<T,_may_create>::getInstance(){61 // boost supports RAII-Style locking, so we don't need to unlock62 boost::mutex::scoped_lock guard(instanceLock);63 if(!theInstance.get()) {64 theInstance.reset(creator::make());65 }66 return *theInstance;67 }68 69 template <class T,bool _may_create>70 T* Singleton<T,_may_create>::getPointer(){71 // boost supports RAII-Style locking, so we don't need to unlock72 boost::mutex::scoped_lock guard(instanceLock);73 if(!theInstance.get()) {74 theInstance.reset(creator::make());75 }76 return theInstance.get();77 78 }79 80 template <class T,bool _may_create>81 void Singleton<T,_may_create>::purgeInstance(){82 // boost supports RAII-Style locking, so we don't need to unlock83 boost::mutex::scoped_lock guard(instanceLock);84 theInstance.reset();85 }86 87 template <class T,bool _may_create>88 T& Singleton<T,_may_create>::resetInstance(){89 ptr_t oldInstance;90 {91 // boost supports RAII-Style locking, so we don't need to unlock92 boost::mutex::scoped_lock guard(instanceLock);93 94 oldInstance = theInstance;95 theInstance.reset(creator::make());96 // oldworld does not need protection any more,97 // since we should have the only reference98 99 // worldLock handles access to the pointer,100 // not to the object101 } // scope-end releases the lock102 103 // oldInstance goes out of scope at the End of this function. The carried object will then be destroyed by the auto_ptr104 return *theInstance;105 }106 107 108 template <class T,bool _may_create>109 void Singleton<T,_may_create>::setInstance(T* newInstance){110 assert(!theInstance.get() && "Trying to set the instance of an already created singleton");111 boost::mutex::scoped_lock guard(instanceLock);112 theInstance.reset(newInstance);113 }114 115 #define CONSTRUCT_SINGLETON(name) \116 template name& Singleton< name , name::may_create >::getInstance(); \117 template name* Singleton< name , name::may_create >::getPointer(); \118 template void Singleton< name , name::may_create >::purgeInstance(); \119 template name& Singleton< name , name::may_create >::resetInstance(); \120 template void Singleton< name , name::may_create >::setInstance( name* );121 132 122 133 #endif /* SINGLETON_IMPL_HPP_ */ -
molecuilder/src/unittests/Makefile.am
rff1812 rabab7e 28 28 MoleculeDescriptorTest \ 29 29 ObserverTest \ 30 SingletonTest \ 30 31 StackClassUnitTest \ 31 32 TesselationUnitTest \ … … 67 68 MoleculeDescriptorTest.cpp \ 68 69 ObserverTest.cpp \ 70 SingletonTest.cpp \ 69 71 stackclassunittest.cpp \ 70 72 tesselationunittest.cpp \ … … 124 126 MoleculeDescriptorTest_LDADD = ${ALLLIBS} 125 127 128 SingletonTest_SOURCES = UnitTestMain.cpp SingletonTest.cpp SingletonTest.hpp 129 SingletonTest_LDADD = $(BOOST_LIB) ${BOOST_THREAD_LIB} 130 126 131 StackClassUnitTest_SOURCES = UnitTestMain.cpp stackclassunittest.cpp stackclassunittest.hpp 127 132 StackClassUnitTest_LDADD = ${ALLLIBS} -
molecuilder/src/unittests/atomsCalculationTest.cpp
rff1812 rabab7e 23 23 #include "World_calculations.hpp" 24 24 #include "atom.hpp" 25 26 #ifdef HAVE_TESTRUNNER 27 #include "UnitTestMain.hpp" 28 #endif /*HAVE_TESTRUNNER*/ 25 29 26 30 // Registers the fixture into the 'registry' -
molecuilder/src/unittests/manipulateAtomsTest.cpp
rff1812 rabab7e 21 21 #include "World.hpp" 22 22 #include "atom.hpp" 23 24 #ifdef HAVE_TESTRUNNER 25 #include "UnitTestMain.hpp" 26 #endif /*HAVE_TESTRUNNER*/ 23 27 24 28 // Registers the fixture into the 'registry'
Note:
See TracChangeset
for help on using the changeset viewer.