Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 70b3e67

Browse files
authored
Merge pull request #2769 from JinShil/move_move
Move __move_post_blt from object.d to core.internal.moving.d merged-on-behalf-of: Petar Kirov <ZombineDev@users.noreply.github.com>
2 parents 873fda8 + 8e06c4b commit 70b3e67

File tree

7 files changed

+101
-79
lines changed

7 files changed

+101
-79
lines changed

mak/COPY

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ COPY=\
2828
$(IMPDIR)\core\internal\dassert.d \
2929
$(IMPDIR)\core\internal\entrypoint.d \
3030
$(IMPDIR)\core\internal\hash.d \
31+
$(IMPDIR)\core\internal\moving.d \
3132
$(IMPDIR)\core\internal\parseoptions.d \
3233
$(IMPDIR)\core\internal\spinlock.d \
3334
$(IMPDIR)\core\internal\string.d \

mak/DOCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ DOCS=\
7676
$(DOCDIR)\core_sys_darwin_netinet_in_.html \
7777
\
7878
$(DOCDIR)\core_internal_dassert.html \
79+
$(DOCDIR)\core_internal_moving.html \
7980
$(DOCDIR)\core_internal_switch_.html \
8081
\
8182
$(DOCDIR)\core_internal_array_appending.html \

mak/SRCS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SRCS=\
2727
src\core\internal\dassert.d \
2828
src\core\internal\entrypoint.d \
2929
src\core\internal\hash.d \
30+
src\core\internal\moving.d \
3031
src\core\internal\parseoptions.d \
3132
src\core\internal\spinlock.d \
3233
src\core\internal\string.d \

mak/WINDOWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ $(IMPDIR)\core\internal\entrypoint.d : src\core\internal\entrypoint.d
138138
$(IMPDIR)\core\internal\hash.d : src\core\internal\hash.d
139139
copy $** $@
140140

141+
$(IMPDIR)\core\internal\moving.d : src\core\internal\moving.d
142+
copy $** $@
143+
141144
$(IMPDIR)\core\internal\parseoptions.d : src\core\internal\parseoptions.d
142145
copy $** $@
143146

posix.mak

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ $(DOCDIR)/core_sys_darwin_netinet_%.html : src/core/sys/darwin/netinet/%.d $(DMD
177177
$(DOCDIR)/core_internal_dassert.html : src/core/internal/dassert.d $(DMD)
178178
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
179179

180+
$(DOCDIR)/core_internal_moving.html : src/core/internal/moving.d $(DMD)
181+
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
182+
180183
$(DOCDIR)/core_internal_switch_.html : src/core/internal/switch_.d $(DMD)
181184
$(DMD) $(DDOCFLAGS) -Df$@ project.ddoc $(DOCFMT) $<
182185

src/core/internal/moving.d

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
This module contains the implementation of move semantics of DIP 1014
3+
4+
Copyright: Copyright Digital Mars 2000 - 2019.
5+
License: Distributed under the
6+
$(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
(See accompanying file LICENSE)
8+
Source: $(DRUNTIMESRC core/_internal/_moving.d)
9+
*/
10+
module core.internal.moving;
11+
12+
/**
13+
Recursively calls the `opPostMove` callbacks of a struct and its members if
14+
they're defined.
15+
16+
When moving a struct instance, the compiler emits a call to this function
17+
after blitting the instance and before releasing the original instance's
18+
memory.
19+
20+
Params:
21+
newLocation = reference to struct instance being moved into
22+
oldLocation = reference to the original instance
23+
24+
Note:
25+
This function is tentatively defined as `nothrow` to prevent
26+
`opPostMove` from being defined without `nothrow`, which would allow
27+
for possibly confusing changes in program flow.
28+
*/
29+
void __move_post_blt(S)(ref S newLocation, ref S oldLocation) nothrow
30+
if (is(S == struct))
31+
{
32+
static foreach (memberName; __traits(allMembers, S))
33+
{
34+
static if (is(typeof(__traits(getMember, S, memberName)) == struct))
35+
{
36+
__move_post_blt(__traits(getMember, newLocation, memberName), __traits(getMember, oldLocation, memberName));
37+
}
38+
}
39+
40+
static if (__traits(hasMember, S, "opPostMove"))
41+
{
42+
import core.internal.traits : lvalueOf, rvalueOf;
43+
static assert( is(typeof(S.init.opPostMove(lvalueOf!S))) &&
44+
!is(typeof(S.init.opPostMove(rvalueOf!S))),
45+
"`" ~ S.stringof ~ ".opPostMove` must take exactly one argument of type `" ~ S.stringof ~ "` by reference");
46+
47+
newLocation.opPostMove(oldLocation);
48+
}
49+
}
50+
51+
@safe nothrow unittest
52+
{
53+
struct A
54+
{
55+
bool movedInto;
56+
void opPostMove(const ref A oldLocation)
57+
{
58+
movedInto = true;
59+
}
60+
}
61+
A src, dest;
62+
__move_post_blt(dest, src);
63+
assert(dest.movedInto);
64+
}
65+
66+
@safe nothrow unittest
67+
{
68+
struct A
69+
{
70+
bool movedInto;
71+
void opPostMove(const ref A oldLocation)
72+
{
73+
movedInto = true;
74+
}
75+
}
76+
struct B
77+
{
78+
A a;
79+
80+
bool movedInto;
81+
void opPostMove(const ref B oldLocation)
82+
{
83+
movedInto = true;
84+
}
85+
}
86+
B src, dest;
87+
__move_post_blt(dest, src);
88+
assert(dest.movedInto && dest.a.movedInto);
89+
}

src/object.d

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -45,85 +45,6 @@ public import core.internal.array.capacity: reserve;
4545
/// See $(REF assumeSafeAppend, core,internal,array,capacity)
4646
public import core.internal.array.capacity: assumeSafeAppend;
4747

48-
/**
49-
* Recursively calls the `opPostMove` callbacks of a struct and its members if
50-
* they're defined.
51-
*
52-
* When moving a struct instance, the compiler emits a call to this function
53-
* after blitting the instance and before releasing the original instance's
54-
* memory.
55-
*
56-
* Params:
57-
* newLocation = reference to struct instance being moved into
58-
* oldLocation = reference to the original instance
59-
*
60-
* Note:
61-
* This function is tentatively defined as `nothrow` to prevent
62-
* `opPostMove` from being defined without `nothrow`, which would allow
63-
* for possibly confusing changes in program flow.
64-
*/
65-
void __move_post_blt(S)(ref S newLocation, ref S oldLocation) nothrow
66-
if (is(S == struct))
67-
{
68-
static foreach (memberName; __traits(allMembers, S))
69-
{
70-
static if (is(typeof(__traits(getMember, S, memberName)) == struct))
71-
{
72-
__move_post_blt(__traits(getMember, newLocation, memberName), __traits(getMember, oldLocation, memberName));
73-
}
74-
}
75-
76-
static if (__traits(hasMember, S, "opPostMove"))
77-
{
78-
import core.internal.traits : lvalueOf, rvalueOf;
79-
static assert( is(typeof(S.init.opPostMove(lvalueOf!S))) &&
80-
!is(typeof(S.init.opPostMove(rvalueOf!S))),
81-
"`" ~ S.stringof ~ ".opPostMove` must take exactly one argument of type `" ~ S.stringof ~ "` by reference");
82-
83-
newLocation.opPostMove(oldLocation);
84-
}
85-
}
86-
87-
@safe nothrow unittest
88-
{
89-
struct A
90-
{
91-
bool movedInto;
92-
void opPostMove(const ref A oldLocation)
93-
{
94-
movedInto = true;
95-
}
96-
}
97-
A src, dest;
98-
__move_post_blt(dest, src);
99-
assert(dest.movedInto);
100-
}
101-
102-
@safe nothrow unittest
103-
{
104-
struct A
105-
{
106-
bool movedInto;
107-
void opPostMove(const ref A oldLocation)
108-
{
109-
movedInto = true;
110-
}
111-
}
112-
struct B
113-
{
114-
A a;
115-
116-
bool movedInto;
117-
void opPostMove(const ref B oldLocation)
118-
{
119-
movedInto = true;
120-
}
121-
}
122-
B src, dest;
123-
__move_post_blt(dest, src);
124-
assert(dest.movedInto && dest.a.movedInto);
125-
}
126-
12748
/**
12849
Destroys the given object and optionally resets to initial state. It's used to
12950
_destroy an object, calling its destructor or finalizer so it no longer
@@ -4101,6 +4022,9 @@ public import core.internal.array.capacity: _d_arraysetlengthTImpl;
41014022
/// See $(REF _d_assert_fail, core,internal,dassert)
41024023
public import core.internal.dassert: _d_assert_fail;
41034024

4025+
/// See $(REF __move_post_blt, core,internal,moving)
4026+
public import core.internal.moving: __move_post_blt;
4027+
41044028
/// See $(REF __switch, core,internal,switch_)
41054029
public import core.internal.switch_: __switch;
41064030
/// See $(REF __switch_error, core,internal,switch_)

0 commit comments

Comments
 (0)