1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce
{
/**
Allows serialisation functions to be attached to a specific type without having to modify the
declaration of that type.
A specialisation of SerialisationTraits must include:
- A static constexpr data member named 'marshallingVersion' with a value that is convertible
to std::optional<int>.
- Either:
- Normally, a single function with the following signature:
@code
template <typename Archive, typename Item>
static void serialise (Archive& archive, Item& item);
@endcode
- For types that must do slightly different work when loading and saving, you may supply two
functions with the following signatures, where "T" is a placeholder for the type on which
SerialisationTraits is specialised:
@code
template <typename Archive>
static void load (Archive& archive, T& item);
template <typename Archive>
static void save (Archive& archive, const T& item);
@endcode
If the marshallingVersion converts to a null optional, then all versioning information will be
ignored when marshalling the type. Otherwise, if the value converts to a non-null optional, this
versioning information will be included when serialising the type.
Inside serialise() and load() you may call archive.getVersion() to find the detected version
of the object being deserialised. archive.getVersion() will return an std::optional<int>,
where 'nullopt' indicates that no versioning information was detected.
Marshalling functions can also be specified directly inside the type to be marshalled. This
approach may be preferable as it is more concise. Internal marshalling functions are written
in exactly the same way as external ones; i.e. the type must include a marshallingVersion,
and either a single serialise function, or a load/save pair of functions, as specified above.
@tags{Core}
*/
template <typename T> struct SerialisationTraits
{
/* Intentionally left blank. */
};
#define JUCE_COMPARISON_OPS X(==) X(!=) X(<) X(<=) X(>) X(>=)
/**
Combines an object with a name.
Instances of Named have reference-like semantics. That is, Named stores a reference
to a wrapped value, rather than storing the value internally.
@tparam T the type of reference that is wrapped. Passing "const T" will cause the Named
instance to hold a "const T&"; passing "T" will cause the Named instance to
hold a "T&".
@see named()
@tags{Core}
*/
template <typename T>
struct Named
{
#define X(op) auto operator op (const Named& other) const { return value op other.value; }
JUCE_COMPARISON_OPS
#undef X
std::string_view name; ///< A name that corresponds to the value
T& value; ///< A reference to a value to wrap
};
/** Produces a Named instance that holds a mutable reference. */
template <typename T> constexpr auto named (std::string_view c, T& t) { return Named<T> { c, t }; }
/** Produces a Named instance that holds an immutable reference. */
template <typename T> constexpr auto named (std::string_view c, const T& t) { return Named<const T> { c, t }; }
/**
Holds a reference to some kind of size value, used to indicate that an object being marshalled
is of variable size (e.g. Array, vector, map, set, etc.).
If you need to write your own serialisation routines for a dynamically-sized type, ensure
that you archive an instance of SerialisationSize before any of the contents of the container.
@tparam T the (probably numeric) type of the size value
@see serialisztionSize()
@tags{Core}
*/
template <typename T>
struct SerialisationSize
{
#define X(op) auto operator op (const SerialisationSize& other) const { return size op other.size; }
JUCE_COMPARISON_OPS
#undef X
T& size;
};
/** Produces a SerialisationSize instance that holds a mutable reference to a size value. */
template <typename T> constexpr auto serialisationSize (T& t) -> std::enable_if_t<std::is_integral_v<T>, SerialisationSize<T>> { return { t }; }
/** Produces a SerialisationSize instance that holds an immutable reference to a size value. */
template <typename T> constexpr auto serialisationSize (const T& t) -> std::enable_if_t<std::is_integral_v<T>, SerialisationSize<const T>> { return { t }; }
#undef JUCE_COMPARISON_OPS
//==============================================================================
/*
The following are specialisations of SerialisationTraits for commonly-used types.
*/
#ifndef DOXYGEN
template <typename... Ts>
struct SerialisationTraits<std::vector<Ts...>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = t.size();
archive (serialisationSize (size));
t.resize (size);
for (auto& element : t)
archive (element);
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
archive (serialisationSize (t.size()));
for (auto& element : t)
archive (element);
}
};
template <typename Element, typename Mutex, int minSize>
struct SerialisationTraits<Array<Element, Mutex, minSize>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = t.size();
archive (serialisationSize (size));
t.resize (size);
for (auto& element : t)
archive (element);
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
archive (serialisationSize (t.size()));
for (auto& element : t)
archive (element);
}
};
template <>
struct SerialisationTraits<StringArray>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void serialise (Archive& archive, T& t)
{
archive (t.strings);
}
};
template <typename... Ts>
struct SerialisationTraits<std::pair<Ts...>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void serialise (Archive& archive, T& t)
{
archive (named ("first", t.first), named ("second", t.second));
}
};
template <typename T>
struct SerialisationTraits<std::optional<T>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive>
static void load (Archive& archive, std::optional<T>& t)
{
bool engaged = false;
archive (named ("engaged", engaged));
if (! engaged)
return;
t.emplace();
archive (named ("value", *t));
}
template <typename Archive>
static void save (Archive& archive, const std::optional<T>& t)
{
archive (named ("engaged", t.has_value()));
if (t.has_value())
archive (named ("value", *t));
}
};
template <>
struct SerialisationTraits<std::string>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive>
static void load (Archive& archive, std::string& t)
{
String temporary;
archive (temporary);
t = temporary.toStdString();
}
template <typename Archive>
static void save (Archive& archive, const std::string& t)
{
archive (String (t));
}
};
template <typename... Ts>
struct SerialisationTraits<std::map<Ts...>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = t.size();
archive (serialisationSize (size));
for (auto i = (decltype (size)) 0; i < size; ++i)
{
std::pair<typename T::key_type, typename T::mapped_type> element;
archive (element);
t.insert (element);
}
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
auto size = t.size();
archive (serialisationSize (size));
for (const auto& element : t)
archive (element);
}
};
template <typename... Ts>
struct SerialisationTraits<std::set<Ts...>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = t.size();
archive (serialisationSize (size));
for (auto i = (decltype (size)) 0; i < size; ++i)
{
typename T::value_type element;
archive (element);
t.insert (element);
}
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
auto size = t.size();
archive (serialisationSize (size));
for (const auto& element : t)
archive (element);
}
};
template <size_t N>
struct SerialisationTraits<char[N]>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void serialise (Archive& archive, T& t) { archive (String (t, N)); }
};
template <typename Element, size_t N>
struct SerialisationTraits<Element[N]>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = N;
archive (serialisationSize (size));
for (auto& element : t)
archive (element);
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
const auto size = N;
archive (serialisationSize (size));
for (auto& element : t)
archive (element);
}
};
template <typename Element, size_t N>
struct SerialisationTraits<std::array<Element, N>>
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename T>
static void load (Archive& archive, T& t)
{
auto size = N;
archive (serialisationSize (size));
for (auto& element : t)
archive (element);
}
template <typename Archive, typename T>
static void save (Archive& archive, const T& t)
{
const auto size = N;
archive (serialisationSize (size));
for (auto& element : t)
archive (element);
}
};
/*
This namespace holds utilities for detecting and using serialisation functions.
The contents of this namespace are private, and liable to change, so you shouldn't use any of
the contents directly.
*/
namespace detail
{
struct DummyArchive
{
template <typename... Ts>
bool operator() (Ts&&...);
std::optional<int> getVersion() const { return {}; }
};
template <typename T, typename = void>
constexpr auto hasInternalVersion = false;
template <typename T>
constexpr auto hasInternalVersion<T, std::void_t<decltype (T::marshallingVersion)>> = true;
template <typename Traits, typename T, typename = void>
constexpr auto hasInternalSerialise = false;
template <typename Traits, typename T>
constexpr auto hasInternalSerialise<Traits, T, std::void_t<decltype (Traits::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
template <typename Traits, typename T, typename = void>
constexpr auto hasInternalLoad = false;
template <typename Traits, typename T>
constexpr auto hasInternalLoad<Traits, T, std::void_t<decltype (Traits::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
template <typename Traits, typename T, typename = void>
constexpr auto hasInternalSave = false;
template <typename Traits, typename T>
constexpr auto hasInternalSave<Traits, T, std::void_t<decltype (Traits::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> = true;
template <typename T>
struct SerialisedTypeTrait { using type = T; };
template <typename T>
struct SerialisedTypeTrait<SerialisationTraits<T>> { using type = T; };
template <typename T>
using SerialisedType = typename SerialisedTypeTrait<T>::type;
template <typename T>
constexpr auto hasSerialisation = hasInternalVersion<SerialisedType<T>>
|| hasInternalSerialise<T, SerialisedType<T>>
|| hasInternalLoad<T, SerialisedType<T>>
|| hasInternalSave<T, SerialisedType<T>>;
/* Different kinds of serialisation function. */
enum class SerialisationKind
{
none, // The type doesn't have any serialisation
primitive, // The type has serialisation handling defined directly on the archiver. enums will be converted to equivalent integral values
internal, // The type has internally-defined serialisation utilities
external, // The type has an external specialisation of SerialisationTraits
};
/* The SerialisationKind to use for the type T.
Primitive serialisation is used for arithmetic types, enums, Strings, and vars.
Internal serialisation is used for types that declare an internal marshallingVersion,
serialise(), load(), or save().
External serialisation is used in all other cases.
*/
template <typename T>
constexpr auto serialisationKind = []
{
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T> || std::is_same_v<T, String> || std::is_same_v<T, var>)
return SerialisationKind::primitive;
else if constexpr (hasSerialisation<T>)
return SerialisationKind::internal;
else if constexpr (hasSerialisation<SerialisationTraits<T>>)
return SerialisationKind::external;
else
return SerialisationKind::none;
}();
/* This trait defines the serialisation utilities that are used for primitive types. */
template <typename T, SerialisationKind kind = serialisationKind<T>>
struct ForwardingSerialisationTraits
{
static constexpr auto marshallingVersion = std::nullopt;
template <typename Archive, typename Primitive>
static auto load (Archive& archive, Primitive& t)
{
if constexpr (std::is_enum_v<Primitive>)
return archive (*reinterpret_cast<std::underlying_type_t<Primitive>*> (&t));
else
return archive (t);
}
template <typename Archive, typename Primitive>
static auto save (Archive& archive, const Primitive& t)
{
if constexpr (std::is_enum_v<Primitive>)
return archive (*reinterpret_cast<const std::underlying_type_t<Primitive>*> (&t));
else
return archive (t);
}
};
/* This specialisation will be used for types with internal serialisation.
All members of ForwardingSerialisationTraits forward to the corresponding member of T.
*/
template <typename T>
struct ForwardingSerialisationTraits<T, SerialisationKind::internal>
{
static constexpr std::optional<int> marshallingVersion { T::marshallingVersion };
template <typename Archive, typename Item>
static auto serialise (Archive& archive, Item& t) -> decltype (Item::serialise (archive, t)) { return Item::serialise (archive, t); }
template <typename Archive, typename Item>
static auto load (Archive& archive, Item& t) -> decltype (Item::load (archive, t)) { return Item::load (archive, t); }
template <typename Archive, typename Item>
static auto save (Archive& archive, const Item& t) -> decltype (Item::save (archive, t)) { return Item::save (archive, t); }
};
/* This specialisation will be used for types with external serialisation.
@see SerialisationTraits
*/
template <typename T>
struct ForwardingSerialisationTraits<T, SerialisationKind::external> : SerialisationTraits<T> {};
template <typename T, typename = void>
constexpr auto hasSerialise = false;
template <typename T>
constexpr auto hasSerialise<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::serialise (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
template <typename T, typename = void>
constexpr auto hasLoad = false;
template <typename T>
constexpr auto hasLoad<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::load (std::declval<DummyArchive&>(), std::declval<T&>()))>> = true;
template <typename T, typename = void>
constexpr auto hasSave = false;
template <typename T>
constexpr auto hasSave<T, std::void_t<decltype (ForwardingSerialisationTraits<T>::save (std::declval<DummyArchive&>(), std::declval<const T&>()))>> = true;
template <typename T>
constexpr auto delayStaticAssert = false;
/* Calls the correct function (serialise or save) to save the argument t to the archive.
*/
template <typename Archive, typename T>
auto doSave (Archive& archive, const T& t)
{
if constexpr (serialisationKind<T> == SerialisationKind::none)
static_assert (delayStaticAssert<T>, "No serialisation function found or marshallingVersion unset");
else if constexpr (hasSerialise<T> && ! hasSave<T>)
return ForwardingSerialisationTraits<T>::serialise (archive, t);
else if constexpr (! hasSerialise<T> && hasSave<T>)
return ForwardingSerialisationTraits<T>::save (archive, t);
else
static_assert (delayStaticAssert<T>, "Multiple serialisation functions found");
}
/* Calls the correct function (serialise or load) to load the argument t from the archive.
*/
template <typename Archive, typename T>
auto doLoad (Archive& archive, T& t)
{
if constexpr (serialisationKind<T> == SerialisationKind::none)
static_assert (delayStaticAssert<T>, "No serialisation function found or marshallingVersion unset");
else if constexpr (hasSerialise<T> && ! hasLoad<T>)
return ForwardingSerialisationTraits<T>::serialise (archive, t);
else if constexpr (! hasSerialise<T> && hasLoad<T>)
return ForwardingSerialisationTraits<T>::load (archive, t);
else
static_assert (delayStaticAssert<T>, "Multiple serialisation functions found");
}
} // namespace detail
#endif
} // namespace juce
|