ARAContentReader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //------------------------------------------------------------------------------
  2. //! \file ARAContentReader.h
  3. //! content reading utility classes
  4. //! \project ARA SDK Library
  5. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  6. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  7. //! you may not use this file except in compliance with the License.
  8. //! You may obtain a copy of the License at
  9. //!
  10. //! http://www.apache.org/licenses/LICENSE-2.0
  11. //!
  12. //! Unless required by applicable law or agreed to in writing, software
  13. //! distributed under the License is distributed on an "AS IS" BASIS,
  14. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. //! See the License for the specific language governing permissions and
  16. //! limitations under the License.
  17. //------------------------------------------------------------------------------
  18. #ifndef ARAContentReader_h
  19. #define ARAContentReader_h
  20. #include "ARA_Library/Dispatch/ARADispatchBase.h"
  21. #include <type_traits>
  22. #include <iterator>
  23. #include <algorithm>
  24. #include <cmath>
  25. namespace ARA {
  26. /*******************************************************************************/
  27. // ContentTypeMapper
  28. // Map between ARAContentType and its associated event data structs.
  29. /*******************************************************************************/
  30. template <ARAContentType contentType>
  31. struct ContentTypeMapper;
  32. #define ARA_SPECIALIZE_CONTENT_TYPE_MAPPER(contentType, StructType) \
  33. template <> \
  34. struct ContentTypeMapper<contentType> \
  35. { \
  36. using DataType = StructType; \
  37. static constexpr auto enumName { #contentType }; \
  38. static constexpr auto typeName { #StructType }; \
  39. };
  40. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeNotes, ARAContentNote)
  41. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeTempoEntries, ARAContentTempoEntry)
  42. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeBarSignatures, ARAContentBarSignature)
  43. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeStaticTuning, ARAContentTuning)
  44. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeKeySignatures, ARAContentKeySignature)
  45. ARA_SPECIALIZE_CONTENT_TYPE_MAPPER (kARAContentTypeSheetChords, ARAContentChord)
  46. #undef ARA_SPECIALIZE_CONTENT_TYPE_MAPPER
  47. /*******************************************************************************/
  48. // ContentReaderFunctionMapper
  49. // Map to allow templated content reader code to pick the proper non-polymorphic ARA interface calls.
  50. /*******************************************************************************/
  51. template <typename ControllerType, typename ModelObjectRefType> struct ContentReaderFunctionMapper;
  52. #define ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER(ObjectName, RefName) \
  53. template <typename ControllerType> \
  54. struct ContentReaderFunctionMapper<ControllerType, RefName> \
  55. { \
  56. static constexpr auto isContentAvailable { &ControllerType::is##ObjectName##ContentAvailable }; \
  57. static constexpr auto getContentGrade { &ControllerType::get##ObjectName##ContentGrade }; \
  58. static constexpr auto createContentReader { &ControllerType::create##ObjectName##ContentReader }; \
  59. static constexpr auto modelObjectRefTypeName { #RefName }; \
  60. };
  61. ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER (AudioSource, ARAAudioSourceHostRef)
  62. ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER (MusicalContext, ARAMusicalContextHostRef)
  63. ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER (AudioSource, ARAAudioSourceRef)
  64. ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER (AudioModification, ARAAudioModificationRef)
  65. ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER (PlaybackRegion, ARAPlaybackRegionRef)
  66. #undef ARA_SPECIALIZE_CONTENT_READER_FUNCTION_MAPPER
  67. //! @addtogroup ARA_Library_Utility_Content_Readers
  68. //! @{
  69. /*******************************************************************************/
  70. // ContentReaderEventIterator
  71. /** Fully C++11 stl-compatible iterator adapter for ContentReader.
  72. \code{.cpp}
  73. auto tempoContentReader = createTempoReaderForAudioSource (...);
  74. for (const auto& tempoEntry : tempoContentReader)
  75. processTempoEntry (tempoEntry);
  76. \endcode
  77. See also https://en.cppreference.com/w/cpp/iterator
  78. */
  79. /*******************************************************************************/
  80. template <typename ContentReader>
  81. class ContentReaderEventIterator : public std::iterator<std::random_access_iterator_tag, typename ContentReader::DataType, ARAInt32, typename ContentReader::DataType*, typename ContentReader::DataType>
  82. {
  83. public:
  84. using DataType = typename ContentReader::DataType;
  85. ContentReaderEventIterator () = delete;
  86. inline ContentReaderEventIterator (const ContentReader* contentReader, ARAInt32 initialEventIndex) noexcept
  87. : _contentReader { contentReader },
  88. _eventIndex { initialEventIndex },
  89. _cachedEventIndex { -1 }
  90. {}
  91. inline ContentReaderEventIterator& operator++ () noexcept { ++this->_eventIndex; return *this; }
  92. inline ContentReaderEventIterator operator++ (int) noexcept { ContentReaderEventIterator result = *this; ++(*this); return result; }
  93. inline ContentReaderEventIterator& operator-- () noexcept { --this->_eventIndex; return *this; }
  94. inline ContentReaderEventIterator operator-- (int) noexcept { ContentReaderEventIterator result = *this; --(*this); return result; }
  95. inline ContentReaderEventIterator& operator+= (ARAInt32 offset) noexcept { this->_eventIndex += offset; return *this; }
  96. inline ContentReaderEventIterator& operator-= (ARAInt32 offset) noexcept { this->_eventIndex -= offset; return *this; }
  97. inline ContentReaderEventIterator operator+ (ARAInt32 offset) const noexcept { return ContentReaderEventIterator (*this) + offset; }
  98. inline friend ContentReaderEventIterator operator+ (ARAInt32 offset, const ContentReaderEventIterator& it) noexcept { return ContentReaderEventIterator (it) + offset; }
  99. inline ContentReaderEventIterator operator- (ARAInt32 offset) const noexcept { return ContentReaderEventIterator (*this) - offset; }
  100. inline friend ContentReaderEventIterator operator- (ARAInt32 offset, const ContentReaderEventIterator& it) noexcept { return ContentReaderEventIterator (it) - offset; }
  101. inline ARAInt32 operator- (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex - other._eventIndex; }
  102. inline bool operator== (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex == other._eventIndex; }
  103. inline bool operator!= (const ContentReaderEventIterator& other) const noexcept { return !(*this == other); }
  104. inline bool operator< (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex < other._eventIndex; }
  105. inline bool operator> (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex > other._eventIndex; }
  106. inline bool operator<= (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex <= other._eventIndex; }
  107. inline bool operator>= (const ContentReaderEventIterator& other) const noexcept { return this->_eventIndex >= other._eventIndex; }
  108. inline const DataType& operator* () const noexcept { return *this->getCachedData (this->_eventIndex); }
  109. inline const DataType* operator-> () const noexcept { return this->getCachedData (this->_eventIndex); }
  110. inline const DataType& operator[] (ARAInt32 offset) const noexcept { return *this->getCachedData (this->_eventIndex + offset); }
  111. private:
  112. inline const DataType* getCachedData (ARAInt32 index) const noexcept
  113. {
  114. if (this->_cachedEventIndex != index)
  115. {
  116. _cachedData = this->_contentReader->getDataForEvent (index);
  117. this->_cachedEventIndex = index;
  118. }
  119. return &this->_cachedData;
  120. }
  121. private:
  122. const ContentReader* _contentReader;
  123. ARAInt32 _eventIndex;
  124. mutable ARAInt32 _cachedEventIndex;
  125. mutable DataType _cachedData;
  126. };
  127. /*******************************************************************************/
  128. // Internal no-op validation class for ContentReaders
  129. template <ARAContentType contentType, typename ControllerType, typename ContentReaderRefType>
  130. struct NoContentValidator
  131. {
  132. inline void validateEventCount (ARAInt32 /*eventCount*/) {}
  133. inline void prepareValidateEvent (ControllerType* /*controller*/, ContentReaderRefType /*ref*/, ARAInt32 /*eventIndex*/) {}
  134. inline void validateEvent (const void* /*dataPtr*/, ARAInt32 /*eventIndex*/) {}
  135. };
  136. /*******************************************************************************/
  137. // ContentReader
  138. /** Wrapper class for convenient and type safe content reading.
  139. */
  140. /*******************************************************************************/
  141. template <ARAContentType contentType, typename ControllerType, typename ContentReaderRefType, typename ValidatorClass = NoContentValidator<contentType, ControllerType, ContentReaderRefType>>
  142. class ContentReader
  143. {
  144. protected:
  145. inline ContentReader () noexcept = default; // not to be called externally, used as helper for move c'tor only
  146. public:
  147. //! The type of ARA content data for this reader.
  148. using DataType = typename ContentTypeMapper<contentType>::DataType;
  149. template<typename ModelObjectRefType>
  150. inline ContentReader (ControllerType* controller, ModelObjectRefType modelObjectRef, const ARAContentTimeRange* range = nullptr) noexcept
  151. : _controller { controller },
  152. _isAvailable { (controller) ? (controller->*ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::isContentAvailable) (modelObjectRef, contentType) : false },
  153. _grade { this->_isAvailable ? (controller->*ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::getContentGrade) (modelObjectRef, contentType) : kARAContentGradeInitial },
  154. _ref { this->_isAvailable ? (controller->*ContentReaderFunctionMapper<ControllerType, ModelObjectRefType>::createContentReader) (modelObjectRef, contentType, range) : nullptr },
  155. _eventCount { this->_isAvailable ? controller->getContentReaderEventCount (this->_ref) : 0 }
  156. {
  157. if (this->_isAvailable)
  158. this->_validator.validateEventCount (this->_eventCount);
  159. }
  160. ContentReader (const ContentReader& other) = delete;
  161. ContentReader& operator= (const ContentReader& other) = delete;
  162. inline ContentReader (ContentReader&& other) noexcept
  163. : ContentReader {}
  164. { *this = std::move (other); }
  165. inline ContentReader& operator= (ContentReader&& other) noexcept
  166. {
  167. std::swap (this->_controller, other._controller);
  168. std::swap (this->_isAvailable, other._isAvailable);
  169. this->_grade = other._grade;
  170. std::swap (this->_ref, other._ref);
  171. this->_eventCount = other._eventCount;
  172. return *this;
  173. }
  174. inline ~ContentReader () noexcept
  175. {
  176. if (this->_isAvailable)
  177. this->_controller->destroyContentReader (this->_ref);
  178. }
  179. //! Tests whether content is available.
  180. //! Can be written as `if (someContentReader) ...`
  181. inline operator bool () const noexcept
  182. { return this->_isAvailable; }
  183. //! Get the grade of the content.
  184. inline ARAContentGrade getGrade () const noexcept
  185. { return this->_grade; }
  186. //! Get the number of content events.
  187. //! Will be 0 if content is not available, or for time-limited content types if range does not cover any events.
  188. inline ARAInt32 getEventCount () const noexcept
  189. { return this->_eventCount; }
  190. //! Returns a pointer to the data at index \p eventIndex.
  191. //! Care must be taken when accessing this pointer, its content will only be valid until
  192. //! any other content reading call is made or the reader is destroyed!
  193. inline const DataType* getDataPtrForEvent (ARAInt32 eventIndex) const noexcept
  194. {
  195. #if defined (ARA_INTERNAL_ASSERT)
  196. ARA_INTERNAL_ASSERT (eventIndex < this->_eventCount);
  197. #endif
  198. _validator.prepareValidateEvent (this->_controller, this->_ref, eventIndex);
  199. const DataType* dataPtr = static_cast<const DataType*> (this->_controller->getContentReaderDataForEvent (this->_ref, eventIndex));
  200. _validator.validateEvent (dataPtr, eventIndex);
  201. return dataPtr;
  202. }
  203. //! Returns the data at index \p eventIndex.
  204. inline DataType getDataForEvent (ARAInt32 eventIndex) const noexcept
  205. { return *this->getDataPtrForEvent (eventIndex); }
  206. //! \copybrief getDataForEvent
  207. inline DataType operator[] (ARAInt32 eventIndex) const noexcept
  208. { return this->getDataForEvent (eventIndex); }
  209. inline DataType operator[] (size_t eventIndex) const noexcept
  210. { return this->getDataForEvent (static_cast<ARAInt32> (eventIndex)); }
  211. //! @name STL Iterator Compatibility
  212. //! See ContentReaderEventIterator <>.
  213. //@{
  214. using const_iterator = ContentReaderEventIterator<ContentReader>;
  215. inline const_iterator begin () const
  216. { return const_iterator (this, 0); }
  217. inline const_iterator end () const
  218. { return const_iterator (this, this->_eventCount); }
  219. //@}
  220. private:
  221. ControllerType* _controller { nullptr };
  222. bool _isAvailable { false };
  223. ARAContentGrade _grade { kARAContentGradeInitial };
  224. ContentReaderRefType _ref { nullptr };
  225. ARAInt32 _eventCount { 0 };
  226. mutable ValidatorClass _validator;
  227. };
  228. //! @} ARA_Library_Utility_Content_Readers
  229. } // namespace ARA
  230. #endif // ARAContentReader_h