EnTT  2.7.3
docs/entity.md
1 # Crash Course: entity-component system
2 
3 <!--
4 @cond TURN_OFF_DOXYGEN
5 -->
6 # Table of Contents
7 
8 * [Introduction](#introduction)
9 * [Design choices](#design-choices)
10  * [A bitset-free entity-component system](#a-bitset-free-entity-component-system)
11  * [Pay per use](#pay-per-use)
12 * [Vademecum](#vademecum)
13 * [The Registry, the Entity and the Component](#the-registry-the-entity-and-the-component)
14  * [Single instance components](#single-instance-components)
15  * [Observe changes](#observe-changes)
16  * [Who let the tags out?](#who-let-the-tags-out)
17  * [Runtime components](#runtime-components)
18  * [A journey through a plugin](#a-journey-through-a-plugin)
19  * [Sorting: is it possible?](#sorting-is-it-possible)
20  * [Snapshot: complete vs continuous](#snapshot-complete-vs-continuous)
21  * [Snapshot loader](#snapshot-loader)
22  * [Continuous loader](#continuous-loader)
23  * [Archives](#archives)
24  * [One example to rule them all](#one-example-to-rule-them-all)
25  * [Prototype](#prototype)
26  * [Helpers](#helpers)
27  * [Dependency function](#dependency-function)
28  * [Labels](#labels)
29  * [Null entity](#null-entity)
30 * [View: to persist or not to persist?](#view-to-persist-or-not-to-persist)
31  * [Standard View](#standard-view)
32  * [Single component standard view](#single-component-standard-view)
33  * [Multi component standard view](#multi-component-standard-view)
34  * [Persistent View](#persistent-view)
35  * [Raw View](#raw-view)
36  * [Runtime View](#runtime-view)
37  * [Give me everything](#give-me-everything)
38 * [Iterations: what is allowed and what is not](#iterations-what-is-allowed-and-what-is-not)
39 * [Multithreading](#multithreading)
40 <!--
41 @endcond TURN_OFF_DOXYGEN
42 -->
43 
44 # Introduction
45 
46 `EnTT` is a header-only, tiny and easy to use entity-component system (and much
47 more) written in modern C++.<br/>
48 The entity-component-system (also known as _ECS_) is an architectural pattern
49 used mostly in game development.
50 
51 # Design choices
52 
53 ## A bitset-free entity-component system
54 
55 `EnTT` is a _bitset-free_ entity-component system that doesn't require users to
56 specify the component set at compile-time.<br/>
57 This is why users can instantiate the core class simply like:
58 
59 ```cpp
60 entt::DefaultRegistry registry;
61 ```
62 
63 In place of its more annoying and error-prone counterpart:
64 
65 ```cpp
66 entt::DefaultRegistry<Comp0, Comp1, ..., CompN> registry;
67 ```
68 
69 ## Pay per use
70 
71 `EnTT` is entirely designed around the principle that users have to pay only for
72 what they want.
73 
74 When it comes to using an entity-component system, the tradeoff is usually
75 between performance and memory usage. The faster it is, the more memory it uses.
76 However, slightly worse performance along non-critical paths are the right price
77 to pay to reduce memory usage and I've always wondered why this kind of tools do
78 not leave me the choice.<br/>
79 `EnTT` follows a completely different approach. It squeezes the best from the
80 basic data structures and gives users the possibility to pay more for higher
81 performance where needed.<br/>
82 The disadvantage of this approach is that users need to know the systems they
83 are working on and the tools they are using. Otherwise, the risk to ruin the
84 performance along critical paths is high.
85 
86 So far, this choice has proven to be a good one and I really hope it can be for
87 many others besides me.
88 
89 # Vademecum
90 
91 The `Registry` to store, the views to iterate. That's all.
92 
93 An entity (the _E_ of an _ECS_) is an opaque identifier that users should just
94 use as-is and store around if needed. Do not try to inspect an entity
95 identifier, its format can change in future and a registry offers all the
96 functionalities to query them out-of-the-box. The underlying type of an entity
97 (either `std::uint16_t`, `std::uint32_t` or `std::uint64_t`) can be specified
98 when defining a registry (actually the `DefaultRegistry` is nothing more than a
99 `Registry` where the type of the entities is `std::uint32_t`).<br/>
100 Components (the _C_ of an _ECS_) should be plain old data structures or more
101 complex and movable data structures with a proper constructor. Actually, the
102 sole requirement of a component type is that it must be both move constructible
103 and move assignable. They are list initialized by using the parameters provided
104 to construct the component itself. No need to register components or their types
105 neither with the registry nor with the entity-component system at all.<br/>
106 Systems (the _S_ of an _ECS_) are just plain functions, functors, lambdas or
107 whatever users want. They can accept a `Registry` or a view of any type and use
108 them the way they prefer. No need to register systems or their types neither
109 with the registry nor with the entity-component system at all.
110 
111 The following sections will explain in short how to use the entity-component
112 system, the core part of the whole library.<br/>
113 In fact, the project is composed of many other classes in addition to those
114 describe below. For more details, please refer to the inline documentation.
115 
116 # The Registry, the Entity and the Component
117 
118 A registry can store and manage entities, as well as create views to iterate the
119 underlying data structures.<br/>
120 `Registry` is a class template that lets users decide what's the preferred type
121 to represent an entity. Because `std::uint32_t` is large enough for almost all
122 the cases, there exists also an alias named `DefaultRegistry` for
123 `Registry<std::uint32_t>`.
124 
125 Entities are represented by _entity identifiers_. An entity identifier is an
126 opaque type that users should not inspect or modify in any way. It carries
127 information about the entity itself and its version.
128 
129 A registry can be used both to construct and destroy entities:
130 
131 ```cpp
132 // constructs a naked entity with no components and returns its identifier
133 auto entity = registry.create();
134 
135 // destroys an entity and all its components
136 registry.destroy(entity);
137 ```
138 
139 Entities can also be destroyed _by type_, that is by specifying the types of the
140 tags or components that identify them:
141 
142 ```cpp
143 // destroys the entity that owns the given tag, if any
144 registry.destroy<MyTag>(entt::tag_t{});
145 
146 // destroys the entities that own the given components, if any
147 registry.destroy<AComponent, AnotherComponent>();
148 ```
149 
150 When an entity is destroyed, the registry can freely reuse it internally with a
151 slightly different identifier. In particular, the version of an entity is
152 increased each and every time it's discarded.<br/>
153 In case entity identifiers are stored around, the registry offers all the
154 functionalities required to test them and get out of the them all the
155 information they carry:
156 
157 ```cpp
158 // returns true if the entity is still valid, false otherwise
159 bool b = registry.valid(entity);
160 
161 // gets the version contained in the entity identifier
162 auto version = registry.version(entity);
163 
164 // gets the actual version for the given entity
165 auto curr = registry.current(entity);
166 ```
167 
168 Components can be assigned to or removed from entities at any time with a few
169 calls to member functions of the registry. As for the entities, the registry
170 offers also a set of functionalities users can use to work with the components.
171 
172 The `assign` member function template creates, initializes and assigns to an
173 entity the given component. It accepts a variable number of arguments to
174 construct the component itself if present:
175 
176 ```cpp
177 registry.assign<Position>(entity, 0., 0.);
178 
179 // ...
180 
181 Velocity &velocity = registry.assign<Velocity>(entity);
182 velocity.dx = 0.;
183 velocity.dy = 0.;
184 ```
185 
186 If an entity already has the given component, the `replace` member function
187 template can be used to replace it:
188 
189 ```cpp
190 registry.replace<Position>(entity, 0., 0.);
191 
192 // ...
193 
194 Velocity &velocity = registry.replace<Velocity>(entity);
195 velocity.dx = 0.;
196 velocity.dy = 0.;
197 ```
198 
199 In case users want to assign a component to an entity, but it's unknown whether
200 the entity already has it or not, `accommodate` does the work in a single call
201 (there is a performance penalty to pay for this mainly due to the fact that it
202 has to check if the entity already has the given component or not):
203 
204 ```cpp
205 registry.accommodate<Position>(entity, 0., 0.);
206 
207 // ...
208 
209 Velocity &velocity = registry.accommodate<Velocity>(entity);
210 velocity.dx = 0.;
211 velocity.dy = 0.;
212 ```
213 
214 Note that `accommodate` is a slightly faster alternative for the following
215 `if/else` statement and nothing more:
216 
217 ```cpp
218 if(registry.has<Comp>(entity)) {
219  registry.replace<Comp>(entity, arg1, argN);
220 } else {
221  registry.assign<Comp>(entity, arg1, argN);
222 }
223 ```
224 
225 As already shown, if in doubt about whether or not an entity has one or more
226 components, the `has` member function template may be useful:
227 
228 ```cpp
229 bool b = registry.has<Position, Velocity>(entity);
230 ```
231 
232 On the other side, if the goal is to delete a single component, the `remove`
233 member function template is the way to go when it's certain that the entity owns
234 a copy of the component:
235 
236 ```cpp
237 registry.remove<Position>(entity);
238 ```
239 
240 Otherwise consider to use the `reset` member function. It behaves similarly to
241 `remove` but with a strictly defined behavior (and a performance penalty is the
242 price to pay for this). In particular it removes the component if and only if it
243 exists, otherwise it returns safely to the caller:
244 
245 ```cpp
246 registry.reset<Position>(entity);
247 ```
248 
249 There exist also two other _versions_ of the `reset` member function:
250 
251 * If no entity is passed to it, `reset` will remove the given component from
252  each entity that has it:
253 
254  ```cpp
255  registry.reset<Position>();
256  ```
257 
258 * If neither the entity nor the component are specified, all the entities still
259  in use and their components are destroyed:
260 
261  ```cpp
262  registry.reset();
263  ```
264 
265 Finally, references to components can be retrieved simply by doing this:
266 
267 ```cpp
268 const auto &cregistry = registry;
269 
270 // const and non-const reference
271 const Position &position = cregistry.get<Position>(entity);
272 Position &position = registry.get<Position>(entity);
273 
274 // const and non-const references
275 std::tuple<const Position &, const Velocity &> tup = cregistry.get<Position, Velocity>(entity);
276 std::tuple<Position &, Velocity &> tup = registry.get<Position, Velocity>(entity);
277 ```
278 
279 The `get` member function template gives direct access to the component of an
280 entity stored in the underlying data structures of the registry.
281 
282 ## Single instance components
283 
284 In those cases where all what is needed is a single instance component, tags are
285 the right tool to achieve the purpose.<br/>
286 Tags undergo the same requirements of components. They can be either plain old
287 data structures or more complex and movable data structures with a proper
288 constructor.<br/>
289 Actually, the same type can be used both as a tag and as a component and the
290 registry will not complain about it. It is up to users to properly manage their
291 own types. In some cases, the tag `tag_t` must also be used in order to
292 disambiguate overloads of member functions.
293 
294 Attaching tags to entities and removing them is trivial:
295 
296 ```cpp
297 auto player = registry.create();
298 auto camera = registry.create();
299 
300 // attaches a default-initialized tag to an entity
301 registry.assign<PlayingCharacter>(entt::tag_t{}, player);
302 
303 // attaches a tag to an entity and initializes it
304 registry.assign<Camera>(entt::tag_t{}, camera, player);
305 
306 // removes tags from their owners
307 registry.remove<PlayingCharacter>();
308 registry.remove<Camera>();
309 ```
310 
311 In case a tag already has an owner, its content can be updated by means of the
312 `replace` member function template and the ownership of the tag can be
313 transferred to another entity using the `move` member function template:
314 
315 ```
316 // replaces the content of the given tag
317 Point &point = registry.replace<Point>(entt::tag_t{}, 1.f, 1.f);
318 
319 // transfers the ownership of the tag to another entity
320 entity_type prev = registry.move<Point>(next);
321 ```
322 
323 If in doubt about whether or not a tag already has an owner, the `has` member
324 function template may be useful:
325 
326 ```cpp
327 bool b = registry.has<PlayingCharacter>();
328 ```
329 
330 References to tags can be retrieved simply by doing this:
331 
332 ```cpp
333 const auto &cregistry = registry;
334 
335 // either a non-const reference ...
336 PlayingCharacter &player = registry.get<PlayingCharacter>();
337 
338 // ... or a const one
339 const Camera &camera = cregistry.get<Camera>();
340 ```
341 
342 The `get` member function template gives direct access to the tag as stored in
343 the underlying data structures of the registry.
344 
345 As shown above, in almost all the cases the entity identifier isn't required.
346 Since a single instance component can have only one associated entity, it
347 doesn't make much sense to mention it explicitly.<br/>
348 To find out who the owner is, just do the following:
349 
350 ```cpp
351 auto player = registry.attachee<PlayingCharacter>();
352 ```
353 
354 Note that iterating tags isn't possible for obvious reasons. Tags give direct
355 access to single entities and nothing more.
356 
357 ## Observe changes
358 
359 Because of how the registry works internally, it stores a couple of signal
360 handlers for each pool in order to notify some of its data structures on the
361 construction and destruction of components.<br/>
362 These signal handlers are also exposed and made available to users. This is the
363 basic brick to build fancy things like dependencies and reactive systems.
364 
365 To get a sink to be used to connect and disconnect listeners so as to be
366 notified on the creation of a component, use the `construction` member function:
367 
368 ```cpp
369 // connects a free function
370 registry.construction<Position>().connect<&MyFreeFunction>();
371 
372 // connects a member function
373 registry.construction<Position>().connect<MyClass, &MyClass::member>(&instance);
374 
375 // disconnects a free function
376 registry.construction<Position>().disconnect<&MyFreeFunction>();
377 
378 // disconnects a member function
379 registry.construction<Position>().disconnect<MyClass, &MyClass::member>(&instance);
380 ```
381 
382 To be notified when components are destroyed, use the `destruction` member
383 function instead.
384 
385 The function type of a listener is the same in both cases:
386 
387 ```cpp
388 void(Registry<Entity> &, Entity);
389 ```
390 
391 In other terms, a listener is provided with the registry that triggered the
392 notification and the entity affected by the change. Note also that:
393 
394 * Listeners are invoked **after** components have been assigned to entities.
395 * Listeners are invoked **before** components have been removed from entities.
396 * The order of invocation of the listeners isn't guaranteed in any case.
397 
398 There are also some limitations on what a listener can and cannot do. In
399 particular:
400 
401 * Connecting and disconnecting other functions from within the body of a
402  listener should be avoided. It can lead to undefined behavior in some cases.
403 * Assigning and removing components and tags from within the body of a listener
404  that observes the destruction of instances of a given type should be avoided.
405  It can lead to undefined behavior in some cases. This type of listeners is
406  intended to provide users with an easy way to perform cleanup and nothing
407  more.
408 
409 To a certain extent, these limitations do not apply. However, it is risky to try
410 to force them and users should respect the limitations unless they know exactly
411 what they are doing. Subtle bugs are the price to pay in case of errors
412 otherwise.
413 
414 In general, events and therefore listeners must not be used as replacements for
415 systems. They should not contain much logic and interactions with a registry
416 should be kept to a minimum, if possible. Note also that the greater the number
417 of listeners, the greater the performance hit when components are created or
418 destroyed.
419 
420 ### Who let the tags out?
421 
422 As an extension, signals are also provided with tags. Although they are not
423 strictly required internally, it makes sense that a user expects signal support
424 even when it comes to tags actually.<br/>
425 Signals for tags undergo exactly the same requirements of those introduced for
426 components. Also the function type for a listener is the same and it's invoked
427 with the same guarantees discussed above.
428 
429 To get the sinks for a tag just use tag `tag_t` to disambiguate overloads of
430 member functions as in the following example:
431 
432 ```cpp
433 registry.construction<MyTag>(entt::tag_t{}).connect<&MyFreeFunction>();
434 registry.destruction<MyTag>(entt::tag_t{}).connect<MyClass, &MyClass::member>(&instance);
435 ```
436 
437 Listeners for tags and components are managed separately and do not influence
438 each other in any case. Therefore, note that the greater the number of listeners
439 for a type, the greater the performance hit when a tag of the given type is
440 created or destroyed.
441 
442 ## Runtime components
443 
444 Defining components at runtime is useful to support plugin systems and mods in
445 general. However, it seems impossible with a tool designed around a bunch of
446 templates. Indeed it's not that difficult.<br/>
447 Of course, some features cannot be easily exported into a runtime
448 environment. As an example, sorting a group of components defined at runtime
449 isn't for free if compared to most of the other operations. However, the basic
450 functionalities of an entity-component system such as `EnTT` fit the problem
451 perfectly and can also be used to manage runtime components if required.<br/>
452 All that is necessary to do it is to know the identifiers of the components. An
453 identifier is nothing more than a number or similar that can be used at runtime
454 to work with the type system.
455 
456 In `EnTT`, identifiers are easily accessible:
457 
458 ```cpp
459 entt::DefaultRegistry registry;
460 
461 // standard component identifier
462 auto ctype = registry.type<Position>();
463 
464 // single instance component identifier
465 auto ttype = registry.type<PlayingCharacter>(entt::tag_t{});
466 ```
467 
468 Once the identifiers are made available, almost everything becomes pretty
469 simple.
470 
471 ### A journey through a plugin
472 
473 `EnTT` comes with an example (actually a test) that shows how to integrate
474 compile-time and runtime components in a stack based JavaScript environment. It
475 uses [`Duktape`](https://github.com/svaarala/duktape) under the hood, mainly
476 because I wanted to learn how it works at the time I was writing the code.
477 
478 The code is not production-ready and overall performance can be highly improved.
479 However, I sacrificed optimizations in favor of a more readable piece of code. I
480 hope I succeeded.<br/>
481 Note also that this isn't neither the only nor (probably) the best way to do it.
482 In fact, the right way depends on the scripting language and the problem one is
483 facing in general.<br/>
484 That being said, feel free to use it at your own risk.
485 
486 The basic idea is that of creating a compile-time component aimed to map all the
487 runtime components assigned to an entity.<br/>
488 Identifiers come in use to address the right function from a map when invoked
489 from the runtime environment and to filter entities when iterating.<br/>
490 With a bit of gymnastic, one can narrow views and improve the performance to
491 some extent but it was not the goal of the example.
492 
493 ## Sorting: is it possible?
494 
495 It goes without saying that sorting entities and components is possible with
496 `EnTT`.<br/>
497 In fact, there are two functions that respond to slightly different needs:
498 
499 * Components can be sorted directly:
500 
501  ```cpp
502  registry.sort<Renderable>([](const auto &lhs, const auto &rhs) {
503  return lhs.z < rhs.z;
504 
505  });
506  ```
507 
508  There exists also the possibility to use a custom sort function object, as
509  long as it adheres to the requirements described in the inline
510  documentation.<br/>
511  This is possible mainly because users can get much more with a custom sort
512  function object if the pattern of usage is known. As an example, in case of an
513  almost sorted pool, quick sort could be much, much slower than insertion sort.
514 
515 * Components can be sorted according to the order imposed by another component:
516 
517  ```cpp
518  registry.sort<Movement, Physics>();
519  ```
520 
521  In this case, instances of `Movement` are arranged in memory so that cache
522  misses are minimized when the two components are iterated together.
523 
524 ## Snapshot: complete vs continuous
525 
526 The `Registry` class offers basic support to serialization.<br/>
527 It doesn't convert components and tags to bytes directly, there wasn't the need
528 of another tool for serialization out there. Instead, it accepts an opaque
529 object with a suitable interface (namely an _archive_) to serialize its internal
530 data structures and restore them later. The way types and instances are
531 converted to a bunch of bytes is completely in charge to the archive and thus to
532 final users.
533 
534 The goal of the serialization part is to allow users to make both a dump of the
535 entire registry or a narrower snapshot, that is to select only the components
536 and the tags in which they are interested.<br/>
537 Intuitively, the use cases are different. As an example, the first approach is
538 suitable for local save/restore functionalities while the latter is suitable for
539 creating client-server applications and for transferring somehow parts of the
540 representation side to side.
541 
542 To take a snapshot of the registry, use the `snapshot` member function. It
543 returns a temporary object properly initialized to _save_ the whole registry or
544 parts of it.
545 
546 Example of use:
547 
548 ```cpp
549 OutputArchive output;
550 
551 registry.snapshot()
552  .entities(output)
553  .destroyed(output)
554  .component<AComponent, AnotherComponent>(output)
555  .tag<MyTag>(output);
556 ```
557 
558 It isn't necessary to invoke all these functions each and every time. What
559 functions to use in which case mostly depends on the goal and there is not a
560 golden rule to do that.
561 
562 The `entities` member function asks the registry to serialize all the entities
563 that are still in use along with their versions. On the other side, the
564 `destroyed` member function tells to the registry to serialize the entities that
565 have been destroyed and are no longer in use.<br/>
566 These two functions can be used to save and restore the whole set of entities
567 with the versions they had during serialization.
568 
569 The `component` member function is a function template the aim of which is to
570 store aside components. The presence of a template parameter list is a
571 consequence of a couple of design choices from the past and in the present:
572 
573 * First of all, there is no reason to force a user to serialize all the
574  components at once and most of the times it isn't desiderable. As an example,
575  in case the stuff for the HUD in a game is put into the registry for some
576  reasons, its components can be freely discarded during a serialization step
577  because probably the software already knows how to reconstruct the HUD
578  correctly from scratch.
579 
580 * Furthermore, the registry makes heavy use of _type-erasure_ techniques
581  internally and doesn't know at any time what types of components it contains.
582  Therefore being explicit at the call point is mandatory.
583 
584 There exists also another version of the `component` member function that
585 accepts a range of entities to serialize. This version is a bit slower than the
586 other one, mainly because it iterates the range of entities more than once for
587 internal purposes. However, it can be used to filter out those entities that
588 shouldn't be serialized for some reasons.<br/>
589 As an example:
590 
591 ```cpp
592 const auto view = registry.view<Serialize>();
593 OutputArchive output;
594 
595 registry.snapshot()
596  .component<AComponent, AnotherComponent>(output, view.cbegin(), view.cend());
597 ```
598 
599 The `tag` member function is similar to the previous one, apart from the fact
600 that it works with tags and not with components.<br/>
601 Note also that both `component` and `tag` store items along with entities. It
602 means that they work properly without a call to the `entities` member function.
603 
604 Once a snapshot is created, there exist mainly two _ways_ to load it: as a whole
605 and in a kind of _continuous mode_.<br/>
606 The following sections describe both loaders and archives in details.
607 
608 ### Snapshot loader
609 
610 A snapshot loader requires that the destination registry be empty and loads all
611 the data at once while keeping intact the identifiers that the entities
612 originally had.<br/>
613 To do that, the registry offers a member function named `restore` that returns a
614 temporary object properly initialized to _restore_ a snapshot.
615 
616 Example of use:
617 
618 ```cpp
619 InputArchive input;
620 
621 registry.restore()
622  .entities(input)
623  .destroyed(input)
624  .component<AComponent, AnotherComponent>(input)
625  .tag<MyTag>(input)
626  .orphans();
627 ```
628 
629 It isn't necessary to invoke all these functions each and every time. What
630 functions to use in which case mostly depends on the goal and there is not a
631 golden rule to do that. For obvious reasons, what is important is that the data
632 are restored in exactly the same order in which they were serialized.
633 
634 The `entities` and `destroyed` member functions restore the sets of entities and
635 the versions that the entities originally had at the source.
636 
637 The `component` member function restores all and only the components specified
638 and assigns them to the right entities. Note that the template parameter list
639 must be exactly the same used during the serialization. The same applies to the
640 `tag` member function.
641 
642 The `orphans` member function literally destroys those entities that have
643 neither components nor tags. It's usually useless if the snapshot is a full dump
644 of the source. However, in case all the entities are serialized but only few
645 components and tags are saved, it could happen that some of the entities have
646 neither components nor tags once restored. The best users can do to deal with
647 them is to destroy those entities and thus update their versions.
648 
649 ### Continuous loader
650 
651 A continuous loader is designed to load data from a source registry to a
652 (possibly) non-empty destination. The loader can accommodate in a registry more
653 than one snapshot in a sort of _continuous loading_ that updates the
654 destination one step at a time.<br/>
655 Identifiers that entities originally had are not transferred to the target.
656 Instead, the loader maps remote identifiers to local ones while restoring a
657 snapshot. Because of that, this kind of loader offers a way to update
658 automatically identifiers that are part of components or tags (as an example, as
659 data members or gathered in a container).<br/>
660 Another difference with the snapshot loader is that the continuous loader does
661 not need to work with the private data structures of a registry. Furthermore, it
662 has an internal state that must persist over time. Therefore, there is no reason
663 to create it by means of a registry, or to limit its lifetime to that of a
664 temporary object.
665 
666 Example of use:
667 
668 ```cpp
669 entt::ContinuousLoader<entity_type> loader{registry};
670 InputArchive input;
671 
672 loader.entities(input)
673  .destroyed(input)
674  .component<AComponent, AnotherComponent, DirtyComponent>(input, &DirtyComponent::parent, &DirtyComponent::child)
675  .tag<MyTag, DirtyTag>(input, &DirtyTag::container)
676  .orphans()
677  .shrink();
678 ```
679 
680 It isn't necessary to invoke all these functions each and every time. What
681 functions to use in which case mostly depends on the goal and there is not a
682 golden rule to do that. For obvious reasons, what is important is that the data
683 are restored in exactly the same order in which they were serialized.
684 
685 The `entities` and `destroyed` member functions restore groups of entities and
686 map each entity to a local counterpart when required. In other terms, for each
687 remote entity identifier not yet registered by the loader, the latter creates a
688 local identifier so that it can keep the local entity in sync with the remote
689 one.
690 
691 The `component` and `tag` member functions restore all and only the components
692 and the tags specified and assign them to the right entities.<br/>
693 In case the component or the tag contains entities itself (either as data
694 members of type `entity_type` or as containers of entities), the loader can
695 update them automatically. To do that, it's enough to specify the data members
696 to update as shown in the example.
697 
698 The `orphans` member function literally destroys those entities that have
699 neither components nor tags after a restore. It has exactly the same purpose
700 described in the previous section and works the same way.
701 
702 Finally, `shrink` helps to purge local entities that no longer have a remote
703 conterpart. Users should invoke this member function after restoring each
704 snapshot, unless they know exactly what they are doing.
705 
706 ### Archives
707 
708 Archives must publicly expose a predefined set of member functions. The API is
709 straightforward and consists only of a group of function call operators that
710 are invoked by the snapshot class and the loaders.
711 
712 In particular:
713 
714 * An output archive, the one used when creating a snapshot, must expose a
715  function call operator with the following signature to store entities:
716 
717  ```cpp
718  void operator()(Entity);
719  ```
720 
721  Where `Entity` is the type of the entities used by the registry. Note that all
722  the member functions of the snapshot class make also an initial call to this
723  endpoint to save the _size_ of the set they are going to store.<br/>
724  In addition, an archive must accept a pair of entity and either component or
725  tag for each type to be serialized. Therefore, given a type `T`, the archive
726  must contain a function call operator with the following signature:
727 
728  ```cpp
729  void operator()(Entity, const T &);
730  ```
731 
732  The output archive can freely decide how to serialize the data. The register
733  is not affected at all by the decision.
734 
735 * An input archive, the one used when restoring a snapshot, must expose a
736  function call operator with the following signature to load entities:
737 
738  ```cpp
739  void operator()(Entity &);
740  ```
741 
742  Where `Entity` is the type of the entities used by the registry. Each time the
743  function is invoked, the archive must read the next element from the
744  underlying storage and copy it in the given variable. Note that all the member
745  functions of a loader class make also an initial call to this endpoint to read
746  the _size_ of the set they are going to load.<br/>
747  In addition, the archive must accept a pair of entity and either component or
748  tag for each type to be restored. Therefore, given a type `T`, the archive
749  must contain a function call operator with the following signature:
750 
751  ```cpp
752  void operator()(Entity &, T &);
753  ```
754 
755  Every time such an operator is invoked, the archive must read the next
756  elements from the underlying storage and copy them in the given variables.
757 
758 ### One example to rule them all
759 
760 `EnTT` comes with some examples (actually some tests) that show how to integrate
761 a well known library for serialization as an archive. It uses
762 [`Cereal C++`](https://uscilab.github.io/cereal/) under the hood, mainly
763 because I wanted to learn how it works at the time I was writing the code.
764 
765 The code is not production-ready and it isn't neither the only nor (probably)
766 the best way to do it. However, feel free to use it at your own risk.
767 
768 The basic idea is to store everything in a group of queues in memory, then bring
769 everything back to the registry with different loaders.
770 
771 ## Prototype
772 
773 A prototype defines a type of an application in terms of its parts. They can be
774 used to assign components to entities of a registry at once.<br/>
775 Roughly speaking, in most cases prototypes can be considered just as templates
776 to use to initialize entities according to _concepts_. In fact, users can create
777 how many prototypes they want, each one initialized differently from the others.
778 
779 The following is an example of use of a prototype:
780 
781 ```cpp
782 entt::DefaultRegistry registry;
783 entt::DefaultPrototype prototype{registry};
784 
785 prototype.set<Position>(100.f, 100.f);
786 prototype.set<Velocity>(0.f, 0.f);
787 
788 // ...
789 
790 const auto entity = prototype();
791 ```
792 
793 To assign and remove components from a prototype, it offers two dedicated member
794 functions named `set` and `unset`. The `has` member function can be used to know
795 if a given prototype contains one or more components and the `get` member
796 function can be used to retrieve the components.
797 
798 Creating an entity from a prototype is straightforward:
799 
800 * To create a new entity from scratch and assign it a prototype, this is the way
801  to go:
802  ```cpp
803  const auto entity = prototype();
804  ```
805  It is equivalent to the following invokation:
806  ```cpp
807  const auto entity = prototype.create();
808  ```
809 
810 * In case we want to initialize an already existing entity, we can provide the
811  `operator()` directly with the entity identifier:
812  ```cpp
813  prototype(entity);
814  ```
815  It is equivalent to the following invokation:
816  ```cpp
817  prototype.assign(entity);
818  ```
819  Note that existing components aren't overwritten in this case. Only those
820  components that the entity doesn't own yet are copied over. All the other
821  components remain unchanged.
822 
823 * Finally, to assign or replace all the components for an entity, thus
824  overwriting existing ones:
825  ```cpp
826  prototype.accommodate(entity);
827  ```
828 
829 In the examples above, the prototype uses its underlying registry to create
830 entities and components both for its purposes and when it's cloned. To use a
831 different repository to clone a prototype, all the member functions accept also
832 a reference to a valid registry as a first argument.
833 
834 Prototypes are a very useful tool that can save a lot of typing sometimes.
835 Furthermore, the codebase may be easier to maintain, since updating a prototype
836 is much less error prone than jumping around in the codebase to update all the
837 snippets copied and pasted around to initialize entities and components.
838 
839 ## Helpers
840 
841 The so called _helpers_ are small classes and functions mainly designed to offer
842 built-in support for the most basic functionalities.<br/>
843 The list of helpers will grow longer as time passes and new ideas come out.
844 
845 ### Dependency function
846 
847 A _dependency function_ is a predefined listener, actually a function template
848 to use to automatically assign components to an entity when a type has a
849 dependency on some other types.<br/>
850 The following adds components `AType` and `AnotherType` whenever `MyType` is
851 assigned to an entity:
852 
853 ```cpp
854 entt::connnect<AType, AnotherType>(registry.construction<MyType>());
855 ```
856 
857 A component is assigned to an entity and thus default initialized only in case
858 the entity itself hasn't it yet. It means that already existent components won't
859 be overriden.<br/>
860 A dependency can easily be broken by means of the following function template:
861 
862 ```cpp
863 entt::disconnect<AType, AnotherType>(registry.construction<MyType>());
864 ```
865 
866 ### Labels
867 
868 There's nothing magical about the way labels can be assigned to entities while
869 avoiding a performance hit at runtime. Nonetheless, the syntax can be annoying
870 and that's why a more user-friendly shortcut is provided to do it.<br/>
871 This shortcut is the alias template `entt::label`.
872 
873 If used in combination with hashed strings, it helps to use labels where types
874 would be required otherwise. As an example:
875 
876 ```cpp
877 registry.assign<entt::label<"enemy"_hs>>(entity);
878 ```
879 
880 ## Null entity
881 
882 In `EnTT`, there exists a sort of _null entity_ made available to users that is
883 accessible via the `entt::null` variable.<br/>
884 The library guarantees that the following expression always returns false:
885 
886 ```cpp
887 registry.valid(entt::null);
888 ```
889 
890 In other terms, a registry will reject the null entity in all cases because it
891 isn't considered valid. It means that the null entity cannot own components or
892 tags for obvious reasons.<br/>
893 The type of the null entity is internal and should not be used for any purpose
894 other than defining the null entity itself. However, there exist implicit
895 conversions from the null entity to identifiers of any allowed type:
896 
897 ```cpp
898 typename entt::DefaultRegistry::entity_type null = entt::null;
899 ```
900 
901 Similarly, the null entity can be compared to any other identifier:
902 
903 ```cpp
904 const auto entity = registry.create();
905 const bool null = (entity == entt::null);
906 ```
907 
908 # View: to persist or not to persist?
909 
910 First of all, it is worth answering an obvious question: why views?<br/>
911 Roughly speaking, they are a good tool to enforce single responsibility. A
912 system that has access to a registry can create and destroy entities, as well as
913 assign and remove components. On the other side, a system that has access to a
914 view can only iterate entities and their components, then read or update the
915 data members of the latter.<br/>
916 It is a subtle difference that can help designing a better software sometimes.
917 
918 There are mainly four kinds of views: standard (also known as `View`),
919 persistent (also known as `PersistentView`), raw (also known as `RawView`) and
920 runtime (also known as `RuntimeView`).<br/>
921 All of them have pros and cons to take in consideration. In particular:
922 
923 * Standard views:
924 
925  Pros:
926 
927  * They work out-of-the-box and don't require any dedicated data structure.
928  * Creating and destroying them isn't expensive at all because they don't have
929  any type of initialization.
930  * They are the best tool for iterating entities for a single component.
931  * They are the best tool for iterating entities for multiple components when
932  one of the components is assigned to a significantly low number of entities.
933  * They don't affect any other operations of the registry.
934 
935  Cons:
936 
937  * Their performance tend to degenerate when the number of components to
938  iterate grows up and the most of the entities have all of them.
939 
940 * Persistent views:
941 
942  Pros:
943 
944  * Once prepared, creating and destroying them isn't expensive at all because
945  they don't have any type of initialization.
946  * They are the best tool for iterating entities for multiple components when
947  most entities have them all.
948 
949  Cons:
950 
951  * They have dedicated data structures and thus affect the memory usage to a
952  minimal extent.
953  * If not previously prepared, the first time they are used they go through an
954  initialization step that could take a while.
955  * They affect to a minimum the creation and destruction of entities and
956  components. In other terms: the more persistent views there will be, the
957  less performing will be creating and destroying entities and components.
958 
959 * Raw views:
960 
961  Pros:
962 
963  * They work out-of-the-box and don't require any dedicated data structure.
964  * Creating and destroying them isn't expensive at all because they don't have
965  any type of initialization.
966  * They are the best tool for iterating components when it is not necessary to
967  know which entities they belong to.
968  * They don't affect any other operations of the registry.
969 
970  Cons:
971 
972  * They can be used to iterate only one type of component at a time.
973  * They don't return the entity to which a component belongs to the caller.
974 
975 * Runtime views:
976 
977  Pros:
978 
979  * Their lists of components are defined at runtime and not at compile-time.
980  * Creating and destroying them isn't expensive at all because they don't have
981  any type of initialization.
982  * They are the best tool for things like plugin systems and mods in general.
983  * They don't affect any other operations of the registry.
984 
985  Cons:
986 
987  * Their performances are definitely lower than those of all the other views,
988  although they are still usable and sufficient for most of the purposes.
989 
990 To sum up and as a rule of thumb:
991 
992 * Use a raw view to iterate components only (no entities) for a given type.
993 * Use a standard view to iterate entities and components for a single type.
994 * Use a standard view to iterate entities and components for multiple types when
995  the number of types is low. Standard views are really optimized and persistent
996  views won't add much in this case.
997 * Use a standard view to iterate entities and components for multiple types when
998  a significantly low number of entities have one of the components.
999 * Use a standard view in all those cases where a persistent view would give a
1000  boost to performance but the iteration isn't performed frequently.
1001 * Prepare and use a persistent view when you want to iterate only entities for
1002  multiple components.
1003 * Prepare and use a persistent view when you want to iterate entities for
1004  multiple components and each component is assigned to a great number of
1005  entities but the intersection between the sets of entities is small.
1006 * Prepare and use a persistent view in all the cases where a standard view
1007  wouldn't fit well otherwise.
1008 * Finally, in case you don't know at compile-time what are the components to
1009  use, choose a runtime view and set them during execution.
1010 
1011 To easily iterate entities and components, all the views offer the common
1012 `begin` and `end` member functions that allow users to use a view in a typical
1013 range-for loop. Almost all the views offer also a *more functional* `each`
1014 member function that accepts a callback for convenience.<br/>
1015 Continue reading for more details or refer to the inline documentation.
1016 
1017 ## Standard View
1018 
1019 A standard view behaves differently if it's constructed for a single component
1020 or if it has been requested to iterate multiple components. Even the API is
1021 different in the two cases.<br/>
1022 All that they share is the way they are created by means of a registry:
1023 
1024 ```cpp
1025 // single component standard view
1026 auto single = registry.view<Position>();
1027 
1028 // multi component standard view
1029 auto multi = registry.view<Position, Velocity>();
1030 ```
1031 
1032 For all that remains, it's worth discussing them separately.<br/>
1033 
1034 ### Single component standard view
1035 
1036 Single component standard views are specialized in order to give a boost in
1037 terms of performance in all the situation. This kind of views can access the
1038 underlying data structures directly and avoid superfluous checks.<br/>
1039 They offer a bunch of functionalities to get the number of entities they are
1040 going to return and a raw access to the entity list as well as to the component
1041 list. It's also possible to ask a view if it contains a given entity.<br/>
1042 Refer to the inline documentation for all the details.
1043 
1044 There is no need to store views around for they are extremely cheap to
1045 construct, even though they can be copied without problems and reused freely. In
1046 fact, they return newly created and correctly initialized iterators whenever
1047 `begin` or `end` are invoked.<br/>
1048 To iterate a single component standard view, either use it in a range-for loop:
1049 
1050 ```cpp
1051 auto view = registry.view<Renderable>();
1052 
1053 for(auto entity: view) {
1054  Renderable &renderable = view.get(entity);
1055 
1056  // ...
1057 }
1058 ```
1059 
1060 Or rely on the `each` member function to iterate entities and get all their
1061 components at once:
1062 
1063 ```cpp
1064 registry.view<Renderable>().each([](auto entity, auto &renderable) {
1065  // ...
1066 });
1067 ```
1068 
1069 The `each` member function is highly optimized. Unless users want to iterate
1070 only entities, using `each` should be the preferred approach.
1071 
1072 **Note**: prefer the `get` member function of a view instead of the `get` member
1073 function template of a registry during iterations, if possible. However, keep in
1074 mind that it works only with the components of the view itself.
1075 
1076 ### Multi component standard view
1077 
1078 Multi component standard views iterate entities that have at least all the given
1079 components in their bags. During construction, these views look at the number of
1080 entities available for each component and pick up a reference to the smallest
1081 set of candidates in order to speed up iterations.<br/>
1082 They offer fewer functionalities than their companion views for single
1083 component. In particular, a multi component standard view exposes utility
1084 functions to get the estimated number of entities it is going to return and to
1085 know whether it's empty or not. It's also possible to ask a view if it contains
1086 a given entity.<br/>
1087 Refer to the inline documentation for all the details.
1088 
1089 There is no need to store views around for they are extremely cheap to
1090 construct, even though they can be copied without problems and reused freely. In
1091 fact, they return newly created and correctly initialized iterators whenever
1092 `begin` or `end` are invoked.<br/>
1093 To iterate a multi component standard view, either use it in a range-for loop:
1094 
1095 ```cpp
1096 auto view = registry.view<Position, Velocity>();
1097 
1098 for(auto entity: view) {
1099  // a component at a time ...
1100  Position &position = view.get<Position>(entity);
1101  Velocity &velocity = view.get<Velocity>(entity);
1102 
1103  // ... or multiple components at once
1104  std::tuple<Position &, Velocity &> tup = view.get<Position, Velocity>(entity);
1105 
1106  // ...
1107 }
1108 ```
1109 
1110 Or rely on the `each` member function to iterate entities and get all their
1111 components at once:
1112 
1113 ```cpp
1114 registry.view<Position, Velocity>().each([](auto entity, auto &position, auto &velocity) {
1115  // ...
1116 });
1117 ```
1118 
1119 The `each` member function is highly optimized. Unless users want to iterate
1120 only entities or get only some of the components, using `each` should be the
1121 preferred approach.
1122 
1123 **Note**: prefer the `get` member function of a view instead of the `get` member
1124 function template of a registry during iterations, if possible. However, keep in
1125 mind that it works only with the components of the view itself.
1126 
1127 ## Persistent View
1128 
1129 A persistent view returns all the entities and only the entities that have at
1130 least the given components. Moreover, it's guaranteed that the entity list is
1131 tightly packed in memory for fast iterations.<br/>
1132 In general, persistent views don't stay true to the order of any set of
1133 components unless users explicitly sort them.
1134 
1135 Persistent views can be used only to iterate multiple components. To create this
1136 kind of views, the tag `persistent_t` must also be used in order to disambiguate
1137 overloads of the `view` member function:
1138 
1139 ```cpp
1140 auto view = registry.view<Position, Velocity>(entt::persistent_t{});
1141 ```
1142 
1143 There is no need to store views around for they are extremely cheap to
1144 construct, even though they can be copied without problems and reused freely. In
1145 fact, they return newly created and correctly initialized iterators whenever
1146 `begin` or `end` are invoked.<br/>
1147 That being said, persistent views perform an initialization step the very first
1148 time they are constructed and this could be quite costly. To avoid it, consider
1149 asking to the registry to _prepare_ them when no entities have been created yet:
1150 
1151 ```cpp
1152 registry.prepare<Position, Velocity>();
1153 ```
1154 
1155 If the registry is empty, preparation is extremely fast. Moreover the `prepare`
1156 member function template is idempotent. Feel free to invoke it even more than
1157 once: if the view has been already prepared before, the function returns
1158 immediately and does nothing.
1159 
1160 A persistent view offers a bunch of functionalities to get the number of
1161 entities it's going to return, a raw access to the entity list and the
1162 possibility to sort the underlying data structures according to the order of one
1163 of the components for which it has been constructed. It's also possible to ask a
1164 view if it contains a given entity.<br/>
1165 Refer to the inline documentation for all the details.
1166 
1167 To iterate a persistent view, either use it in a range-for loop:
1168 
1169 ```cpp
1170 auto view = registry.view<Position, Velocity>(entt::persistent_t{});
1171 
1172 for(auto entity: view) {
1173  // a component at a time ...
1174  Position &position = view.get<Position>(entity);
1175  Velocity &velocity = view.get<Velocity>(entity);
1176 
1177  // ... or multiple components at once
1178  std::tuple<Position &, Velocity &> tup = view.get<Position, Velocity>(entity);
1179 
1180  // ...
1181 }
1182 ```
1183 
1184 Or rely on the `each` member function to iterate entities and get all their
1185 components at once:
1186 
1187 ```cpp
1188 registry.view<Position, Velocity>(entt::persistent_t{}).each([](auto entity, auto &position, auto &velocity) {
1189  // ...
1190 });
1191 ```
1192 
1193 Performance are more or less the same. The best approach depends mainly on
1194 whether all the components have to be accessed or not.
1195 
1196 **Note**: prefer the `get` member function of a view instead of the `get` member
1197 function template of a registry during iterations, if possible. However, keep in
1198 mind that it works only with the components of the view itself.
1199 
1200 ## Raw View
1201 
1202 Raw views return all the components of a given type. This kind of views can
1203 access components directly and avoid extra indirections like when components are
1204 accessed via an entity identifier.<br/>
1205 They offer a bunch of functionalities to get the number of instances they are
1206 going to return and a raw access to the entity list as well as to the component
1207 list.<br/>
1208 Refer to the inline documentation for all the details.
1209 
1210 Raw views can be used only to iterate components for a single type. To create
1211 this kind of views, the tag `raw_t` must also be used in order to disambiguate
1212 overloads of the `view` member function:
1213 
1214 ```cpp
1215 auto view = registry.view<Renderable>(entt::raw_t{});
1216 ```
1217 
1218 There is no need to store views around for they are extremely cheap to
1219 construct, even though they can be copied without problems and reused freely. In
1220 fact, they return newly created and correctly initialized iterators whenever
1221 `begin` or `end` are invoked.<br/>
1222 To iterate a raw view, use it in a range-for loop:
1223 
1224 ```cpp
1225 auto view = registry.view<Renderable>(entt::raw_t{});
1226 
1227 for(auto &&component: raw) {
1228  // ...
1229 }
1230 ```
1231 
1232 Or rely on the `each` member function:
1233 
1234 ```cpp
1235 registry.view<Renderable>(entt::raw_t{}).each([](auto &renderable) {
1236  // ...
1237 });
1238 ```
1239 
1240 Performance are exactly the same in both cases.
1241 
1242 **Note**: raw views don't have a `get` member function for obvious reasons.
1243 
1244 ## Runtime View
1245 
1246 Runtime views iterate entities that have at least all the given components in
1247 their bags. During construction, these views look at the number of entities
1248 available for each component and pick up a reference to the smallest
1249 set of candidates in order to speed up iterations.<br/>
1250 They offer more or less the same functionalities of a multi component standard
1251 view. However, they don't expose a `get` member function and users should refer
1252 to the registry that generated the view to access components. In particular, a
1253 runtime view exposes utility functions to get the estimated number of entities
1254 it is going to return and to know whether it's empty or not. It's also possible
1255 to ask a view if it contains a given entity.<br/>
1256 Refer to the inline documentation for all the details.
1257 
1258 Runtime view are extremely cheap to construct and should not be stored around in
1259 any case. They should be used immediately after creation and then they should be
1260 thrown away. The reasons for this go far beyond the scope of this document.<br/>
1261 To iterate a runtime view, either use it in a range-for loop:
1262 
1263 ```cpp
1264 using component_type = typename decltype(registry)::component_type;
1265 component_type types[] = { registry.type<Position>(), registry.type<Velocity>() };
1266 
1267 auto view = registry.view(std::cbegin(types), std::cend(types));
1268 
1269 for(auto entity: view) {
1270  // a component at a time ...
1271  Position &position = registry.get<Position>(entity);
1272  Velocity &velocity = registry.get<Velocity>(entity);
1273 
1274  // ... or multiple components at once
1275  std::tuple<Position &, Velocity &> tup = view.get<Position, Velocity>(entity);
1276 
1277  // ...
1278 }
1279 ```
1280 
1281 Or rely on the `each` member function to iterate entities:
1282 
1283 ```cpp
1284 using component_type = typename decltype(registry)::component_type;
1285 component_type types[] = { registry.type<Position>(), registry.type<Velocity>() };
1286 
1287 auto view = registry.view(std::cbegin(types), std::cend(types)).each([](auto entity) {
1288  // ...
1289 });
1290 ```
1291 
1292 Performance are exactly the same in both cases.
1293 
1294 **Note**: runtime views are meant for all those cases where users don't know at
1295 compile-time what components to use to iterate entities. This is particularly
1296 well suited to plugin systems and mods in general. Where possible, don't use
1297 runtime views, as their performance are slightly inferior to those of the other
1298 views.
1299 
1300 
1301 ## Give me everything
1302 
1303 Views are narrow windows on the entire list of entities. They work by filtering
1304 entities according to their components.<br/>
1305 In some cases there may be the need to iterate all the entities still in use
1306 regardless of their components. The registry offers a specific member function
1307 to do that:
1308 
1309 ```cpp
1310 registry.each([](auto entity) {
1311  // ...
1312 });
1313 ```
1314 
1315 It returns to the caller all the entities that are still in use by means of the
1316 given function.<br/>
1317 As a rule of thumb, consider using a view if the goal is to iterate entities
1318 that have a determinate set of components. A view is usually much faster than
1319 combining this function with a bunch of custom tests.<br/>
1320 In all the other cases, this is the way to go.
1321 
1322 There exists also another member function to use to retrieve orphans. An orphan
1323 is an entity that is still in use and has neither assigned components nor
1324 tags.<br/>
1325 The signature of the function is the same of `each`:
1326 
1327 ```cpp
1328 registry.orphans([](auto entity) {
1329  // ...
1330 });
1331 ```
1332 
1333 To test the _orphanity_ of a single entity, use the member function `orphan`
1334 instead. It accepts a valid entity identifer as an argument and returns true in
1335 case the entity is an orphan, false otherwise.
1336 
1337 In general, all these functions can result in poor performance.<br/>
1338 `each` is fairly slow because of some checks it performs on each and every
1339 entity. For similar reasons, `orphans` can be even slower. Both functions should
1340 not be used frequently to avoid the risk of a performance hit.
1341 
1342 # Iterations: what is allowed and what is not
1343 
1344 Most of the _ECS_ available out there have some annoying limitations (at least
1345 from my point of view): entities and components cannot be created nor destroyed
1346 during iterations.<br/>
1347 `EnTT` partially solves the problem with a few limitations:
1348 
1349 * Creating entities and components is allowed during iterations.
1350 * Deleting an entity or removing its components is allowed during iterations if
1351  it's the one currently returned by the view. For all the other entities,
1352  destroying them or removing their components isn't allowed and it can result
1353  in undefined behavior.
1354 
1355 Iterators are invalidated and the behavior is undefined if an entity is modified
1356 or destroyed and it's not the one currently returned by the view nor a newly
1357 created one.<br/>
1358 To work around it, possible approaches are:
1359 
1360 * Store aside the entities and the components to be removed and perform the
1361  operations at the end of the iteration.
1362 * Mark entities and components with a proper tag component that indicates they
1363  must be purged, then perform a second iteration to clean them up one by one.
1364 
1365 A notable side effect of this feature is that the number of required allocations
1366 is further reduced in most of the cases.
1367 
1368 # Multithreading
1369 
1370 In general, the entire registry isn't thread safe as it is. Thread safety isn't
1371 something that users should want out of the box for several reasons. Just to
1372 mention one of them: performance.<br/>
1373 Views and consequently the approach adopted by `EnTT` are the great exception to
1374 the rule. It's true that views and thus their iterators aren't thread safe by
1375 themselves. Because of this users shouldn't try to iterate a set of components
1376 and modify the same set concurrently. However:
1377 
1378 * As long as a thread iterates the entities that have the component `X` or
1379  assign and removes that component from a set of entities, another thread can
1380  safely do the same with components `Y` and `Z` and everything will work like a
1381  charm. As a trivial example, users can freely execute the rendering system and
1382  iterate the renderable entities while updating a physic component concurrently
1383  on a separate thread.
1384 
1385 * Similarly, a single set of components can be iterated by multiple threads as
1386  long as the components are neither assigned nor removed in the meantime. In
1387  other words, a hypothetical movement system can start multiple threads, each
1388  of which will access the components that carry information about velocity and
1389  position for its entities.
1390 
1391 This kind of entity-component systems can be used in single threaded
1392 applications as well as along with async stuff or multiple threads. Moreover,
1393 typical thread based models for _ECS_ don't require a fully thread safe registry
1394 to work. Actually, users can reach the goal with the registry as it is while
1395 working with most of the common models.
1396 
1397 Because of the few reasons mentioned above and many others not mentioned, users
1398 are completely responsible for synchronization whether required. On the other
1399 hand, they could get away with it without having to resort to particular
1400 expedients.