1 # Push EnTT across boundaries
8 * [Introduction](#introduction)
9 * [Named types and traits class](#named-types-and-traits-class)
10 * [Do not mix types](#do-not-mix-types)
11 * [Macros, macros everywhere](#macros-macros-everywhere)
12 * [Conflicts](#conflicts)
13 * [Allocations: the dark side of the force](#allocations-the-dark-side-of-the-force)
15 @endcond TURN_OFF_DOXYGEN
20 `EnTT` has historically had a limit when used across boundaries on Windows in
21 general and on GNU/Linux when default visibility was set to _hidden_. The
22 limitation is due mainly to a custom utility used to assign unique, sequential
23 identifiers to different types. Unfortunately, this tool is used by several core
24 classes (the `registry` among the others) that are thus almost unusable across
26 The reasons for that are beyond the purposes of this document. However, the good
27 news is that `EnTT` also offers now a way to overcome this limit and to push
28 things across boundaries without problems when needed.
30 # Named types and traits class
32 To allow a type to work properly across boundaries when used by a class that
33 requires to assign unique identifiers to types, users must specialize a class
34 template to literally give a compile-time name to the type itself.<br/>
35 The name of the class template is `name_type_traits` and the specialization must
36 be such that it exposes a static constexpr data member named `value` having type
37 either `ENTT_ID_TYPE` or `entt::hashed_string::hash_type`. Its value is the user
38 defined unique identifier assigned to the specific type.<br/>
39 Identifiers are not to be sequentially generated in this case.
44 struct my_type { /* ... */ };
47 struct entt::named_type_traits<my_type> {
48 static constexpr auto value = "my_type"_hs;
52 Because of the rules of the language, the specialization must reside in the
53 global namespace or in the `entt` namespace. There is no way to change this rule
54 unfortunately, because it doesn't depend on the library itself.
56 The good aspect of this approach is that it's not intrusive at all. The other
57 way around was in fact forcing users to inherit all their classes from a common
58 base. Something to avoid, at least from my point of view.<br/>
59 However, despite the fact that it's not intrusive, it would be great if it was
60 also easier to use and a bit less error-prone. This is why a bunch of macros
61 exist to ease defining named types.
65 Someone might think that this trick is valid only for the types to push across
66 boundaries. This isn't how things work. In fact, the problem is more complex
68 As a rule of thumb, users should never mix named and non-named types. Whenever
69 a type is given a name, all the types must be given a name. As an example,
70 consider the `registry` class template: in case it is pushed across boundaries,
71 all the types of components should be assigned a name to avoid subtle bugs.
73 Indeed, this constraint can be relaxed in many cases. However, it is difficult
74 to define a general rule to follow that is not the most stringent, unless users
75 know exactly what they are doing. Therefore, I won't elaborate on giving further
78 # Macros, macros everywhere
80 The library comes with a set of predefined macros to use to declare named types
81 or export already existing ones. In particular:
83 * `ENTT_NAMED_TYPE` can be used to assign a name to already existing types. This
84 macro must be used in the global namespace even when the types to be named are
88 ENTT_NAMED_TYPE(my_type)
89 ENTT_NAMED_TYPE(ns::another_type)
92 * `ENTT_NAMED_STRUCT` can be used to define and export a struct at the same
93 time. It accepts also an optional namespace in which to define the given type.
94 This macro must be used in the global namespace.
97 ENTT_NAMED_STRUCT(my_type, { /* struct definition */})
98 ENTT_NAMED_STRUCT(ns, another_type, { /* struct definition */})
101 * `ENTT_NAMED_CLASS` can be used to define and export a class at the same
102 time. It accepts also an optional namespace in which to define the given type.
103 This macro must be used in the global namespace.
106 ENTT_NAMED_CLASS(my_type, { /* class definition */})
107 ENTT_NAMED_CLASS(ns, another_type, { /* class definition */})
110 Nested namespaces are supported out of the box as well in all cases. As an
114 ENTT_NAMED_STRUCT(nested::ns, my_type, { /* struct definition */})
117 These macros can be used to avoid specializing the `named_type_traits` class
118 template. In all cases, the name of the class is used also as a seed to generate
119 the compile-time unique identifier.
123 When using macros, unique identifiers are 32/64 bit integers generated by
124 hashing strings during compilation. Therefore, conflicts are rare but still
125 possible. In case of conflicts, everything simply will get broken at runtime and
126 the strangest things will probably take place.<br/>
127 Unfortunately, there is no safe way to prevent it. If this happens, it will be
128 enough to give a different value to one of the conflicting types to solve the
129 problem. To do this, users can either assign a different name to the class or
130 directly define a specialization for the `named_type_traits` class template.
132 # Allocations: the dark side of the force
134 As long as `EnTT` won't support custom allocators, another problem with
135 allocations will remain alive instead. This is in fact easily solved, or at
136 least it is if one knows it.
138 To allow users to add types dynamically, the library makes extensive use of type
139 erasure techniques and dynamic allocations for pools (whether they are for
140 components, events or anything else). The problem occurs when, for example, a
141 registry is created on one side of a boundary and a pool is dynamically created
142 on the other side. In the best case, everything will crash at the exit, while at
143 worst it will do so at runtime.<br/>
144 To avoid problems, the pools must be generated from the same side of the
145 boundary where the object that owns them is also created. As an example, when
146 the registry is created in the main executable and used across boundaries for a
147 given type of component, the pool for that type must be created before passing
148 around the registry itself. To do this is fortunately quite easy, since it is
149 sufficient to invoke any of the methods that involve the given type (continuing
150 the example with the registry, a call to `reserve` or `size` is more than
153 Maybe one day some dedicated methods will be added that do nothing but create a
154 pool for a given type. Until now it has been preferred to keep the API cleaner
155 as they are not strictly necessary.