2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Hansson
43 * Declaration of a wrapper for protobuf output streams and input streams.
46 #ifndef __PROTO_PROTOIO_HH__
47 #define __PROTO_PROTOIO_HH__
49 #include <google/protobuf/io/coded_stream.h>
50 #include <google/protobuf/io/gzip_stream.h>
51 #include <google/protobuf/io/zero_copy_stream_impl.h>
52 #include <google/protobuf/message.h>
57 * A ProtoStream provides the shared functionality of the input and
58 * output streams. At the moment this is limited to magic number.
65 /// Use the ASCII characters gem5 as our magic number
66 static const uint32_t magicNumber = 0x356d6567;
69 * Create a ProtoStream.
76 * Hide the copy constructor and assignment operator.
79 ProtoStream(const ProtoStream&);
80 ProtoStream& operator=(const ProtoStream&);
85 * A ProtoOutputStream wraps a coded stream, potentially with
86 * compression, based on looking at the file name. Writing to the
87 * stream is done to enable interaction with the file on a per-message
88 * basis to avoid having to deal with huge data structures. The latter
89 * is made possible by encoding the length of each message in the
92 class ProtoOutputStream : public ProtoStream
98 * Create an output stream for a given file name. If the filename
99 * ends with .gz then the file will be compressed accordinly.
101 * @param filename Path to the file to create or truncate
103 ProtoOutputStream(const std::string& filename);
106 * Destruct the output stream, and also flush and close the
107 * underlying file streams and coded streams.
109 ~ProtoOutputStream();
112 * Write a message to the stream, preprending it with the message
115 * @param msg Message to write to the stream
117 void write(const google::protobuf::Message& msg);
121 /// Underlying file output stream
122 std::ofstream fileStream;
124 /// Zero Copy stream wrapping the STL output stream
125 google::protobuf::io::OstreamOutputStream* wrappedFileStream;
127 /// Optional Gzip stream to wrap the Zero Copy stream
128 google::protobuf::io::GzipOutputStream* gzipStream;
130 /// Top-level zero-copy stream, either with compression or not
131 google::protobuf::io::ZeroCopyOutputStream* zeroCopyStream;
136 * A ProtoInputStream wraps a coded stream, potentially with
137 * decompression, based on looking at the file name. Reading from the
138 * stream is done on a per-message basis to avoid having to deal with
139 * huge data structures. The latter assumes the length of each message
140 * is encoded in the stream when it is written.
142 class ProtoInputStream : public ProtoStream
148 * Create an input stream for a given file name. If the filename
149 * ends with .gz then the file will be decompressed accordingly.
151 * @param filename Path to the file to read from
153 ProtoInputStream(const std::string& filename);
156 * Destruct the input stream, and also close the underlying file
157 * streams and coded streams.
162 * Read a message from the stream.
164 * @param msg Message read from the stream
165 * @param return True if a message was read, false if reading fails
167 bool read(google::protobuf::Message& msg);
170 * Reset the input stream and seek to the beginning of the file.
177 * Create the internal streams that are wrapping the input file.
179 void createStreams();
182 * Destroy the internal streams that are wrapping the input file.
184 void destroyStreams();
186 /// Underlying file input stream
187 std::ifstream fileStream;
189 /// Hold on to the file name for debug messages
190 const std::string fileName;
192 /// Boolean flag to remember whether we use gzip or not
195 /// Zero Copy stream wrapping the STL input stream
196 google::protobuf::io::IstreamInputStream* wrappedFileStream;
198 /// Optional Gzip stream to wrap the Zero Copy stream
199 google::protobuf::io::GzipInputStream* gzipStream;
201 /// Top-level zero-copy stream, either with compression or not
202 google::protobuf::io::ZeroCopyInputStream* zeroCopyStream;
206 #endif //__PROTO_PROTOIO_HH