00001 /* 00002 * Copyright 2018 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_IO_INTERNAL_IN_MEMORY_PROTO_STREAM_H_ 00018 #define CARTOGRAPHER_IO_INTERNAL_IN_MEMORY_PROTO_STREAM_H_ 00019 00020 #include <queue> 00021 00022 #include "absl/memory/memory.h" 00023 #include "cartographer/common/port.h" 00024 #include "cartographer/io/proto_stream_interface.h" 00025 #include "google/protobuf/message.h" 00026 00027 namespace cartographer { 00028 namespace io { 00029 00030 class ForwardingProtoStreamWriter 00031 : public cartographer::io::ProtoStreamWriterInterface { 00032 public: 00033 // A callback that is invoked anytime 'WriteProto()' is called on the 00034 // 'ForwardingProtoStreamWriter'. When 'Close()' is called on the 00035 // 'ForwardingProtoStreamWriter' the callback is invoked with a 'nullptr'. 00036 using WriterCallback = 00037 std::function<bool(const google::protobuf::Message* proto)>; 00038 00039 explicit ForwardingProtoStreamWriter(WriterCallback writer_callback) 00040 : writer_callback_(writer_callback) {} 00041 ~ForwardingProtoStreamWriter() = default; 00042 00043 void WriteProto(const google::protobuf::Message& proto) override; 00044 bool Close() override; 00045 00046 private: 00047 WriterCallback writer_callback_; 00048 }; 00049 00050 class InMemoryProtoStreamReader 00051 : public cartographer::io::ProtoStreamReaderInterface { 00052 public: 00053 explicit InMemoryProtoStreamReader( 00054 std::queue<std::unique_ptr<google::protobuf::Message>>&& state_chunks) 00055 : state_chunks_(std::move(state_chunks)) {} 00056 InMemoryProtoStreamReader() = default; 00057 ~InMemoryProtoStreamReader() = default; 00058 00059 InMemoryProtoStreamReader(const InMemoryProtoStreamReader&) = delete; 00060 InMemoryProtoStreamReader& operator=(const InMemoryProtoStreamReader&) = 00061 delete; 00062 00063 template <typename MessageType> 00064 void AddProto(const MessageType& proto) { 00065 state_chunks_.push(absl::make_unique<MessageType>(proto)); 00066 } 00067 00068 bool ReadProto(google::protobuf::Message* proto) override; 00069 bool eof() const override { return state_chunks_.empty(); } 00070 00071 private: 00072 std::queue<std::unique_ptr<google::protobuf::Message>> state_chunks_; 00073 }; 00074 00075 } // namespace io 00076 } // namespace cartographer 00077 00078 #endif // CARTOGRAPHER_IO_INTERNAL_IN_MEMORY_PROTO_STREAM_H_