This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 422
Add std::unique_ptr #2723
Merged
Merged
Add std::unique_ptr #2723
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Added `core.stdcpp.memory.unique_ptr` | ||
|
|
||
| Added `core.stdcpp.memory.unique_ptr`, which corresponds to C++ `std::unique_ptr`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** | ||
| * D binding to C++ <memory>. | ||
| * | ||
| * Copyright: Copyright (c) 2019 D Language Foundation | ||
| * License: Distributed under the | ||
| * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0). | ||
| * (See accompanying file LICENSE) | ||
| * Authors: Manu Evans | ||
| * Source: $(DRUNTIMESRC core/stdcpp/memory.d) | ||
| */ | ||
|
|
||
| module core.stdcpp.memory; | ||
|
|
||
| public import core.stdcpp.allocator; | ||
|
|
||
| import core.stdcpp.xutility : StdNamespace; | ||
|
|
||
| extern(C++, (StdNamespace)): | ||
|
|
||
| /// | ||
| unique_ptr!T make_unique(T, Args...)(auto ref Args args) | ||
| { | ||
| import core.lifetime : forward; | ||
| import core.stdcpp.new_ : cpp_new; | ||
|
|
||
| return unique_ptr!T(cpp_new!T(forward!args)); | ||
| } | ||
|
|
||
| /// | ||
| struct default_delete(T) | ||
| { | ||
| /// | ||
| alias pointer = ClassOrPtr!T; | ||
|
|
||
| /// | ||
| void opCall()(pointer ptr) const | ||
| { | ||
| import core.stdcpp.new_ : cpp_delete; | ||
|
|
||
| cpp_delete(ptr); | ||
| } | ||
| } | ||
|
|
||
| /// | ||
| extern(C++, class) | ||
| struct unique_ptr(T, Deleter = default_delete!T) | ||
| { | ||
| extern(D): | ||
| /// | ||
| this(this) @disable; | ||
|
|
||
| /// | ||
| ~this() | ||
| { | ||
| reset(); | ||
| } | ||
|
|
||
| /// | ||
| ref unique_ptr opAssign(typeof(null)) | ||
| { | ||
| reset(); | ||
| return this; | ||
| } | ||
|
|
||
| /// | ||
| void reset(pointer p = null) | ||
| { | ||
| pointer t = __ptr(); | ||
| __ptr() = p; | ||
| if (t) | ||
| get_deleter()(t); | ||
| } | ||
|
|
||
| nothrow pure @safe @nogc: | ||
| /// | ||
| alias pointer = ClassOrPtr!T; | ||
| /// | ||
| alias element_type = T; | ||
| /// | ||
| alias deleter_type = Deleter; | ||
|
|
||
| /// | ||
| this(pointer ptr) | ||
| { | ||
| __ptr() = ptr; | ||
| } | ||
|
|
||
| /// | ||
| inout(pointer) get() inout nothrow | ||
| { | ||
| return __ptr(); | ||
| } | ||
|
|
||
| /// | ||
| bool opCast(T : bool)() const nothrow | ||
| { | ||
| return __ptr() != null; | ||
| } | ||
|
|
||
| /// | ||
| pointer release() nothrow | ||
| { | ||
| pointer t = __ptr(); | ||
| __ptr() = null; | ||
| return t; | ||
| } | ||
|
|
||
| // void swap(ref unique_ptr u) nothrow | ||
| // { | ||
| // __ptr_.swap(__u.__ptr_); | ||
| // } | ||
|
|
||
| version (CppRuntime_Microsoft) | ||
| { | ||
| /// | ||
| ref inout(deleter_type) get_deleter() inout nothrow { return _Mypair._Myval1; } | ||
|
|
||
| private: | ||
| import core.stdcpp.xutility : _Compressed_pair; | ||
|
|
||
| ref pointer __ptr() nothrow { return _Mypair._Myval2; } | ||
| inout(pointer) __ptr() inout nothrow { return _Mypair._Myval2; } | ||
|
|
||
| _Compressed_pair!(Deleter, pointer) _Mypair; | ||
| } | ||
| else version (CppRuntime_Gcc) | ||
| { | ||
| /// | ||
| ref inout(deleter_type) get_deleter() inout nothrow { return _M_t.get!1; } | ||
|
|
||
| private: | ||
| import core.stdcpp.tuple : tuple, get; | ||
|
|
||
| ref pointer __ptr() nothrow { return _M_t.get!0; } | ||
| inout(pointer) __ptr() inout nothrow { return _M_t.get!0; } | ||
|
|
||
| tuple!(pointer, Deleter) _M_t; | ||
| } | ||
| else version (CppRuntime_Clang) | ||
| { | ||
| /// | ||
| ref inout(deleter_type) get_deleter() inout nothrow { return __ptr_.second; } | ||
|
|
||
| private: | ||
| import core.stdcpp.xutility : __compressed_pair; | ||
|
|
||
| ref pointer __ptr() nothrow { return __ptr_.first; } | ||
| inout(pointer) __ptr() inout nothrow { return __ptr_.first; } | ||
|
|
||
| __compressed_pair!(pointer, deleter_type) __ptr_; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private: | ||
|
|
||
| template ClassOrPtr(T) | ||
| { | ||
| static if (is(T == class)) | ||
| alias ClassOrPtr = T; | ||
| else | ||
| alias ClassOrPtr = T*; | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #include <memory> | ||
|
|
||
| std::unique_ptr<int> passThrough(std::unique_ptr<int> x) | ||
| { | ||
| return std::move(x); | ||
| } | ||
| std::unique_ptr<int> changeIt(std::unique_ptr<int> x) | ||
| { | ||
| auto p = new int(20); // ensure new pointer is different from released pointer | ||
| x.reset(); | ||
| return std::unique_ptr<int>(p); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import core.stdcpp.memory; | ||
|
|
||
| unittest | ||
| { | ||
| import core.lifetime : move; | ||
|
|
||
| unique_ptr!int up; | ||
| assert(!up); | ||
| assert(up.get == null); | ||
|
|
||
| auto up2 = make_unique!int(10); | ||
| assert(!!up2); | ||
| assert(up2.get != null); | ||
| assert(*up2.get == 10); | ||
|
|
||
| static assert(!__traits(compiles, up = up2)); | ||
| static assert(!__traits(compiles, unique_ptr!int(up))); | ||
|
|
||
| unique_ptr!int x = up2.move; | ||
| assert(!!x && !up2); | ||
|
|
||
| up = x.move; | ||
| assert(!!up && !x); | ||
|
|
||
| int* p = up.get; | ||
| up = passThrough(up.move); | ||
| assert(up.get == p); | ||
| up = changeIt(up.move); | ||
| assert(up.get != p); | ||
| assert(*up.get == 20); | ||
|
|
||
| up.reset(); | ||
| assert(!up); | ||
| } | ||
|
|
||
| extern(C++): | ||
|
|
||
| unique_ptr!int passThrough(unique_ptr!int x); | ||
| unique_ptr!int changeIt(unique_ptr!int x); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems malloc returns the same pointer that was just freed, this the reason the ci fails on osx. mybe check the value instead
assert(*up.get == 20);?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I've added a commit to ensure the pointers in changeIt are different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect. It's working now.