💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.2 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_EVENT_HPP
  20. #define ANTKEEPER_EVENT_HPP
  21. #include <atomic>
  22. #include <cstdlib>
  23. /**
  24. * Abstract base class for events.
  25. */
  26. class event_base
  27. {
  28. public:
  29. /// Destroys an event base.
  30. virtual ~event_base() = default;
  31. /// Returns the unique event type identifier for this event type.
  32. virtual const std::size_t get_event_type_id() const = 0;
  33. /**
  34. * Allocates a copy of this event.
  35. *
  36. * @return Newly allocated copy of this event.
  37. */
  38. virtual event_base* clone() const = 0;
  39. protected:
  40. /// Returns then increments the next available event type ID.
  41. static std::size_t next_event_type_id();
  42. };
  43. inline std::size_t event_base::next_event_type_id()
  44. {
  45. static std::atomic<std::size_t> next_event_type_id{0};
  46. return next_event_type_id++;
  47. }
  48. /**
  49. * Templated abstract base class for events.
  50. *
  51. * @tparam T The derived class.
  52. */
  53. template <typename T>
  54. class event: public event_base
  55. {
  56. public:
  57. /// The unique event type identifier for this event type.
  58. static const std::atomic<std::size_t> event_type_id;
  59. /// Destroys an event
  60. virtual ~event() = default;
  61. virtual const std::size_t get_event_type_id() const final;
  62. virtual event_base* clone() const = 0;
  63. };
  64. template <typename T>
  65. const std::atomic<std::size_t> event<T>::event_type_id{event_base::next_event_type_id()};
  66. template <typename T>
  67. inline const std::size_t event<T>::get_event_type_id() const
  68. {
  69. return event_type_id;
  70. }
  71. #endif // ANTKEEPER_EVENT_HPP