test_singleton_plain.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_singleton_plain.cpp:
  3. // Test the singleton class for a "plain" singleton (used as singleton<Foo>)
  4. //
  5. // - is_destroyed returns false when singleton is active or uninitialized
  6. // - is_destroyed returns true when singleton is destructed
  7. // - the singleton is eventually destructed (no memory leak)
  8. // (C) Copyright 2018 Alexander Grund
  9. // Use, modification and distribution is subject to the Boost Software
  10. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #include "test_tools.hpp"
  13. #include <boost/serialization/singleton.hpp>
  14. #include <boost/preprocessor/stringize.hpp>
  15. #include <stdexcept>
  16. // Can't use BOOST_TEST because:
  17. // a) destructors are called after program exit
  18. // b) This is intended to be used by shared libraries too which would then need their own report_errors call
  19. // We halso have to disable the Wterminate warning as we call this from dtors
  20. // C++ will terminate the program in such cases which is OK here
  21. #pragma GCC diagnostic push
  22. #pragma GCC diagnostic ignored "-Wterminate"
  23. #define THROW_ON_FALSE(cond) if(!(cond)) throw std::runtime_error(__FILE__ "(" BOOST_PP_STRINGIZE(__LINE__) ") Assertion failed: " #cond)
  24. // Enum to designate the state of the singletonized instances
  25. enum ConstructionState{CS_UNINIT, CS_INIT, CS_DESTROYED};
  26. // We need another singleton to check for the destruction of the singletons at program exit
  27. // We don't need all the magic for shared library anti-optimization and can keep it very simple
  28. struct controller{
  29. static controller& getInstance(){
  30. static controller instance;
  31. return instance;
  32. }
  33. ConstructionState state;
  34. private:
  35. controller() {
  36. state = CS_UNINIT;
  37. }
  38. ~controller();
  39. };
  40. // A simple class that sets its construction state in the controller singleton
  41. struct Foo{
  42. Foo(): i(42) {
  43. // access controller singleton. Therefore controller will be constructed before this
  44. THROW_ON_FALSE(controller::getInstance().state == CS_UNINIT);
  45. controller::getInstance().state = CS_INIT;
  46. }
  47. ~Foo() {
  48. // Because controller is constructed before this, it will be destructed AFTER this. Hence controller is still valid
  49. THROW_ON_FALSE(controller::getInstance().state == CS_INIT);
  50. controller::getInstance().state = CS_DESTROYED;
  51. }
  52. // Volatile to prevent compiler optimization from removing this
  53. volatile int i;
  54. };
  55. controller::~controller() {
  56. // If this fails, the singletons were not freed and memory is leaked
  57. THROW_ON_FALSE(state == CS_DESTROYED);
  58. // If this fails, then the destroyed flag is not set and one may use a deleted instance if relying on this flag
  59. THROW_ON_FALSE(boost::serialization::singleton<Foo>::is_destroyed());
  60. }
  61. int
  62. test_main( int /* argc */, char* /* argv */[] )
  63. {
  64. // Check if the singleton is alive and use it
  65. BOOST_CHECK(!boost::serialization::singleton<Foo>::is_destroyed());
  66. BOOST_CHECK(boost::serialization::singleton<Foo>::get_const_instance().i == 42);
  67. return EXIT_SUCCESS;
  68. }