70#define JUCE_COMPARISON_OPS X(==) X(!=) X(<) X(<=) X(>) X(>=)
89 #define X(op) auto operator op (const Named& other) const { return value op other.value; }
98template <
typename T>
constexpr auto named (std::string_view c, T& t) {
return Named<T> { c, t }; }
101template <
typename T>
constexpr auto named (std::string_view c,
const T& t) {
return Named<const T> { c, t }; }
119 #define X(op) auto operator op (const SerialisationSize& other) const { return size op other.size; }
127template <
typename T>
constexpr auto serialisationSize (T& t) -> std::enable_if_t<std::is_integral_v<T>,
SerialisationSize<T>> {
return { t }; }
130template <
typename T>
constexpr auto serialisationSize (
const T& t) -> std::enable_if_t<std::is_integral_v<T>,
SerialisationSize<const T>> {
return { t }; }
132#undef JUCE_COMPARISON_OPS
141template <
typename... Ts>
144 static constexpr auto marshallingVersion = std::nullopt;
146 template <
typename Archive,
typename T>
147 static void load (Archive& archive, T& t)
149 auto size = t.size();
150 archive (serialisationSize (size));
153 for (
auto& element : t)
157 template <
typename Archive,
typename T>
158 static void save (Archive& archive,
const T& t)
160 archive (serialisationSize (t.size()));
162 for (
auto& element : t)
167template <
typename Element,
typename Mutex,
int minSize>
170 static constexpr auto marshallingVersion = std::nullopt;
172 template <
typename Archive,
typename T>
173 static void load (Archive& archive, T& t)
175 auto size = t.
size();
176 archive (serialisationSize (size));
179 for (
auto& element : t)
183 template <
typename Archive,
typename T>
184 static void save (Archive& archive,
const T& t)
186 archive (serialisationSize (t.size()));
188 for (
auto& element : t)
196 static constexpr auto marshallingVersion = std::nullopt;
198 template <
typename Archive,
typename T>
199 static void serialise (Archive& archive, T& t)
205template <
typename... Ts>
208 static constexpr auto marshallingVersion = std::nullopt;
210 template <
typename Archive,
typename T>
211 static void serialise (Archive& archive, T& t)
213 archive (named (
"first", t.first), named (
"second", t.second));
220 static constexpr auto marshallingVersion = std::nullopt;
222 template <
typename Archive>
223 static void load (Archive& archive, std::optional<T>& t)
225 bool engaged =
false;
227 archive (named (
"engaged", engaged));
233 archive (named (
"value", *t));
236 template <
typename Archive>
237 static void save (Archive& archive,
const std::optional<T>& t)
239 archive (named (
"engaged", t.has_value()));
242 archive (named (
"value", *t));
249 static constexpr auto marshallingVersion = std::nullopt;
251 template <
typename Archive>
252 static void load (Archive& archive, std::string& t)
256 t = temporary.toStdString();
259 template <
typename Archive>
260 static void save (Archive& archive,
const std::string& t)
262 archive (String (t));
266template <
typename... Ts>
269 static constexpr auto marshallingVersion = std::nullopt;
271 template <
typename Archive,
typename T>
272 static void load (Archive& archive, T& t)
274 auto size = t.size();
275 archive (serialisationSize (size));
277 for (
auto i = (
decltype (size)) 0; i < size; ++i)
279 std::pair<typename T::key_type, typename T::mapped_type> element;
285 template <
typename Archive,
typename T>
286 static void save (Archive& archive,
const T& t)
288 auto size = t.size();
289 archive (serialisationSize (size));
291 for (
const auto& element : t)
296template <
typename... Ts>
299 static constexpr auto marshallingVersion = std::nullopt;
301 template <
typename Archive,
typename T>
302 static void load (Archive& archive, T& t)
304 auto size = t.size();
305 archive (serialisationSize (size));
307 for (
auto i = (
decltype (size)) 0; i < size; ++i)
309 typename T::value_type element;
315 template <
typename Archive,
typename T>
316 static void save (Archive& archive,
const T& t)
318 auto size = t.size();
319 archive (serialisationSize (size));
321 for (
const auto& element : t)
329 static constexpr auto marshallingVersion = std::nullopt;
331 template <
typename Archive,
typename T>
332 static void serialise (Archive& archive, T& t) { archive (String (t, N)); }
335template <
typename Element,
size_t N>
338 static constexpr auto marshallingVersion = std::nullopt;
340 template <
typename Archive,
typename T>
341 static void load (Archive& archive, T& t)
344 archive (serialisationSize (size));
346 for (
auto& element : t)
350 template <
typename Archive,
typename T>
351 static void save (Archive& archive,
const T& t)
354 archive (serialisationSize (size));
356 for (
auto& element : t)
361template <
typename Element,
size_t N>
364 static constexpr auto marshallingVersion = std::nullopt;
366 template <
typename Archive,
typename T>
367 static void load (Archive& archive, T& t)
370 archive (serialisationSize (size));
372 for (
auto& element : t)
376 template <
typename Archive,
typename T>
377 static void save (Archive& archive,
const T& t)
380 archive (serialisationSize (size));
382 for (
auto& element : t)
397 template <
typename... Ts>
398 bool operator() (Ts&&...);
400 std::optional<int> getVersion()
const {
return {}; }
403 template <
typename T,
typename =
void>
404 constexpr auto hasInternalVersion =
false;
406 template <
typename T>
407 constexpr auto hasInternalVersion<T, std::void_t<
decltype (T::marshallingVersion)>> =
true;
409 template <
typename Traits,
typename T,
typename =
void>
410 constexpr auto hasInternalSerialise =
false;
412 template <
typename Traits,
typename T>
413 constexpr auto hasInternalSerialise<Traits, T, std::void_t<decltype (Traits::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> =
true;
415 template <
typename Traits,
typename T,
typename =
void>
416 constexpr auto hasInternalLoad =
false;
418 template <
typename Traits,
typename T>
419 constexpr auto hasInternalLoad<Traits, T, std::void_t<decltype (Traits::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> =
true;
421 template <
typename Traits,
typename T,
typename =
void>
422 constexpr auto hasInternalSave =
false;
424 template <
typename Traits,
typename T>
425 constexpr auto hasInternalSave<Traits, T, std::void_t<decltype (Traits::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> =
true;
427 template <
typename T>
428 struct SerialisedTypeTrait {
using type = T; };
430 template <
typename T>
431 struct SerialisedTypeTrait<SerialisationTraits<T>> {
using type = T; };
433 template <
typename T>
434 using SerialisedType =
typename SerialisedTypeTrait<T>::type;
436 template <
typename T>
437 constexpr auto hasSerialisation = hasInternalVersion<SerialisedType<T>>
438 || hasInternalSerialise<T, SerialisedType<T>>
439 || hasInternalLoad<T, SerialisedType<T>>
440 || hasInternalSave<T, SerialisedType<T>>;
443 enum class SerialisationKind
458 template <
typename T>
459 constexpr auto serialisationKind = []
461 if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T> || std::is_same_v<T, String> || std::is_same_v<T, var>)
462 return SerialisationKind::primitive;
463 else if constexpr (hasSerialisation<T>)
464 return SerialisationKind::internal;
465 else if constexpr (hasSerialisation<SerialisationTraits<T>>)
466 return SerialisationKind::external;
468 return SerialisationKind::none;
472 template <
typename T, SerialisationKind kind = serialisationKind<T>>
473 struct ForwardingSerialisationTraits
475 static constexpr auto marshallingVersion = std::nullopt;
477 template <
typename Archive,
typename Primitive>
478 static auto load (Archive& archive, Primitive& t)
480 if constexpr (std::is_enum_v<Primitive>)
481 return archive (*
reinterpret_cast<std::underlying_type_t<Primitive>*
> (&t));
486 template <
typename Archive,
typename Primitive>
487 static auto save (Archive& archive,
const Primitive& t)
489 if constexpr (std::is_enum_v<Primitive>)
490 return archive (*
reinterpret_cast<const std::underlying_type_t<Primitive>*
> (&t));
500 template <
typename T>
501 struct ForwardingSerialisationTraits<T, SerialisationKind::internal>
503 static constexpr std::optional<int> marshallingVersion { T::marshallingVersion };
505 template <
typename Archive,
typename Item>
506 static auto serialise (Archive& archive, Item& t) ->
decltype (Item::serialise (archive, t)) {
return Item::serialise (archive, t); }
508 template <
typename Archive,
typename Item>
509 static auto load (Archive& archive, Item& t) ->
decltype (Item::load (archive, t)) {
return Item::load (archive, t); }
511 template <
typename Archive,
typename Item>
512 static auto save (Archive& archive,
const Item& t) ->
decltype (Item::save (archive, t)) {
return Item::save (archive, t); }
519 template <
typename T>
520 struct ForwardingSerialisationTraits<T, SerialisationKind::external> : SerialisationTraits<T> {};
522 template <
typename T,
typename =
void>
523 constexpr auto hasSerialise =
false;
525 template <
typename T>
526 constexpr auto hasSerialise<T, std::void_t<
decltype (ForwardingSerialisationTraits<T>::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> =
true;
528 template <
typename T,
typename =
void>
529 constexpr auto hasLoad =
false;
531 template <
typename T>
532 constexpr auto hasLoad<T, std::void_t<
decltype (ForwardingSerialisationTraits<T>::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> =
true;
534 template <
typename T,
typename =
void>
535 constexpr auto hasSave =
false;
537 template <
typename T>
538 constexpr auto hasSave<T, std::void_t<
decltype (ForwardingSerialisationTraits<T>::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> =
true;
540 template <
typename T>
541 constexpr auto delayStaticAssert =
false;
545 template <
typename Archive,
typename T>
546 auto doSave (Archive& archive,
const T& t)
548 if constexpr (serialisationKind<T> == SerialisationKind::none)
549 static_assert (delayStaticAssert<T>,
"No serialisation function found or marshallingVersion unset");
550 else if constexpr (hasSerialise<T> && ! hasSave<T>)
551 return ForwardingSerialisationTraits<T>::serialise (archive, t);
552 else if constexpr (! hasSerialise<T> && hasSave<T>)
553 return ForwardingSerialisationTraits<T>::save (archive, t);
555 static_assert (delayStaticAssert<T>,
"Multiple serialisation functions found");
560 template <
typename Archive,
typename T>
561 auto doLoad (Archive& archive, T& t)
563 if constexpr (serialisationKind<T> == SerialisationKind::none)
564 static_assert (delayStaticAssert<T>,
"No serialisation function found or marshallingVersion unset");
565 else if constexpr (hasSerialise<T> && ! hasLoad<T>)
566 return ForwardingSerialisationTraits<T>::serialise (archive, t);
567 else if constexpr (! hasSerialise<T> && hasLoad<T>)
568 return ForwardingSerialisationTraits<T>::load (archive, t);
570 static_assert (delayStaticAssert<T>,
"Multiple serialisation functions found");
int size() const noexcept
T & value
A reference to a value to wrap.
JUCE_COMPARISON_OPS std::string_view name
A name that corresponds to the value.