destroy_detector.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* destroy_detector.h - report object destruction for boost signal system
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  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 General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef CORE_DESTROY_DETECTOR_H_DD71BC27_C341_5CF3_8E0B_71C19AE585A2
  18. #define CORE_DESTROY_DETECTOR_H_DD71BC27_C341_5CF3_8E0B_71C19AE585A2
  19. #include <core/std/memory.h>
  20. #include <core/nothing.h>
  21. namespace rainynite {
  22. /**
  23. * A base class that allows tracking connection to boost signals.
  24. *
  25. * Note that using enable_shared_from_this seems like a attractive alternative
  26. * to empty destroy_detector, but it is more limited: it won't work when
  27. * instance is placed on stack or owned by unique_ptr.
  28. */
  29. class DestroyDetector {
  30. public:
  31. DestroyDetector() :
  32. destroy_detector(make_shared<Nothing>())
  33. {}
  34. DestroyDetector(DestroyDetector const& /*other*/) :
  35. destroy_detector(make_shared<Nothing>())
  36. {}
  37. /**
  38. * Connect signal and make connection track this object.
  39. */
  40. template <class S, class F>
  41. auto connect_boost(S& signal, F lambda) {
  42. auto slot = typename S::slot_type(lambda);
  43. slot.track_foreign(destroy_detector);
  44. return signal.connect(slot);
  45. }
  46. private:
  47. shared_ptr<Nothing> const destroy_detector;
  48. };
  49. } // namespace rainynite
  50. #endif