message.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  3. #pragma once
  4. #include "definitions.hpp"
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <initializer_list>
  9. #include <limits>
  10. #include <tuple>
  11. #include <type_traits>
  12. namespace rotor_light {
  13. struct ActorBase;
  14. struct SupervisorBase;
  15. /** \struct Message
  16. * \brief base class for all rotor-light messages */
  17. struct Message {
  18. Message() : to{0} {}
  19. /** records destination */
  20. Message(ActorId to_) : to{to_} {}
  21. Message(const Message &) = delete;
  22. Message(Message &&) = delete;
  23. /** message type, to be filled by queue upon message construction */
  24. MessageTypeId type;
  25. private:
  26. friend struct ActorBase;
  27. friend struct SupervisorBase;
  28. ActorId to;
  29. };
  30. namespace traits {
  31. template <size_t... Items>
  32. auto constexpr max =
  33. [] { return std::max(std::initializer_list<size_t>{Items...}); };
  34. /** \struct MessageStorage
  35. * \brief properly allocated & sized space for any user-defined
  36. * message type
  37. */
  38. template <typename... Ts> struct MessageStorage {
  39. /** calculates maximum size of all messages */
  40. static constexpr size_t item_size = max<sizeof(Ts)...>();
  41. /** calculates maximum alignment of all messages */
  42. static constexpr size_t item_alignment = max<alignof(Ts)...>();
  43. /** aliases aligned_storage to store max size and max alignment */
  44. using Item = std::aligned_storage_t<item_size, item_alignment>;
  45. };
  46. } // namespace traits
  47. } // namespace rotor_light