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.

577 lines
20KB

  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. namespace juce
  18. {
  19. /**
  20. Allows serialisation functions to be attached to a specific type without having to modify the
  21. declaration of that type.
  22. A specialisation of SerialisationTraits must include:
  23. - A static constexpr data member named 'marshallingVersion' with a value that is convertible
  24. to std::optional<int>.
  25. - Either:
  26. - Normally, a single function with the following signature:
  27. @code
  28. template <typename Archive, typename Item>
  29. static void serialise (Archive& archive, Item& item);
  30. @endcode
  31. - For types that must do slightly different work when loading and saving, you may supply two
  32. functions with the following signatures, where "T" is a placeholder for the type on which
  33. SerialisationTraits is specialised:
  34. @code
  35. template <typename Archive>
  36. static void load (Archive& archive, T& item);
  37. template <typename Archive>
  38. static void save (Archive& archive, const T& item);
  39. @endcode
  40. If the marshallingVersion converts to a null optional, then all versioning information will be
  41. ignored when marshalling the type. Otherwise, if the value converts to a non-null optional, this
  42. versioning information will be included when serialising the type.
  43. Inside serialise() and load() you may call archive.getVersion() to find the detected version
  44. of the object being deserialised. archive.getVersion() will return an std::optional<int>,
  45. where 'nullopt' indicates that no versioning information was detected.
  46. Marshalling functions can also be specified directly inside the type to be marshalled. This
  47. approach may be preferable as it is more concise. Internal marshalling functions are written
  48. in exactly the same way as external ones; i.e. the type must include a marshallingVersion,
  49. and either a single serialise function, or a load/save pair of functions, as specified above.
  50. @tags{Core}
  51. */
  52. template <typename T> struct SerialisationTraits
  53. {
  54. /* Intentionally left blank. */
  55. };
  56. #define JUCE_COMPARISON_OPS X(==) X(!=) X(<) X(<=) X(>) X(>=)
  57. /**
  58. Combines an object with a name.
  59. Instances of Named have reference-like semantics. That is, Named stores a reference
  60. to a wrapped value, rather than storing the value internally.
  61. @tparam T the type of reference that is wrapped. Passing "const T" will cause the Named
  62. instance to hold a "const T&"; passing "T" will cause the Named instance to
  63. hold a "T&".
  64. @see named()
  65. @tags{Core}
  66. */
  67. template <typename T>
  68. struct Named
  69. {
  70. #define X(op) auto operator op (const Named& other) const { return value op other.value; }
  71. JUCE_COMPARISON_OPS
  72. #undef X
  73. std::string_view name; ///< A name that corresponds to the value
  74. T& value; ///< A reference to a value to wrap
  75. };
  76. /** Produces a Named instance that holds a mutable reference. */
  77. template <typename T> constexpr auto named (std::string_view c, T& t) { return Named<T> { c, t }; }
  78. /** Produces a Named instance that holds an immutable reference. */
  79. template <typename T> constexpr auto named (std::string_view c, const T& t) { return Named<const T> { c, t }; }
  80. /**
  81. Holds a reference to some kind of size value, used to indicate that an object being marshalled
  82. is of variable size (e.g. Array, vector, map, set, etc.).
  83. If you need to write your own serialisation routines for a dynamically-sized type, ensure
  84. that you archive an instance of SerialisationSize before any of the contents of the container.
  85. @tparam T the (probably numeric) type of the size value
  86. @see serialisztionSize()
  87. @tags{Core}
  88. */
  89. template <typename T>
  90. struct SerialisationSize
  91. {
  92. #define X(op) auto operator op (const SerialisationSize& other) const { return size op other.size; }
  93. JUCE_COMPARISON_OPS
  94. #undef X
  95. T& size;
  96. };
  97. /** Produces a SerialisationSize instance that holds a mutable reference to a size value. */
  98. template <typename T> constexpr auto serialisationSize (T& t) -> std::enable_if_t<std::is_integral_v<T>, SerialisationSize<T>> { return { t }; }
  99. /** Produces a SerialisationSize instance that holds an immutable reference to a size value. */
  100. template <typename T> constexpr auto serialisationSize (const T& t) -> std::enable_if_t<std::is_integral_v<T>, SerialisationSize<const T>> { return { t }; }
  101. #undef JUCE_COMPARISON_OPS
  102. //==============================================================================
  103. /*
  104. The following are specialisations of SerialisationTraits for commonly-used types.
  105. */
  106. #ifndef DOXYGEN
  107. template <typename... Ts>
  108. struct SerialisationTraits<std::vector<Ts...>>
  109. {
  110. static constexpr auto marshallingVersion = std::nullopt;
  111. template <typename Archive, typename T>
  112. static void load (Archive& archive, T& t)
  113. {
  114. auto size = t.size();
  115. archive (serialisationSize (size));
  116. t.resize (size);
  117. for (auto& element : t)
  118. archive (element);
  119. }
  120. template <typename Archive, typename T>
  121. static void save (Archive& archive, const T& t)
  122. {
  123. archive (serialisationSize (t.size()));
  124. for (auto& element : t)
  125. archive (element);
  126. }
  127. };
  128. template <typename Element, typename Mutex, int minSize>
  129. struct SerialisationTraits<Array<Element, Mutex, minSize>>
  130. {
  131. static constexpr auto marshallingVersion = std::nullopt;
  132. template <typename Archive, typename T>
  133. static void load (Archive& archive, T& t)
  134. {
  135. auto size = t.size();
  136. archive (serialisationSize (size));
  137. t.resize (size);
  138. for (auto& element : t)
  139. archive (element);
  140. }
  141. template <typename Archive, typename T>
  142. static void save (Archive& archive, const T& t)
  143. {
  144. archive (serialisationSize (t.size()));
  145. for (auto& element : t)
  146. archive (element);
  147. }
  148. };
  149. template <>
  150. struct SerialisationTraits<StringArray>
  151. {
  152. static constexpr auto marshallingVersion = std::nullopt;
  153. template <typename Archive, typename T>
  154. static void serialise (Archive& archive, T& t)
  155. {
  156. archive (t.strings);
  157. }
  158. };
  159. template <typename... Ts>
  160. struct SerialisationTraits<std::pair<Ts...>>
  161. {
  162. static constexpr auto marshallingVersion = std::nullopt;
  163. template <typename Archive, typename T>
  164. static void serialise (Archive& archive, T& t)
  165. {
  166. archive (named ("first", t.first), named ("second", t.second));
  167. }
  168. };
  169. template <typename T>
  170. struct SerialisationTraits<std::optional<T>>
  171. {
  172. static constexpr auto marshallingVersion = std::nullopt;
  173. template <typename Archive>
  174. static void load (Archive& archive, std::optional<T>& t)
  175. {
  176. bool engaged = false;
  177. archive (named ("engaged", engaged));
  178. if (! engaged)
  179. return;
  180. t.emplace();
  181. archive (named ("value", *t));
  182. }
  183. template <typename Archive>
  184. static void save (Archive& archive, const std::optional<T>& t)
  185. {
  186. archive (named ("engaged", t.has_value()));
  187. if (t.has_value())
  188. archive (named ("value", *t));
  189. }
  190. };
  191. template <>
  192. struct SerialisationTraits<std::string>
  193. {
  194. static constexpr auto marshallingVersion = std::nullopt;
  195. template <typename Archive>
  196. static void load (Archive& archive, std::string& t)
  197. {
  198. String temporary;
  199. archive (temporary);
  200. t = temporary.toStdString();
  201. }
  202. template <typename Archive>
  203. static void save (Archive& archive, const std::string& t)
  204. {
  205. archive (String (t));
  206. }
  207. };
  208. template <typename... Ts>
  209. struct SerialisationTraits<std::map<Ts...>>
  210. {
  211. static constexpr auto marshallingVersion = std::nullopt;
  212. template <typename Archive, typename T>
  213. static void load (Archive& archive, T& t)
  214. {
  215. auto size = t.size();
  216. archive (serialisationSize (size));
  217. for (auto i = (decltype (size)) 0; i < size; ++i)
  218. {
  219. std::pair<typename T::key_type, typename T::mapped_type> element;
  220. archive (element);
  221. t.insert (element);
  222. }
  223. }
  224. template <typename Archive, typename T>
  225. static void save (Archive& archive, const T& t)
  226. {
  227. auto size = t.size();
  228. archive (serialisationSize (size));
  229. for (const auto& element : t)
  230. archive (element);
  231. }
  232. };
  233. template <typename... Ts>
  234. struct SerialisationTraits<std::set<Ts...>>
  235. {
  236. static constexpr auto marshallingVersion = std::nullopt;
  237. template <typename Archive, typename T>
  238. static void load (Archive& archive, T& t)
  239. {
  240. auto size = t.size();
  241. archive (serialisationSize (size));
  242. for (auto i = (decltype (size)) 0; i < size; ++i)
  243. {
  244. typename T::value_type element;
  245. archive (element);
  246. t.insert (element);
  247. }
  248. }
  249. template <typename Archive, typename T>
  250. static void save (Archive& archive, const T& t)
  251. {
  252. auto size = t.size();
  253. archive (serialisationSize (size));
  254. for (const auto& element : t)
  255. archive (element);
  256. }
  257. };
  258. template <size_t N>
  259. struct SerialisationTraits<char[N]>
  260. {
  261. static constexpr auto marshallingVersion = std::nullopt;
  262. template <typename Archive, typename T>
  263. static void serialise (Archive& archive, T& t) { archive (String (t, N)); }
  264. };
  265. template <typename Element, size_t N>
  266. struct SerialisationTraits<Element[N]>
  267. {
  268. static constexpr auto marshallingVersion = std::nullopt;
  269. template <typename Archive, typename T>
  270. static void load (Archive& archive, T& t)
  271. {
  272. auto size = N;
  273. archive (serialisationSize (size));
  274. for (auto& element : t)
  275. archive (element);
  276. }
  277. template <typename Archive, typename T>
  278. static void save (Archive& archive, const T& t)
  279. {
  280. const auto size = N;
  281. archive (serialisationSize (size));
  282. for (auto& element : t)
  283. archive (element);
  284. }
  285. };
  286. template <typename Element, size_t N>
  287. struct SerialisationTraits<std::array<Element, N>>
  288. {
  289. static constexpr auto marshallingVersion = std::nullopt;
  290. template <typename Archive, typename T>
  291. static void load (Archive& archive, T& t)
  292. {
  293. auto size = N;
  294. archive (serialisationSize (size));
  295. for (auto& element : t)
  296. archive (element);
  297. }
  298. template <typename Archive, typename T>
  299. static void save (Archive& archive, const T& t)
  300. {
  301. const auto size = N;
  302. archive (serialisationSize (size));
  303. for (auto& element : t)
  304. archive (element);
  305. }
  306. };
  307. /*
  308. This namespace holds utilities for detecting and using serialisation functions.
  309. The contents of this namespace are private, and liable to change, so you shouldn't use any of
  310. the contents directly.
  311. */
  312. namespace detail
  313. {
  314. struct DummyArchive
  315. {
  316. template <typename... Ts>
  317. bool operator() (Ts&&...);
  318. std::optional<int> getVersion() const { return {}; }
  319. };
  320. template <typename T, typename = void>
  321. constexpr auto hasInternalVersion = false;
  322. template <typename T>
  323. constexpr auto hasInternalVersion<T, std::void_t<decltype (T::marshallingVersion)>> = true;
  324. template <typename Traits, typename T, typename = void>
  325. constexpr auto hasInternalSerialise = false;
  326. template <typename Traits, typename T>
  327. constexpr auto hasInternalSerialise<Traits, T, std::void_t<decltype (Traits::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
  328. template <typename Traits, typename T, typename = void>
  329. constexpr auto hasInternalLoad = false;
  330. template <typename Traits, typename T>
  331. constexpr auto hasInternalLoad<Traits, T, std::void_t<decltype (Traits::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
  332. template <typename Traits, typename T, typename = void>
  333. constexpr auto hasInternalSave = false;
  334. template <typename Traits, typename T>
  335. constexpr auto hasInternalSave<Traits, T, std::void_t<decltype (Traits::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> = true;
  336. template <typename T>
  337. struct SerialisedTypeTrait { using type = T; };
  338. template <typename T>
  339. struct SerialisedTypeTrait<SerialisationTraits<T>> { using type = T; };
  340. template <typename T>
  341. using SerialisedType = typename SerialisedTypeTrait<T>::type;
  342. template <typename T>
  343. constexpr auto hasSerialisation = hasInternalVersion<SerialisedType<T>>
  344. || hasInternalSerialise<T, SerialisedType<T>>
  345. || hasInternalLoad<T, SerialisedType<T>>
  346. || hasInternalSave<T, SerialisedType<T>>;
  347. /* Different kinds of serialisation function. */
  348. enum class SerialisationKind
  349. {
  350. none, // The type doesn't have any serialisation
  351. primitive, // The type has serialisation handling defined directly on the archiver. enums will be converted to equivalent integral values
  352. internal, // The type has internally-defined serialisation utilities
  353. external, // The type has an external specialisation of SerialisationTraits
  354. };
  355. /* The SerialisationKind to use for the type T.
  356. Primitive serialisation is used for arithmetic types, enums, Strings, and vars.
  357. Internal serialisation is used for types that declare an internal marshallingVersion,
  358. serialise(), load(), or save().
  359. External serialisation is used in all other cases.
  360. */
  361. template <typename T>
  362. constexpr auto serialisationKind = []
  363. {
  364. if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T> || std::is_same_v<T, String> || std::is_same_v<T, var>)
  365. return SerialisationKind::primitive;
  366. else if constexpr (hasSerialisation<T>)
  367. return SerialisationKind::internal;
  368. else if constexpr (hasSerialisation<SerialisationTraits<T>>)
  369. return SerialisationKind::external;
  370. else
  371. return SerialisationKind::none;
  372. }();
  373. /* This trait defines the serialisation utilities that are used for primitive types. */
  374. template <typename T, SerialisationKind kind = serialisationKind<T>>
  375. struct ForwardingSerialisationTraits
  376. {
  377. static constexpr auto marshallingVersion = std::nullopt;
  378. template <typename Archive, typename Primitive>
  379. static auto load (Archive& archive, Primitive& t)
  380. {
  381. if constexpr (std::is_enum_v<Primitive>)
  382. return archive (*reinterpret_cast<std::underlying_type_t<Primitive>*> (&t));
  383. else
  384. return archive (t);
  385. }
  386. template <typename Archive, typename Primitive>
  387. static auto save (Archive& archive, const Primitive& t)
  388. {
  389. if constexpr (std::is_enum_v<Primitive>)
  390. return archive (*reinterpret_cast<const std::underlying_type_t<Primitive>*> (&t));
  391. else
  392. return archive (t);
  393. }
  394. };
  395. /* This specialisation will be used for types with internal serialisation.
  396. All members of ForwardingSerialisationTraits forward to the corresponding member of T.
  397. */
  398. template <typename T>
  399. struct ForwardingSerialisationTraits<T, SerialisationKind::internal>
  400. {
  401. static constexpr std::optional<int> marshallingVersion { T::marshallingVersion };
  402. template <typename Archive, typename Item>
  403. static auto serialise (Archive& archive, Item& t) -> decltype (Item::serialise (archive, t)) { return Item::serialise (archive, t); }
  404. template <typename Archive, typename Item>
  405. static auto load (Archive& archive, Item& t) -> decltype (Item::load (archive, t)) { return Item::load (archive, t); }
  406. template <typename Archive, typename Item>
  407. static auto save (Archive& archive, const Item& t) -> decltype (Item::save (archive, t)) { return Item::save (archive, t); }
  408. };
  409. /* This specialisation will be used for types with external serialisation.
  410. @see SerialisationTraits
  411. */
  412. template <typename T>
  413. struct ForwardingSerialisationTraits<T, SerialisationKind::external> : SerialisationTraits<T> {};
  414. template <typename T, typename = void>
  415. constexpr auto hasSerialise = false;
  416. template <typename T>
  417. constexpr auto hasSerialise<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
  418. template <typename T, typename = void>
  419. constexpr auto hasLoad = false;
  420. template <typename T>
  421. constexpr auto hasLoad<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
  422. template <typename T, typename = void>
  423. constexpr auto hasSave = false;
  424. template <typename T>
  425. constexpr auto hasSave<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> = true;
  426. template <typename T>
  427. constexpr auto delayStaticAssert = false;
  428. /* Calls the correct function (serialise or save) to save the argument t to the archive.
  429. */
  430. template <typename Archive, typename T>
  431. auto doSave (Archive& archive, const T& t)
  432. {
  433. if constexpr (serialisationKind<T> == SerialisationKind::none)
  434. static_assert (delayStaticAssert<T>, "No serialisation function found or marshallingVersion unset");
  435. else if constexpr (hasSerialise<T> && ! hasSave<T>)
  436. return ForwardingSerialisationTraits<T>::serialise (archive, t);
  437. else if constexpr (! hasSerialise<T> && hasSave<T>)
  438. return ForwardingSerialisationTraits<T>::save (archive, t);
  439. else
  440. static_assert (delayStaticAssert<T>, "Multiple serialisation functions found");
  441. }
  442. /* Calls the correct function (serialise or load) to load the argument t from the archive.
  443. */
  444. template <typename Archive, typename T>
  445. auto doLoad (Archive& archive, T& t)
  446. {
  447. if constexpr (serialisationKind<T> == SerialisationKind::none)
  448. static_assert (delayStaticAssert<T>, "No serialisation function found or marshallingVersion unset");
  449. else if constexpr (hasSerialise<T> && ! hasLoad<T>)
  450. return ForwardingSerialisationTraits<T>::serialise (archive, t);
  451. else if constexpr (! hasSerialise<T> && hasLoad<T>)
  452. return ForwardingSerialisationTraits<T>::load (archive, t);
  453. else
  454. static_assert (delayStaticAssert<T>, "Multiple serialisation functions found");
  455. }
  456. } // namespace detail
  457. #endif
  458. } // namespace juce