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.

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