test_singleton_inherited.cpp 3.4 KB

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