From 6e078989da6b1ebffeea7295897a53d2723f1c7b Mon Sep 17 00:00:00 2001 From: mingwugmail Date: Sat, 25 Jun 2022 14:25:04 -0400 Subject: [PATCH] add T dup(T)(T obj) for shallowClone --- source/jdiutil/memory.d | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/source/jdiutil/memory.d b/source/jdiutil/memory.d index 8d8d524..d8a2690 100644 --- a/source/jdiutil/memory.d +++ b/source/jdiutil/memory.d @@ -98,3 +98,22 @@ unittest { main(); } + +/* ========================================================================== *\ +\* ========================================================================== */ +// from: https://forum.dlang.org/post/acafsosotrjdswwuklob@forum.dlang.org +// use the same name as array.dup https://dlang.org/spec/arrays.html +extern (C) Object _d_newclass(TypeInfo_Class ci); + +T dup(T)(T obj) { // shallowClone + if (obj is null) + return null; + ClassInfo ci = obj.classinfo; + size_t start = Object.classinfo.m_init.length; + size_t end = ci.m_init.length; + T clone = cast(T)_d_newclass(ci); + (cast(void*)clone)[start .. end] = (cast(void*)obj)[start .. end]; + return clone; +} + +