Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
ec59b987
Commit
ec59b987
authored
Dec 14, 2017
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test `Own<const void>`, fix bug.
parent
479f0712
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
1 deletion
+54
-1
memory-test.c++
c++/src/kj/memory-test.c++
+53
-0
memory.h
c++/src/kj/memory.h
+1
-1
No files found.
c++/src/kj/memory-test.c++
View file @
ec59b987
...
@@ -228,6 +228,59 @@ TEST(Memory, OwnVoid) {
...
@@ -228,6 +228,59 @@ TEST(Memory, OwnVoid) {
}
}
}
}
TEST
(
Memory
,
OwnConstVoid
)
{
{
Own
<
StaticType
>
ptr
=
heap
<
StaticType
>
({
123
});
StaticType
*
addr
=
ptr
.
get
();
Own
<
const
void
>
voidPtr
=
kj
::
mv
(
ptr
);
KJ_EXPECT
(
voidPtr
.
get
()
==
implicitCast
<
void
*>
(
addr
));
}
{
Own
<
DynamicType1
>
ptr
=
heap
<
DynamicType1
>
(
123
);
DynamicType1
*
addr
=
ptr
.
get
();
Own
<
const
void
>
voidPtr
=
kj
::
mv
(
ptr
);
KJ_EXPECT
(
voidPtr
.
get
()
==
implicitCast
<
void
*>
(
addr
));
}
{
bool
destructorCalled
=
false
;
Own
<
DerivedDynamic
>
ptr
=
heap
<
DerivedDynamic
>
(
123
,
456
,
destructorCalled
);
DerivedDynamic
*
addr
=
ptr
.
get
();
Own
<
const
void
>
voidPtr
=
kj
::
mv
(
ptr
);
KJ_EXPECT
(
voidPtr
.
get
()
==
implicitCast
<
void
*>
(
addr
));
KJ_EXPECT
(
!
destructorCalled
);
voidPtr
=
nullptr
;
KJ_EXPECT
(
destructorCalled
);
}
{
bool
destructorCalled
=
false
;
Own
<
DerivedDynamic
>
ptr
=
heap
<
DerivedDynamic
>
(
123
,
456
,
destructorCalled
);
DerivedDynamic
*
addr
=
ptr
.
get
();
Own
<
DynamicType2
>
basePtr
=
kj
::
mv
(
ptr
);
DynamicType2
*
baseAddr
=
basePtr
.
get
();
// On most (all?) C++ ABIs, the second base class in a multiply-inherited class is offset from
// the beginning of the object (assuming the first base class has non-zero size). We use this
// fact here to verify that then casting to Own<void> does in fact result in a pointer that
// points to the start of the overall object, not the base class. We expect that the pointers
// are different here to prove that the test below is non-trivial.
//
// If there is some other ABI where these pointers are the same, and thus this expectation
// fails, then it's no problem to #ifdef out the expectation on that platform.
KJ_EXPECT
(
static_cast
<
void
*>
(
baseAddr
)
!=
static_cast
<
void
*>
(
addr
));
Own
<
const
void
>
voidPtr
=
kj
::
mv
(
basePtr
);
KJ_EXPECT
(
voidPtr
.
get
()
==
static_cast
<
void
*>
(
addr
));
KJ_EXPECT
(
!
destructorCalled
);
voidPtr
=
nullptr
;
KJ_EXPECT
(
destructorCalled
);
}
}
// TODO(test): More tests.
// TODO(test): More tests.
}
// namespace
}
// namespace
...
...
c++/src/kj/memory.h
View file @
ec59b987
...
@@ -73,7 +73,7 @@ void* castToVoid(T* ptr) {
...
@@ -73,7 +73,7 @@ void* castToVoid(T* ptr) {
}
}
template
<
typename
T
>
template
<
typename
T
>
void
*
castToConstVoid
(
T
*
ptr
)
{
const
void
*
castToConstVoid
(
T
*
ptr
)
{
return
CastToVoid_
<
T
>::
applyConst
(
ptr
);
return
CastToVoid_
<
T
>::
applyConst
(
ptr
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment