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
a21a2975
Unverified
Commit
a21a2975
authored
Feb 01, 2018
by
Kenton Varda
Committed by
GitHub
Feb 01, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #624 from capnproto/array-attach
Add Array::attach() and ArrayPtr::attach().
parents
68a9815f
27b33623
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
175 additions
and
2 deletions
+175
-2
array-test.c++
c++/src/kj/array-test.c++
+115
-0
array.h
c++/src/kj/array.h
+49
-1
common.h
c++/src/kj/common.h
+10
-0
memory.h
c++/src/kj/memory.h
+1
-1
No files found.
c++/src/kj/array-test.c++
View file @
a21a2975
...
...
@@ -385,5 +385,120 @@ KJ_TEST("kj::arr()") {
}
#endif
struct
DestructionOrderRecorder
{
DestructionOrderRecorder
(
uint
&
counter
,
uint
&
recordTo
)
:
counter
(
counter
),
recordTo
(
recordTo
)
{}
~
DestructionOrderRecorder
()
{
recordTo
=
++
counter
;
}
uint
&
counter
;
uint
&
recordTo
;
};
TEST
(
Array
,
Attach
)
{
uint
counter
=
0
;
uint
destroyed1
=
0
;
uint
destroyed2
=
0
;
uint
destroyed3
=
0
;
auto
obj1
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed1
);
auto
obj2
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed2
);
auto
obj3
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed3
);
auto
builder
=
kj
::
heapArrayBuilder
<
Own
<
DestructionOrderRecorder
>>
(
1
);
builder
.
add
(
kj
::
mv
(
obj1
));
auto
arr
=
builder
.
finish
();
auto
ptr
=
arr
.
begin
();
Array
<
Own
<
DestructionOrderRecorder
>>
combined
=
arr
.
attach
(
kj
::
mv
(
obj2
),
kj
::
mv
(
obj3
));
KJ_EXPECT
(
combined
.
begin
()
==
ptr
);
KJ_EXPECT
(
obj1
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj2
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj3
.
get
()
==
nullptr
);
KJ_EXPECT
(
destroyed1
==
0
);
KJ_EXPECT
(
destroyed2
==
0
);
KJ_EXPECT
(
destroyed3
==
0
);
combined
=
nullptr
;
KJ_EXPECT
(
destroyed1
==
1
,
destroyed1
);
KJ_EXPECT
(
destroyed2
==
2
,
destroyed2
);
KJ_EXPECT
(
destroyed3
==
3
,
destroyed3
);
}
TEST
(
Array
,
AttachNested
)
{
uint
counter
=
0
;
uint
destroyed1
=
0
;
uint
destroyed2
=
0
;
uint
destroyed3
=
0
;
auto
obj1
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed1
);
auto
obj2
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed2
);
auto
obj3
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed3
);
auto
builder
=
kj
::
heapArrayBuilder
<
Own
<
DestructionOrderRecorder
>>
(
1
);
builder
.
add
(
kj
::
mv
(
obj1
));
auto
arr
=
builder
.
finish
();
auto
ptr
=
arr
.
begin
();
Array
<
Own
<
DestructionOrderRecorder
>>
combined
=
arr
.
attach
(
kj
::
mv
(
obj2
)).
attach
(
kj
::
mv
(
obj3
));
KJ_EXPECT
(
combined
.
begin
()
==
ptr
);
KJ_EXPECT
(
obj1
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj2
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj3
.
get
()
==
nullptr
);
KJ_EXPECT
(
destroyed1
==
0
);
KJ_EXPECT
(
destroyed2
==
0
);
KJ_EXPECT
(
destroyed3
==
0
);
combined
=
nullptr
;
KJ_EXPECT
(
destroyed1
==
1
,
destroyed1
);
KJ_EXPECT
(
destroyed2
==
2
,
destroyed2
);
KJ_EXPECT
(
destroyed3
==
3
,
destroyed3
);
}
TEST
(
Array
,
AttachFromArrayPtr
)
{
uint
counter
=
0
;
uint
destroyed1
=
0
;
uint
destroyed2
=
0
;
uint
destroyed3
=
0
;
auto
obj1
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed1
);
auto
obj2
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed2
);
auto
obj3
=
kj
::
heap
<
DestructionOrderRecorder
>
(
counter
,
destroyed3
);
auto
builder
=
kj
::
heapArrayBuilder
<
Own
<
DestructionOrderRecorder
>>
(
1
);
builder
.
add
(
kj
::
mv
(
obj1
));
auto
arr
=
builder
.
finish
();
auto
ptr
=
arr
.
begin
();
Array
<
Own
<
DestructionOrderRecorder
>>
combined
=
arr
.
asPtr
().
attach
(
kj
::
mv
(
obj2
)).
attach
(
kj
::
mv
(
obj3
));
KJ_EXPECT
(
arr
!=
nullptr
);
KJ_EXPECT
(
combined
.
begin
()
==
ptr
);
KJ_EXPECT
(
obj1
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj2
.
get
()
==
nullptr
);
KJ_EXPECT
(
obj3
.
get
()
==
nullptr
);
KJ_EXPECT
(
destroyed1
==
0
);
KJ_EXPECT
(
destroyed2
==
0
);
KJ_EXPECT
(
destroyed3
==
0
);
combined
=
nullptr
;
KJ_EXPECT
(
destroyed2
==
1
,
destroyed2
);
KJ_EXPECT
(
destroyed3
==
2
,
destroyed3
);
arr
=
nullptr
;
KJ_EXPECT
(
destroyed1
==
3
,
destroyed1
);
}
}
// namespace
}
// namespace kj
c++/src/kj/array.h
View file @
a21a2975
...
...
@@ -25,7 +25,7 @@
#pragma GCC system_header
#endif
#include "
common
.h"
#include "
memory
.h"
#include <string.h>
#include <initializer_list>
...
...
@@ -234,6 +234,10 @@ public:
return
*
this
;
}
template
<
typename
...
Attachments
>
Array
<
T
>
attach
(
Attachments
&&
...
attachments
)
KJ_WARN_UNUSED_RESULT
;
// Like Own<T>::attach(), but attaches to an Array.
private
:
T
*
ptr
;
size_t
size_
;
...
...
@@ -834,4 +838,48 @@ inline Array<Decay<T>> arr(T&& param1, Params&&... params) {
}
#endif
namespace
_
{
// private
template
<
typename
...
T
>
struct
ArrayDisposableOwnedBundle
final
:
public
ArrayDisposer
,
public
OwnedBundle
<
T
...
>
{
ArrayDisposableOwnedBundle
(
T
&&
...
values
)
:
OwnedBundle
<
T
...
>
(
kj
::
fwd
<
T
>
(
values
)...)
{}
void
disposeImpl
(
void
*
,
size_t
,
size_t
,
size_t
,
void
(
*
)(
void
*
))
const
override
{
delete
this
;
}
};
}
// namespace _ (private)
template
<
typename
T
>
template
<
typename
...
Attachments
>
Array
<
T
>
Array
<
T
>::
attach
(
Attachments
&&
...
attachments
)
{
T
*
ptrCopy
=
ptr
;
KJ_IREQUIRE
(
ptrCopy
!=
nullptr
,
"cannot attach to null pointer"
);
// HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to
// accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the
// disposer will still be called when the pointer goes out of scope.
if
(
ptrCopy
==
nullptr
)
ptrCopy
=
reinterpret_cast
<
T
*>
(
1
);
auto
bundle
=
new
_
::
ArrayDisposableOwnedBundle
<
Array
<
T
>
,
Attachments
...
>
(
kj
::
mv
(
*
this
),
kj
::
fwd
<
Attachments
>
(
attachments
)...);
return
Array
<
T
>
(
ptrCopy
,
size_
,
*
bundle
);
}
template
<
typename
T
>
template
<
typename
...
Attachments
>
Array
<
T
>
ArrayPtr
<
T
>::
attach
(
Attachments
&&
...
attachments
)
const
{
T
*
ptrCopy
=
ptr
;
KJ_IREQUIRE
(
ptrCopy
!=
nullptr
,
"cannot attach to null pointer"
);
// HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to
// accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the
// disposer will still be called when the pointer goes out of scope.
if
(
ptrCopy
==
nullptr
)
ptrCopy
=
reinterpret_cast
<
T
*>
(
1
);
auto
bundle
=
new
_
::
ArrayDisposableOwnedBundle
<
Attachments
...
>
(
kj
::
fwd
<
Attachments
>
(
attachments
)...);
return
Array
<
T
>
(
ptrCopy
,
size_
,
*
bundle
);
}
}
// namespace kj
c++/src/kj/common.h
View file @
a21a2975
...
...
@@ -1247,6 +1247,9 @@ private:
//
// So common that we put it in common.h rather than array.h.
template
<
typename
T
>
class
Array
;
template
<
typename
T
>
class
ArrayPtr
:
public
DisallowConstCopyIfNotConst
<
T
>
{
// A pointer to an array. Includes a size. Like any pointer, it doesn't own the target data,
...
...
@@ -1356,6 +1359,13 @@ public:
template
<
typename
U
>
inline
bool
operator
!=
(
const
ArrayPtr
<
U
>&
other
)
const
{
return
!
(
*
this
==
other
);
}
template
<
typename
...
Attachments
>
Array
<
T
>
attach
(
Attachments
&&
...
attachments
)
const
KJ_WARN_UNUSED_RESULT
;
// Like Array<T>::attach(), but also promotes an ArrayPtr to an Array. Generally the attachment
// should be an object that actually owns the array that the ArrayPtr is pointing at.
//
// You must include kj/array.h to call this.
private
:
T
*
ptr
;
size_t
size_
;
...
...
c++/src/kj/memory.h
View file @
a21a2975
...
...
@@ -197,7 +197,7 @@ public:
}
template
<
typename
...
Attachments
>
Own
<
T
>
attach
(
Attachments
&&
...
attachments
);
Own
<
T
>
attach
(
Attachments
&&
...
attachments
)
KJ_WARN_UNUSED_RESULT
;
// Returns an Own<T> which points to the same object but which also ensures that all values
// passed to `attachments` remain alive until after this object is destroyed. Normally
// `attachments` are other Own<?>s pointing to objects that this one depends on.
...
...
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