13 #ifndef __STOUT_RECORDIO_HPP__
14 #define __STOUT_RECORDIO_HPP__
60 Encoder(std::function<std::string(
const T&)> _serialize)
61 : serialize(_serialize) {}
66 std::string
encode(
const T& record)
const
68 std::string s = serialize(record);
73 std::function<std::string(const T&)> serialize;
85 Decoder(std::function<
Try<T>(
const std::string&)> _deserialize)
86 : state(HEADER), deserialize(_deserialize) {}
102 if (state == FAILED) {
103 return Error(
"Decoder is in a FAILED state");
106 std::deque<Try<T>> records;
108 foreach (
char c, data) {
109 if (state == HEADER) {
123 return Error(
"Failed to decode length '" + buffer +
"': " +
127 length = numify.
get();
132 if (numify.
get() <= 0) {
133 records.push_back(deserialize(buffer));
136 }
else if (state == RECORD) {
138 CHECK_LT(buffer.size(), length.
get());
142 if (buffer.size() == length.
get()) {
143 records.push_back(deserialize(buffer));
166 std::function<Try<T>(
const std::string&)> deserialize;
171 #endif // __STOUT_RECORDIO_HPP__
Definition: errorbase.hpp:35
return stringify(s.size())+"\n"+s
Try< T > numify(const std::string &s)
Definition: numify.hpp:29
Try< Nothing > encode(const Netlink< struct rtnl_cls > &cls, const Classifier &classifier)
#define CHECK_SOME(expression)
Definition: check.hpp:44
const T & get() const &
Definition: option.hpp:118
static Try error(const E &e)
Definition: try.hpp:42
bool isError() const
Definition: try.hpp:71
Try< std::deque< Try< T > > > decode(const std::string &data)
Decodes another chunk of data from the "Record-IO" stream and returns the attempted decoding of any a...
Definition: recordio.hpp:100
Given an encoding function for individual records, this provides encoding from typed records into "Re...
Definition: recordio.hpp:57
Given a decoding function for individual records, this provides decoding from "Record-IO" data into t...
Definition: recordio.hpp:82
const T & get() const
Definition: try.hpp:73