ARADispatchBase.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //------------------------------------------------------------------------------
  2. //! \file ARADispatchBase.h
  3. //! C-to-C++ adapter helpers shared by ARA plug-in and host implementations
  4. //! Originally written and contributed to the ARA SDK by PreSonus Software Ltd.
  5. //! Typically, this file is not included directly - either ARAHostDispatch.h or
  6. //! ARAPlugInDispatch.h will be used instead.
  7. //! \project ARA SDK Library
  8. //! \copyright Copyright (c) 2012-2021, Celemony Software GmbH, All Rights Reserved.
  9. //! Developed in cooperation with PreSonus Software Ltd.
  10. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  11. //! you may not use this file except in compliance with the License.
  12. //! You may obtain a copy of the License at
  13. //!
  14. //! http://www.apache.org/licenses/LICENSE-2.0
  15. //!
  16. //! Unless required by applicable law or agreed to in writing, software
  17. //! distributed under the License is distributed on an "AS IS" BASIS,
  18. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. //! See the License for the specific language governing permissions and
  20. //! limitations under the License.
  21. //------------------------------------------------------------------------------
  22. #ifndef ARADispatchBase_h
  23. #define ARADispatchBase_h
  24. #include <cstddef>
  25. #include <type_traits>
  26. #include <algorithm>
  27. #include <memory>
  28. #include "ARA_API/ARAInterface.h"
  29. namespace ARA {
  30. /*******************************************************************************/
  31. // Helper templates for type safe conversions between external opaque (host)refs
  32. // and internal object pointers.
  33. /*******************************************************************************/
  34. template <typename Class, typename... RefTypes>
  35. class ToRefConversionHelper;
  36. template <typename Class>
  37. class ToRefConversionHelper<Class>
  38. {
  39. public:
  40. explicit constexpr inline ToRefConversionHelper (const Class* ptr) noexcept
  41. : _ptr { ptr }
  42. {}
  43. protected:
  44. const Class* const _ptr;
  45. };
  46. template <typename Class, typename FirstRefType, typename... RefTypes>
  47. class ToRefConversionHelper<Class, FirstRefType, RefTypes...> : public ToRefConversionHelper<Class, RefTypes...>
  48. {
  49. public:
  50. using ToRefConversionHelper<Class, RefTypes...>::ToRefConversionHelper;
  51. constexpr inline operator FirstRefType () const noexcept
  52. { return reinterpret_cast<FirstRefType> (const_cast<Class*> (this->_ptr)); }
  53. };
  54. template <typename Class, typename... RefTypes>
  55. class FromRefConversionHelper;
  56. template <typename Class>
  57. class FromRefConversionHelper<Class>
  58. {
  59. protected:
  60. explicit constexpr inline FromRefConversionHelper (Class* ptr) noexcept
  61. : _ptr { ptr }
  62. {}
  63. public:
  64. constexpr inline operator Class* () const noexcept
  65. { return this->_ptr; }
  66. constexpr inline Class* operator-> () const noexcept
  67. { return this->_ptr; }
  68. protected:
  69. Class* const _ptr;
  70. };
  71. template <typename Class, typename FirstRefType, typename... RefTypes>
  72. class FromRefConversionHelper<Class, FirstRefType, RefTypes...> : public FromRefConversionHelper<Class, RefTypes...>
  73. {
  74. protected:
  75. using FromRefConversionHelper<Class, RefTypes...>::FromRefConversionHelper;
  76. public:
  77. explicit constexpr inline FromRefConversionHelper (const FirstRefType ref) noexcept
  78. : FromRefConversionHelper<Class, RefTypes...> { reinterpret_cast<Class*> (const_cast<FirstRefType> (ref)) }
  79. {}
  80. };
  81. //! @addtogroup ARA_Library_Utility_SizedStructs
  82. //! @{
  83. /*******************************************************************************/
  84. // ARA_DISABLE_COPY_AND_MOVE macro
  85. // Used to prevent copying / moving an object managed by the API partner.
  86. /*******************************************************************************/
  87. #if !defined (ARA_DISABLE_COPY_AND_MOVE)
  88. #define ARA_DISABLE_COPY_AND_MOVE(ClassName) \
  89. ClassName (const ClassName& other) = delete; \
  90. ClassName& operator= (const ClassName& other) = delete; \
  91. ClassName (ClassName&& other) = delete; \
  92. ClassName& operator= (ClassName&& other) = delete;
  93. #endif
  94. /*******************************************************************************/
  95. // ARA_STRUCT_MEMBER pre-C++17 utility macro
  96. /** When dealing with ARA variable-sized structures, this eases defining the related C++ template arguments */
  97. /*******************************************************************************/
  98. #if defined (__cpp_template_auto)
  99. // defined here so that code can be written to support both pre-C++17 and C++17 by always using the macro
  100. // note that code which can rely on C++17 and up does not need to use the macro,
  101. // writing e.g. SizedStruct<&ARAFactory::supportedPlaybackTransformationFlags> instead.
  102. #define ARA_STRUCT_MEMBER(StructType, member) &ARA::StructType::member
  103. #else
  104. #define ARA_STRUCT_MEMBER(StructType, member) ARA::StructType, decltype (ARA::StructType::member), &ARA::StructType::member
  105. #endif
  106. /*******************************************************************************/
  107. // SizedStruct
  108. //! Templated C++ wrapper for ARA's variable-sized structs, ensuring their proper initialization.
  109. //!
  110. //! ARA structs are versioned with a `structSize` member indicating the last implemented member
  111. //! of the struct. Using SizedStruct will automatically set `structSize` according to the
  112. //! `member` template parameter. For example, to declare an ARADocumentControllerInterface
  113. //! struct that implements every function up until ARADocumentControllerInterface::storeObjectsToArchive(),
  114. //! you can declare a SizedStruct like so:
  115. //! \code{.cpp}
  116. //! SizedStruct<ARA_STRUCT_MEMBER (ARADocumentControllerInterface, storeObjectsToArchive)> dci;
  117. //! \endcode
  118. //! In this example, `dci` won't support any function declared after ARADocumentControllerInterface::storeObjectsToArchive(),
  119. //! meaning it won't support ARA Analysis Algorithm selection.
  120. /*******************************************************************************/
  121. #if defined (_MSC_VER)
  122. __pragma (warning(push))
  123. __pragma (warning(disable : 4324)) // disable padding warnings, instances of this struct template may need to be padded.
  124. #endif
  125. #if defined (__cpp_template_auto)
  126. template <auto member>
  127. struct alignas (alignof (void*)) SizedStruct;
  128. template <typename StructType, typename MemberType, MemberType StructType::*member>
  129. struct alignas (alignof (void*)) SizedStruct<member> : public StructType
  130. #else
  131. template <typename StructType, typename MemberType, MemberType StructType::*member>
  132. struct alignas (alignof (void*)) SizedStruct : public StructType
  133. #endif
  134. {
  135. protected:
  136. //! \p SizedStruct alias, to allow any derived class to easily reference its templated base class.
  137. using BaseType = SizedStruct;
  138. public:
  139. //! Returns the versioned size of the \p StructType.
  140. //! ARA uses \p StructType::structSize as a means of versioning, so this function returns
  141. //! the size of the struct as though \p member was the last implemented field.
  142. static constexpr inline size_t getImplementedSize ()
  143. {
  144. static_assert (std::is_class<StructType>::value && std::is_standard_layout<StructType>::value, "C compatible standard layout struct required");
  145. return reinterpret_cast<intptr_t> (&(static_cast<StructType*> (nullptr)->*member)) + sizeof (static_cast<StructType*> (nullptr)->*member);
  146. }
  147. //! Initializer list constructor.
  148. //! Can be used initialize the \p StructType fields using a braced initializer list.
  149. //! Uninitialized members will be set to 0.
  150. template <typename... InitializerTypes>
  151. constexpr inline SizedStruct (InitializerTypes... initializers) noexcept
  152. : StructType { SizedStruct::getImplementedSize (), std::forward<InitializerTypes> (initializers)... }
  153. {}
  154. //! Default constructor.
  155. //! Special-cases the initializer list constructor only to suppress warnings about missing initializers for default construction.
  156. #if __cplusplus >= 201402L
  157. constexpr
  158. #endif
  159. inline SizedStruct () noexcept
  160. : StructType {}
  161. {
  162. this->structSize = SizedStruct::getImplementedSize ();
  163. }
  164. };
  165. #if defined (_MSC_VER)
  166. __pragma (warning(pop))
  167. #endif
  168. /*******************************************************************************/
  169. // SizedStructPtr
  170. //! Templated smart pointer to conveniently evaluate pointers to ARA variable-sized structures.
  171. //!
  172. //! Wrapping an ARA struct pointer using SizedStructPtr allows easy detection of the struct "version",
  173. //! i.e what members it does and does not support. For example, if we wanted to know whether an
  174. //! ARADocumentControllerInterface instance supports ARA Analysis Algorithm Selection, we could
  175. //! use SizedStructPtr like so:
  176. //! \code{.cpp}
  177. //! bool supportsAnalysisAlgorithmSelection (ARADocumentControllerInterface* dci)
  178. //! {
  179. //! SizedStructPtr<ARADocumentControllerInterface> sizedStructPtr (dci);
  180. //! return sizedStructPtr.implements<ARA_STRUCT_MEMBER (ARADocumentControllerInterface, getProcessingAlgorithmsCount)> ();
  181. //! }
  182. //! \endcode
  183. /*******************************************************************************/
  184. template <typename StructType>
  185. class SizedStructPtr
  186. {
  187. public:
  188. //! Construct from a pointer to \p StructType.
  189. inline SizedStructPtr (const StructType* ptr) noexcept
  190. : _ptr { ptr }
  191. {}
  192. //! Dereference the underlying \p StructType pointer.
  193. inline operator const StructType* () const noexcept
  194. { return this->_ptr; }
  195. //! Access the underlying \p StructType pointer.
  196. inline const StructType* operator-> () const noexcept
  197. { return this->_ptr; }
  198. //! Check if \p StructType is sized large enough to implement \p member.
  199. //! ARA uses the \p StructType::structSize member as a means of versioning -
  200. //! if it is large enough to contain \p member then this function returns true.
  201. //! This is a C++ version of ARA_IMPLEMENTS_FIELD.
  202. #if defined (__cpp_template_auto)
  203. template <auto member>
  204. inline bool implements () const noexcept
  205. {
  206. #if defined (__clang__) && (__clang_major__ < 10)
  207. // see clang bug: https://bugs.llvm.org/show_bug.cgi?id=35655
  208. // as workaround, we're copying the code from SizedStruct<member>::getImplementedSize () here
  209. static_assert (std::is_class<StructType>::value && std::is_standard_layout<StructType>::value, "C compatible standard layout struct required");
  210. return this->_ptr->structSize >= reinterpret_cast<intptr_t> (&(static_cast<StructType*> (nullptr)->*member)) + sizeof (static_cast<StructType*> (nullptr)->*member);
  211. #else
  212. return this->_ptr->structSize >= SizedStruct<member>::getImplementedSize ();
  213. #endif
  214. }
  215. #else
  216. template <typename DummyStructType, typename MemberType, MemberType StructType::*member>
  217. inline bool implements () const noexcept
  218. {
  219. static_assert (std::is_same<DummyStructType, StructType>::value, "must test member of same struct");
  220. return this->_ptr->structSize >= SizedStruct<StructType, MemberType, member>::getImplementedSize ();
  221. }
  222. #endif
  223. private:
  224. const StructType* _ptr;
  225. };
  226. /*******************************************************************************/
  227. // InterfaceInstance
  228. //! Templated base class to ease wrapping ARA interface structure callable into C++ classes.
  229. /*******************************************************************************/
  230. template <typename RefType, typename Interface>
  231. class InterfaceInstance
  232. {
  233. protected:
  234. //! \p InterfaceInstance alias, to allow derived classes to easily reference their templated base class.
  235. using BaseType = InterfaceInstance;
  236. //! Constructor for derived classes to wrap a \p ref and \p interface pointer.
  237. //! The derived classes typically read these values from an instance struct
  238. //! which is passed to their public c'tor.
  239. inline InterfaceInstance (RefType ref, const Interface* ifc) noexcept
  240. : _ref { ref },
  241. _interface { ifc }
  242. {}
  243. public:
  244. //! Retrieve the ARA reference associated with \p Interface.
  245. inline RefType getRef () const noexcept
  246. { return this->_ref; }
  247. //! Retrieve the underlying \p Interface pointer.
  248. inline const SizedStructPtr<Interface>& getInterface () const noexcept
  249. { return this->_interface; }
  250. //! Returns whether or not an implementation of the interface was provided.
  251. inline bool isProvided () const noexcept
  252. { return (this->_interface != nullptr); }
  253. private:
  254. RefType const _ref;
  255. const SizedStructPtr<Interface> _interface;
  256. };
  257. //! @} ARA_Library_Utility_SizedStructs
  258. //! @addtogroup ARA_Library_Utility_Content_Update_Scopes
  259. //! @{
  260. /*******************************************************************************/
  261. // ContentUpdateScopes
  262. //! Utility class to conveniently deal with ARAContentUpdateFlags.
  263. //! This class contains static constants that can be combined with operator+
  264. //! to describe any given change, for example notesAreAffected () + timelineIsAffected ().
  265. //! Likewise, its member functions allow for testing these conditions.
  266. /*******************************************************************************/
  267. class ContentUpdateScopes
  268. {
  269. public:
  270. //! @name Sending content updates.
  271. //! These constants are meant to be used when sending
  272. //! a content update notification. They can be combined using
  273. //! operator+ and operator+= to define a particular update scope.
  274. //@{
  275. //! Nothing is affected by the change.
  276. static constexpr ContentUpdateScopes nothingIsAffected () noexcept { return ContentUpdateScopes (); }
  277. //! The actual signal is affected by the change.
  278. static constexpr ContentUpdateScopes samplesAreAffected () noexcept { return nothingIsAffected ()._flags & ~kARAContentUpdateSignalScopeRemainsUnchanged; }
  279. //! Content information for notes, beat-markers etc. is affected by the change.
  280. static constexpr ContentUpdateScopes notesAreAffected () noexcept { return nothingIsAffected ()._flags & ~kARAContentUpdateNoteScopeRemainsUnchanged; }
  281. //! Content information for tempo, bar signatures etc. is affected by the change.
  282. static constexpr ContentUpdateScopes timelineIsAffected () noexcept { return nothingIsAffected ()._flags & ~kARAContentUpdateTimingScopeRemainsUnchanged; }
  283. //! Content readers for tuning etc. are affected by the change.
  284. static constexpr ContentUpdateScopes tuningIsAffected () noexcept { return nothingIsAffected ()._flags & ~kARAContentUpdateTuningScopeRemainsUnchanged; }
  285. //! Content readers for key signatures, chords etc. are affected by the change.
  286. static constexpr ContentUpdateScopes harmoniesAreAffected () noexcept { return nothingIsAffected ()._flags & ~kARAContentUpdateHarmonicScopeRemainsUnchanged; }
  287. //! Everything is affected by the change.
  288. static constexpr ContentUpdateScopes everythingIsAffected () noexcept { return kARAContentUpdateEverythingChanged; }
  289. //@}
  290. //! @name Creating update scopes.
  291. //@{
  292. //! Default constructor sets flags to nothingIsAffected.
  293. //! This sets flags to mean nothing has changed, and is rarely to be used directly.
  294. constexpr ContentUpdateScopes () noexcept : _flags { _knownFlags } {}
  295. //! Construct from ARAContentFlags.
  296. constexpr ContentUpdateScopes (const ARAContentUpdateFlags flags) noexcept : _flags { flags } {}
  297. //! Conversion to ARAContentUpdateFlags.
  298. constexpr operator ARAContentUpdateFlags () const noexcept { return _flags; }
  299. //! Combine two scopes into a single ContentUpdateScopes.
  300. ContentUpdateScopes operator+= (const ContentUpdateScopes& other) noexcept
  301. {
  302. _flags &= other._flags;
  303. return *this;
  304. }
  305. //! \copybrief operator+=
  306. constexpr ContentUpdateScopes operator+ (const ContentUpdateScopes& other) const noexcept { return (_flags & other._flags); }
  307. //@}
  308. //! @name Receiving content updates.
  309. //! These functions can be used by receivers of content updates
  310. //! to query the scope of what's affected by the update.
  311. //@{
  312. //! \copybrief samplesAreAffected
  313. constexpr bool affectSamples () const noexcept { return ((_flags & kARAContentUpdateSignalScopeRemainsUnchanged) == 0); }
  314. //! \copybrief notesAreAffected
  315. constexpr bool affectNotes () const noexcept { return ((_flags & kARAContentUpdateNoteScopeRemainsUnchanged) == 0); }
  316. //! \copybrief timelineIsAffected
  317. constexpr bool affectTimeline () const noexcept { return ((_flags & kARAContentUpdateTimingScopeRemainsUnchanged) == 0); }
  318. //! \copybrief tuningIsAffected
  319. constexpr bool affectTuning () const noexcept { return ((_flags & kARAContentUpdateTuningScopeRemainsUnchanged) == 0); }
  320. //! \copybrief harmoniesAreAffected
  321. constexpr bool affectHarmonies () const noexcept { return ((_flags & kARAContentUpdateHarmonicScopeRemainsUnchanged) == 0); }
  322. //! \copybrief everythingIsAffected
  323. constexpr bool affectEverything () const noexcept { return ((_flags & _knownFlags) == 0); }
  324. //@}
  325. private:
  326. static constexpr ARAContentUpdateFlags _knownFlags { (kARAContentUpdateSignalScopeRemainsUnchanged |
  327. kARAContentUpdateNoteScopeRemainsUnchanged | kARAContentUpdateTimingScopeRemainsUnchanged |
  328. kARAContentUpdateTuningScopeRemainsUnchanged | kARAContentUpdateHarmonicScopeRemainsUnchanged) };
  329. ARAContentUpdateFlags _flags;
  330. };
  331. //! @} ARA_Library_Utility_Content_Update_Scopes
  332. } // namespace ARA
  333. #endif // ARADispatchBase_h