Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
actor-incubator
Commits
8b86e54d
Commit
8b86e54d
authored
Mar 14, 2019
by
Hauke Goldhammer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the changes requested in the comments of the last commits
parent
14fbc92b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
33 deletions
+92
-33
libcaf_bb/caf/bb/stream_reader.hpp
libcaf_bb/caf/bb/stream_reader.hpp
+18
-33
libcaf_bb/caf/bb/tokenized_integer_reader.hpp
libcaf_bb/caf/bb/tokenized_integer_reader.hpp
+74
-0
No files found.
libcaf_bb/caf/bb/stream_reader.hpp
View file @
8b86e54d
...
...
@@ -18,7 +18,8 @@
#pragma once
#include <fstream>
#include "tokenized_integer_reader.hpp"
#include <string>
#include <vector>
...
...
@@ -31,26 +32,8 @@
namespace
caf
{
namespace
bb
{
using
file_name
=
std
::
string
;
/// @relates stream_reader
/// The Policy defines how the stream_reader pares a line of the given file.
class
IntergerPolicy
{
public:
using
value_type
=
int
;
/// Returns number of produced elements or an error.
expected
<
size_t
>
operator
()(
std
::
string
&
line
,
downstream
<
value_type
>
out
)
{
std
::
vector
<
std
::
string
>
tokens
;
split
(
tokens
,
line
,
' '
);
for
(
auto
&
token
:
tokens
)
out
.
push
(
std
::
stoi
(
token
));
return
tokens
.
size
();
}
};
/// @relates stream_reader
template
<
class
i
Stream
>
template
<
class
Input
Stream
>
struct
stream_reader_state
{
// -- constructors, destructors, and assignment operators --------------------
...
...
@@ -58,14 +41,14 @@ struct stream_reader_state {
// nop
}
void
init
(
i
Stream
&&
src_stream
)
{
void
init
(
Input
Stream
&&
src_stream
)
{
stream
=
std
::
move
(
src_stream
);
}
// -- properties -------------------------------------------------------------
size_t
at_end
()
const
{
return
stream
.
eof
()
;
return
!
stream
;
}
// -- member variables -------------------------------------------------------
...
...
@@ -74,21 +57,21 @@ struct stream_reader_state {
const
char
*
name
;
/// Stream
i
Stream
stream
;
Input
Stream
stream
;
/// Caches the stream line we are about to stream.
std
::
string
line
;
};
/// @relates stream_reader
template
<
class
i
Stream
>
using
stream_source_type
=
stateful_actor
<
stream_reader_state
<
i
Stream
>>
;
/// Streams the content of given
istream line by line using the given policy to
/// all given stream sinks.
template
<
class
Policy
,
class
i
Stream
,
class
Handle
,
class
...
Handles
>
behavior
stream_reader
(
stream_source_type
<
iStream
>*
self
,
iStream
src_stream
,
Handle
sink
,
Handles
...
sinks
)
{
template
<
class
Input
Stream
>
using
stream_source_type
=
stateful_actor
<
stream_reader_state
<
Input
Stream
>>
;
/// Streams the content of given
'src_stream' line by line using the given
///
policy to
all given stream sinks.
template
<
class
Policy
,
class
Input
Stream
,
class
Handle
,
class
...
Handles
>
behavior
stream_reader
(
stream_source_type
<
InputStream
>*
self
,
InputStream
src_stream
,
Handle
sink
,
Handles
...
sinks
)
{
using
value_type
=
typename
Policy
::
value_type
;
self
->
state
.
init
(
std
::
move
(
src_stream
));
// Fail early if we got nothing to stream.
...
...
@@ -105,9 +88,11 @@ behavior stream_reader(stream_source_type<iStream>* self, iStream src_stream,
Policy
pol
;
size_t
i
=
0
;
while
(
i
<
hint
&&
getline
(
st
.
stream
,
st
.
line
))
{
auto
count
=
pol
(
st
.
line
,
out
);
if
(
count
.
engaged
())
if
(
auto
count
=
pol
(
st
.
line
,
out
))
{
i
+=
*
count
;
}
else
{
self
->
quit
(
count
.
error
());
}
}
},
[
self
](
const
unit_t
&
)
{
return
self
->
state
.
at_end
();
});
...
...
libcaf_bb/caf/bb/tokenized_integer_reader.hpp
0 → 100644
View file @
8b86e54d
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <string>
#include <vector>
#include "caf/behavior.hpp"
#include "caf/event_based_actor.hpp"
#include "caf/stateful_actor.hpp"
#include "caf/string_algorithms.hpp"
#include "caf/unit.hpp"
namespace
caf
{
namespace
bb
{
/// @relates stream_reader
/// The error codes for the stream_reader policy.
enum
class
stream_reader_policy_error
:
uint8_t
{
out_of_range
=
1
};
error
make_error
(
stream_reader_policy_error
x
)
{
return
{
static_cast
<
uint8_t
>
(
x
),
atom
(
"stream"
)};
}
/// @relates stream_reader
/// The Policy defines how the stream_reader pares a line of the given stream to
/// integers.
template
<
class
ValueType
=
int32_t
>
class
tokenized_integer_reader
{
public:
using
value_type
=
ValueType
;
/// Returns number of produced elements or an error.
expected
<
size_t
>
operator
()(
const
std
::
string
&
line
,
downstream
<
value_type
>
out
)
{
size_t
count
=
0
;
auto
i
=
line
.
c_str
();
while
(
*
i
!=
'\0'
)
{
// Parse next integer.
char
*
end
=
nullptr
;
auto
value
=
strtoll
(
i
,
&
end
,
10
);
if
(
errno
==
ERANGE
)
{
return
make_error
(
stream_reader_policy_error
::
out_of_range
);
}
++
count
;
out
.
push
(
value
);
// TODO: check whether value fits into value_type
// Advance iterator.
i
=
end
;
while
(
isspace
(
*
i
))
++
i
;
}
return
count
;
}
};
}
// namespace bb
}
// namespace caf
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