TestSingleton.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (c) Electronic Arts Inc. All rights reserved.
  3. ///////////////////////////////////////////////////////////////////////////////
  4. #include <EAStdC/EASingleton.h>
  5. #include <EAStdCTest/EAStdCTest.h>
  6. #include <EATest/EATest.h>
  7. #ifdef _MSC_VER
  8. #pragma warning(push, 0)
  9. #endif
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #ifdef _MSC_VER
  13. #pragma warning(pop)
  14. #endif
  15. class Widget
  16. {
  17. public:
  18. Widget(){}
  19. Widget(const Widget&){}
  20. ~Widget(){}
  21. Widget& operator=(const Widget&) { return *this; }
  22. };
  23. class WidgetS : public EA::StdC::Singleton<WidgetS> // Note that this class inherits from a template of itself.
  24. {
  25. WidgetS(const WidgetS&); // Used to placate VC++ compiler warning C4625
  26. WidgetS& operator=(const WidgetS&); // Used to placate VC++ compiler warning C4626
  27. };
  28. class WidgetSA : public EA::StdC::SingletonAdapter<Widget, true>
  29. {
  30. WidgetSA(const WidgetSA&); // Used to placate VC++ compiler warning C4625
  31. WidgetSA& operator=(const WidgetSA&); // Used to placate VC++ compiler warning C4626
  32. };
  33. int TestSingleton()
  34. {
  35. int nErrorCount(0);
  36. EA::UnitTest::Report("TestSingleton\n");
  37. // Singleton
  38. WidgetS* pWidgetS = WidgetS::GetInstance();
  39. EATEST_VERIFY(pWidgetS == NULL);
  40. // SingletonImplicit
  41. Widget* pWidget = WidgetSA::GetInstance();
  42. EATEST_VERIFY(pWidget != NULL);
  43. WidgetSA::DestroyInstance();
  44. // SingletonExplicit
  45. pWidget = WidgetSA::GetInstance();
  46. EATEST_VERIFY(pWidget != NULL);
  47. pWidget = WidgetSA::CreateInstance("SomeName");
  48. EATEST_VERIFY(pWidget != NULL);
  49. pWidget = WidgetSA::GetInstance();
  50. EATEST_VERIFY(pWidget != NULL);
  51. WidgetSA::DestroyInstance();
  52. return nErrorCount;
  53. }