Commit 8b86e54d authored by Hauke Goldhammer's avatar Hauke Goldhammer

add the changes requested in the comments of the last commits

parent 14fbc92b
......@@ -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 iStream>
template <class InputStream>
struct stream_reader_state {
// -- constructors, destructors, and assignment operators --------------------
......@@ -58,14 +41,14 @@ struct stream_reader_state {
// nop
}
void init(iStream&& src_stream) {
void init(InputStream&& 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
iStream stream;
InputStream stream;
/// Caches the stream line we are about to stream.
std::string line;
};
/// @relates stream_reader
template <class iStream>
using stream_source_type = stateful_actor<stream_reader_state<iStream>>;
/// Streams the content of given istream line by line using the given policy to
/// all given stream sinks.
template <class Policy, class iStream, class Handle, class... Handles>
behavior stream_reader(stream_source_type<iStream>* self, iStream src_stream,
Handle sink, Handles... sinks) {
template <class InputStream>
using stream_source_type = stateful_actor<stream_reader_state<InputStream>>;
/// Streams the content of given 'src_stream' line by line using the given
/// policy to all given stream sinks.
template <class Policy, class InputStream, 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(); });
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment