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
8ebaa614
Commit
8ebaa614
authored
Nov 17, 2014
by
Kenton Varda
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #144 from scottpurdy/std-compat
Adds kj::std as header-only compat layer
parents
01c05f5e
7868bf9f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
200 additions
and
0 deletions
+200
-0
CONTRIBUTORS
CONTRIBUTORS
+1
-0
Makefile.am
c++/Makefile.am
+5
-0
CMakeLists.txt
c++/src/kj/CMakeLists.txt
+5
-0
iostream-test.c++
c++/src/kj/std/iostream-test.c++
+105
-0
iostream.h
c++/src/kj/std/iostream.h
+84
-0
No files found.
CONTRIBUTORS
View file @
8ebaa614
...
...
@@ -6,6 +6,7 @@ Kenton Varda <temporal@gmail.com>: Primary Author
Jason Choy <jjwchoy@gmail.com>: kj/threadlocal.h and other iOS tweaks, `name` annotation in C++ code generator
Remy Blank <rblank@google.com> (contributions copyright Google Inc.): KJ Timers
Joshua Warner <joshuawarner32@gmail.com>: cmake build
Scott Purdy <scott@fer.io>: kj/std iostream interface
This file does not list people who maintain their own Cap'n Proto
implementations as separate projects. Those people are awesome too! :)
c++/Makefile.am
View file @
8ebaa614
...
...
@@ -117,6 +117,7 @@ capnpc_outputs = \
includecapnpdir
=
$(includedir)
/capnp
includekjdir
=
$(includedir)
/kj
includekjparsedir
=
$(includekjdir)
/parse
includekjstddir
=
$(includekjdir)
/std
dist_includecapnp_DATA
=
$(public_capnpc_inputs)
...
...
@@ -154,6 +155,9 @@ includekjparse_HEADERS = \
src/kj/parse/common.h
\
src/kj/parse/char.h
includekjstd_HEADERS
=
\
src/kj/std/iostream.h
includecapnp_HEADERS
=
\
src/capnp/c++.capnp.h
\
src/capnp/common.h
\
...
...
@@ -372,6 +376,7 @@ heavy_tests = \
src/kj/async-io-test.c++
\
src/kj/parse/common-test.c++
\
src/kj/parse/char-test.c++
\
src/kj/std/iostream-test.c++
\
src/capnp/capability-test.c++
\
src/capnp/schema-test.c++
\
src/capnp/schema-loader-test.c++
\
...
...
c++/src/kj/CMakeLists.txt
View file @
8ebaa614
...
...
@@ -45,10 +45,14 @@ set(kj-parse_headers
parse/common.h
parse/char.h
)
set
(
kj-std_headers
std/iostream.h
)
add_library
(
kj
${
kj_sources
}
)
install
(
TARGETS kj ARCHIVE DESTINATION
"
${
LIB_INSTALL_DIR
}
"
)
install
(
FILES
${
kj_headers
}
DESTINATION
"
${
INCLUDE_INSTALL_DIR
}
/kj"
)
install
(
FILES
${
kj-parse_headers
}
DESTINATION
"
${
INCLUDE_INSTALL_DIR
}
/kj/parse"
)
install
(
FILES
${
kj-std_headers
}
DESTINATION
"
${
INCLUDE_INSTALL_DIR
}
/kj/std"
)
set
(
kj-async_sources
async.c++
...
...
@@ -103,6 +107,7 @@ if(BUILD_TESTING)
async-io-test.c++
parse/common-test.c++
parse/char-test.c++
std/iostream-test.c++
)
target_link_libraries
(
kj-heavy-tests kj kj-async gtest gtest_main
)
add_dependencies
(
check kj-heavy-tests
)
...
...
c++/src/kj/std/iostream-test.c++
0 → 100644
View file @
8ebaa614
// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "iostream.h"
#include <sstream>
#include <gtest/gtest.h>
namespace
kj
{
namespace
std
{
namespace
{
TEST
(
StdIoStream
,
WriteVec
)
{
// Check that writing an array of arrays works even when some of the arrays
// are empty. (This used to not work in some cases.)
::
std
::
stringstream
ss
;
StdInputStream
in
(
ss
);
StdOutputStream
out
(
ss
);
ArrayPtr
<
const
byte
>
pieces
[
5
]
=
{
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
),
arrayPtr
(
reinterpret_cast
<
const
byte
*>
(
"foo"
),
3
),
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
),
arrayPtr
(
reinterpret_cast
<
const
byte
*>
(
"bar"
),
3
),
arrayPtr
(
implicitCast
<
const
byte
*>
(
nullptr
),
0
)
};
out
.
write
(
pieces
);
char
buf
[
7
];
in
.
read
(
buf
,
6
);
buf
[
6
]
=
'\0'
;
EXPECT_STREQ
(
"foobar"
,
buf
);
}
TEST
(
StdIoStream
,
TryReadToEndOfFile
)
{
// Check that tryRead works when eof is reached before minBytes.
::
std
::
stringstream
ss
;
StdInputStream
in
(
ss
);
StdOutputStream
out
(
ss
);
const
void
*
bytes
=
"foobar"
;
out
.
write
(
bytes
,
6
);
char
buf
[
9
];
in
.
tryRead
(
buf
,
8
,
8
);
buf
[
6
]
=
'\0'
;
EXPECT_STREQ
(
"foobar"
,
buf
);
}
TEST
(
StdIoStream
,
ReadToEndOfFile
)
{
// Check that read throws an exception when eof is reached before specified
// bytes.
::
std
::
stringstream
ss
;
StdInputStream
in
(
ss
);
StdOutputStream
out
(
ss
);
const
void
*
bytes
=
"foobar"
;
out
.
write
(
bytes
,
6
);
char
buf
[
9
];
Maybe
<
Exception
>
e
=
kj
::
runCatchingExceptions
([
&
]()
{
in
.
read
(
buf
,
8
,
8
);
});
buf
[
6
]
=
'\0'
;
KJ_IF_MAYBE
(
ex
,
e
)
{
// Ensure that the value is still read up to the EOF.
EXPECT_STREQ
(
"foobar"
,
buf
);
}
else
{
ADD_FAILURE
()
<<
"Expected exception"
;
}
}
}
// namespace
}
// namespace std
}
// namespace kj
c++/src/kj/std/iostream.h
0 → 100644
View file @
8ebaa614
// Copyright (c) 2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/*
* Compatibility layer for stdlib iostream
*/
#ifndef KJ_STD_IOSTREAM_H_
#define KJ_STD_IOSTREAM_H_
#include <iostream>
#include "../io.h"
namespace
kj
{
namespace
std
{
class
StdOutputStream
:
public
kj
::
OutputStream
{
public
:
explicit
StdOutputStream
(
::
std
::
ostream
&
stream
)
:
stream_
(
stream
)
{}
~
StdOutputStream
()
noexcept
(
false
)
{}
virtual
void
write
(
const
void
*
src
,
size_t
size
)
override
{
// Always writes the full size.
stream_
.
write
((
char
*
)
src
,
size
);
}
virtual
void
write
(
ArrayPtr
<
const
ArrayPtr
<
const
byte
>>
pieces
)
override
{
// Equivalent to write()ing each byte array in sequence, which is what the
// default implementation does. Override if you can do something better,
// e.g. use writev() to do the write in a single syscall.
for
(
auto
piece
:
pieces
)
{
write
(
piece
.
begin
(),
piece
.
size
());
}
}
private
:
::
std
::
ostream
&
stream_
;
};
class
StdInputStream
:
public
kj
::
InputStream
{
public
:
explicit
StdInputStream
(
::
std
::
istream
&
stream
)
:
stream_
(
stream
)
{}
~
StdInputStream
()
noexcept
(
false
)
{}
virtual
size_t
tryRead
(
void
*
buffer
,
size_t
minBytes
,
size_t
maxBytes
)
override
{
// Like read(), but may return fewer than minBytes on EOF.
stream_
.
read
((
char
*
)
buffer
,
maxBytes
);
return
stream_
.
gcount
();
}
private
:
::
std
::
istream
&
stream_
;
};
}
// namespace std
}
// namespace kj
#endif // KJ_STD_IOSTREAM_H_
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