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.

127 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. namespace juce
  18. {
  19. namespace universal_midi_packets
  20. {
  21. /**
  22. Enables iteration over a collection of Universal MIDI Packets stored as
  23. a contiguous range of 32-bit words.
  24. This iterator is used by Packets to allow access to the messages
  25. that it contains.
  26. @tags{Audio}
  27. */
  28. class Iterator
  29. {
  30. public:
  31. /** Creates an invalid (singular) iterator. */
  32. Iterator() noexcept = default;
  33. /** Creates an iterator pointing at `ptr`. */
  34. explicit Iterator (const uint32_t* ptr, size_t bytes) noexcept
  35. : view (ptr)
  36. #if JUCE_DEBUG
  37. , bytesRemaining (bytes)
  38. #endif
  39. {
  40. ignoreUnused (bytes);
  41. }
  42. using difference_type = std::iterator_traits<const uint32_t*>::difference_type;
  43. using value_type = View;
  44. using reference = const View&;
  45. using pointer = const View*;
  46. using iterator_category = std::forward_iterator_tag;
  47. /** Moves this iterator to the next packet in the range. */
  48. Iterator& operator++() noexcept
  49. {
  50. const auto increment = view.size();
  51. #if JUCE_DEBUG
  52. // If you hit this, the memory region contained a truncated or otherwise
  53. // malformed Universal MIDI Packet.
  54. // The Iterator can only be used on regions containing complete packets!
  55. jassert (increment <= bytesRemaining);
  56. bytesRemaining -= increment;
  57. #endif
  58. view = View (view.data() + increment);
  59. return *this;
  60. }
  61. /** Moves this iterator to the next packet in the range,
  62. returning the value of the iterator before it was
  63. incremented.
  64. */
  65. Iterator operator++ (int) noexcept
  66. {
  67. auto copy = *this;
  68. ++(*this);
  69. return copy;
  70. }
  71. /** Returns true if this iterator points to the same address
  72. as another iterator.
  73. */
  74. bool operator== (const Iterator& other) const noexcept
  75. {
  76. return view == other.view;
  77. }
  78. /** Returns false if this iterator points to the same address
  79. as another iterator.
  80. */
  81. bool operator!= (const Iterator& other) const noexcept
  82. {
  83. return ! operator== (other);
  84. }
  85. /** Returns a reference to a View of the packet currently
  86. pointed-to by this iterator.
  87. The View can be queried for its size and content.
  88. */
  89. reference operator*() noexcept { return view; }
  90. /** Returns a pointer to a View of the packet currently
  91. pointed-to by this iterator.
  92. The View can be queried for its size and content.
  93. */
  94. pointer operator->() noexcept { return &view; }
  95. private:
  96. View view;
  97. #if JUCE_DEBUG
  98. size_t bytesRemaining = 0;
  99. #endif
  100. };
  101. }
  102. }