1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_MISC_SINGLETON
18 #define UT_MISC_SINGLETON
20 #include <nel/misc/command.h>
24 NL_INSTANCE_COUNTER_DECL(CSafeSingleton
);
25 NLMISC_SAFE_SINGLETON_DECL(CSafeSingleton
);
29 NL_INSTANCE_COUNTER_IMPL(CSafeSingleton
);
30 NLMISC_SAFE_SINGLETON_IMPL(CSafeSingleton
);
33 class CUnsafeSingleton
35 NL_INSTANCE_COUNTER_DECL(CUnsafeSingleton
);
37 static CUnsafeSingleton
*_Instance
;
40 CUnsafeSingleton(const CUnsafeSingleton
&other
) {}
44 static CUnsafeSingleton
&getInstance()
46 if (_Instance
== NULL
)
47 _Instance
= new CUnsafeSingleton
;
52 NL_INSTANCE_COUNTER_IMPL(CUnsafeSingleton
);
53 CUnsafeSingleton
*CUnsafeSingleton::_Instance
= NULL
;
56 // Test suite for Singleton behavior
57 class CUTMiscSingleton
: public Test::Suite
59 std::string WorkingPath
;
64 TEST_ADD(CUTMiscSingleton::createSingleton
);
65 TEST_ADD(CUTMiscSingleton::accessSingleton
);
66 //TEST_ADD(CUTMiscSingleton::multiDllSingleton);
71 OldPath
= NLMISC::CPath::getCurrentPath();
73 NLMISC::CPath::setCurrentPath(WorkingPath
.c_str());
78 NLMISC::CPath::setCurrentPath(OldPath
.c_str());
81 void createSingleton()
83 TEST_ASSERT(NLMISC::CInstanceCounterManager::getInstance().getInstanceCounter("CSafeSingleton") == 0);
84 CSafeSingleton
&ss
= CSafeSingleton::getInstance();
86 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CSafeSingleton
) == 1);
88 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CUnsafeSingleton
) == 0);
89 CUnsafeSingleton
&us
= CUnsafeSingleton::getInstance();
91 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CUnsafeSingleton
) == 1);
94 void accessSingleton()
96 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CSafeSingleton
) == 1);
97 CSafeSingleton
&ss
= CSafeSingleton::getInstance();
99 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CSafeSingleton
) == 1);
101 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CUnsafeSingleton
) == 1);
102 CUnsafeSingleton
&us
= CUnsafeSingleton::getInstance();
104 TEST_ASSERT(NL_GET_INSTANCE_COUNTER(CUnsafeSingleton
) == 1);
107 /*void multiDllSingleton()
109 TEST_ASSERT(NLMISC::CCommandRegistry::getInstance().exists("aDynLibCommand") == false);
112 if (lib.loadLibrary("dyn_lib_test", true, true, true) != true)
114 TEST_ASSERT_MSG(false, "failed to reload the dll for testing singletons across dlls");
118 TEST_ASSERT(NLMISC::CCommandRegistry::getInstance().isCommand("aDynLibCommand") == true);
120 IDynLibTest *libTest = dynamic_cast<IDynLibTest*>(lib.getNelLibraryInterface());
121 TEST_ASSERT(libTest != NULL);
123 libTest->testSingleton(this);
126 void assertmentWrapper(Test::Source src
)
131 bool continue_after_failureWrapper()
133 return continue_after_failure();
136 friend class CDynLibTest
;