Skip to content

Commit 159f3b2

Browse files
committed
Added ECS copy semantics.
1 parent e1a8a6f commit 159f3b2

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

source/ECS/Storage.hpp

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,73 @@ namespace ECS
291291
LOG("[ECS][Archetype] Move assigning {} from {}", (void*)(this), (void*)(&p_other));
292292
return *this;
293293
}
294+
// Copy-construct
295+
Archetype(const Archetype& p_other)
296+
: m_bitset{p_other.m_bitset}
297+
, m_components{p_other.m_components}
298+
, m_is_serialisable{p_other.m_is_serialisable}
299+
, m_entities{p_other.m_entities}
300+
, m_instance_size{p_other.m_instance_size}
301+
, m_next_instance_ID{p_other.m_next_instance_ID}
302+
, m_capacity{p_other.m_capacity}
303+
, m_data{(std::byte*)malloc(m_instance_size * m_capacity)}
304+
{
305+
// Copy construct all the components from p_other into this.
306+
if (p_other.m_data != nullptr)
307+
{
308+
for (ArchetypeInstanceID instance = 0; instance < m_next_instance_ID; instance++)
309+
{
310+
const auto instance_start = m_instance_size * instance;
294311

295-
// No copying archetypes
296-
Archetype(const Archetype& p_other) = delete;
297-
Archetype& operator=(const Archetype& p_other) = delete;
312+
for (const auto& comp : m_components)
313+
{
314+
const auto address_offset = instance_start + comp.offset;
315+
comp.type_info.CopyConstruct(&m_data[address_offset], &p_other.m_data[address_offset]);
316+
}
317+
}
318+
}
319+
320+
LOG("[ECS][Archetype] Copy constructed {} from {}", (void*)(this), (void*)(&p_other));
321+
}
322+
// Copy-assign
323+
Archetype& operator=(const Archetype& p_other)
324+
{
325+
if (this != &p_other)
326+
{
327+
if (m_data != nullptr)
328+
{
329+
clear();
330+
free(m_data);
331+
}
332+
333+
m_bitset = p_other.m_bitset;
334+
m_components = p_other.m_components;
335+
m_is_serialisable = p_other.m_is_serialisable;
336+
m_entities = p_other.m_entities;
337+
m_instance_size = p_other.m_instance_size;
338+
m_next_instance_ID = p_other.m_next_instance_ID;
339+
m_capacity = p_other.m_capacity;
340+
m_data = (std::byte*)malloc(m_instance_size * m_capacity);
341+
342+
// Copy construct all the components from p_other into this.
343+
if (p_other.m_data != nullptr)
344+
{
345+
for (ArchetypeInstanceID instance = 0; instance < m_next_instance_ID; instance++)
346+
{
347+
const auto instance_start = m_instance_size * instance;
348+
349+
for (const auto& comp : m_components)
350+
{
351+
const auto address_offset = instance_start + comp.offset;
352+
comp.type_info.CopyConstruct(&m_data[address_offset], &p_other.m_data[address_offset]);
353+
}
354+
}
355+
}
356+
}
357+
358+
LOG("[ECS][Archetype] Copy assigned {} from {}", (void*)(this), (void*)(&p_other));
359+
return *this;
360+
}
298361

299362
// Search the m_components vector for the p_component_ID and return its ComponentLayout.
300363
// Non-template version (when we know the ComponentID but not the Type).
@@ -450,7 +513,7 @@ namespace ECS
450513
// Size is 0 after clear.
451514
void clear()
452515
{
453-
for (size_t instance = 0; instance < m_next_instance_ID; instance++)
516+
for (ArchetypeInstanceID instance = 0; instance < m_next_instance_ID; instance++)
454517
{
455518
const auto instance_start = m_instance_size * instance;
456519

0 commit comments

Comments
 (0)