notify.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* notify.h - abstract class for things that notify about their changes
  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_NODE_NOTIFY_H_BFB9C4AC_D9A9_552F_B76B_454E86DB73E8
  18. #define CORE_NODE_NOTIFY_H_BFB9C4AC_D9A9_552F_B76B_454E86DB73E8
  19. #include <boost/signals2/signal.hpp>
  20. #include <core/std/memory.h>
  21. #include <core/destroy_detector.h>
  22. namespace rainynite::core {
  23. /**
  24. * Abstract entity that notifies of its changes.
  25. *
  26. * TODO: move out of nodes
  27. */
  28. class AbstractNotify : public DestroyDetector {
  29. public:
  30. AbstractNotify() :
  31. DestroyDetector(),
  32. changed_signal()
  33. {}
  34. AbstractNotify(AbstractNotify const& /*other*/) :
  35. DestroyDetector(),
  36. changed_signal()
  37. {}
  38. virtual ~AbstractNotify() = default;
  39. /**
  40. * Function to be called when object has changed.
  41. */
  42. inline void changed() {
  43. changed_signal();
  44. }
  45. /**
  46. * Subscribe to this object changes
  47. */
  48. template <typename F>
  49. boost::signals2::connection subscribe(F f) {
  50. return connect_boost(changed_signal, f);
  51. }
  52. private:
  53. /**
  54. * Signal to subscribe to this object's changes
  55. */
  56. boost::signals2::signal<void()> changed_signal;
  57. };
  58. } // namespace rainynite::core
  59. #endif