The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

121 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #ifndef DOXYGEN
  18. namespace juce::universal_midi_packets
  19. {
  20. /**
  21. Enables iteration over a collection of Universal MIDI Packets stored as
  22. a contiguous range of 32-bit words.
  23. This iterator is used by Packets to allow access to the messages
  24. that it contains.
  25. @tags{Audio}
  26. */
  27. class Iterator
  28. {
  29. public:
  30. /** Creates an invalid (singular) iterator. */
  31. Iterator() noexcept = default;
  32. /** Creates an iterator pointing at `ptr`. */
  33. explicit Iterator (const uint32_t* ptr, size_t bytes) noexcept;
  34. using difference_type = std::iterator_traits<const uint32_t*>::difference_type;
  35. using value_type = View;
  36. using reference = const View&;
  37. using pointer = const View*;
  38. using iterator_category = std::forward_iterator_tag;
  39. /** Moves this iterator to the next packet in the range. */
  40. Iterator& operator++() noexcept
  41. {
  42. const auto increment = view.size();
  43. #if JUCE_DEBUG
  44. // If you hit this, the memory region contained a truncated or otherwise
  45. // malformed Universal MIDI Packet.
  46. // The Iterator can only be used on regions containing complete packets!
  47. jassert (increment <= bytesRemaining);
  48. bytesRemaining -= increment;
  49. #endif
  50. view = View (view.data() + increment);
  51. return *this;
  52. }
  53. /** Moves this iterator to the next packet in the range,
  54. returning the value of the iterator before it was
  55. incremented.
  56. */
  57. Iterator operator++ (int) noexcept
  58. {
  59. auto copy = *this;
  60. ++(*this);
  61. return copy;
  62. }
  63. /** Returns true if this iterator points to the same address
  64. as another iterator.
  65. */
  66. bool operator== (const Iterator& other) const noexcept
  67. {
  68. return view == other.view;
  69. }
  70. /** Returns false if this iterator points to the same address
  71. as another iterator.
  72. */
  73. bool operator!= (const Iterator& other) const noexcept
  74. {
  75. return ! operator== (other);
  76. }
  77. /** Returns a reference to a View of the packet currently
  78. pointed-to by this iterator.
  79. The View can be queried for its size and content.
  80. */
  81. reference operator*() noexcept { return view; }
  82. /** Returns a pointer to a View of the packet currently
  83. pointed-to by this iterator.
  84. The View can be queried for its size and content.
  85. */
  86. pointer operator->() noexcept { return &view; }
  87. private:
  88. View view;
  89. #if JUCE_DEBUG
  90. size_t bytesRemaining = 0;
  91. #endif
  92. };
  93. } // namespace juce::universal_midi_packets
  94. #endif