protoio.hh revision 9397
12SN/A/*
21762SN/A * Copyright (c) 2012 ARM Limited
32SN/A * All rights reserved
42SN/A *
52SN/A * The license below extends only to copyright in the software and shall
62SN/A * not be construed as granting a license to any other intellectual
72SN/A * property including but not limited to intellectual property relating
82SN/A * to a hardware implementation of the functionality of the software
92SN/A * licensed hereunder.  You may use the software subject to the license
102SN/A * terms below provided that you ensure that this notice is replicated
112SN/A * unmodified and in its entirety in all distributions of the software,
122SN/A * modified or unmodified, in source code or in binary form.
132SN/A *
142SN/A * Redistribution and use in source and binary forms, with or without
152SN/A * modification, are permitted provided that the following conditions are
162SN/A * met: redistributions of source code must retain the above copyright
172SN/A * notice, this list of conditions and the following disclaimer;
182SN/A * redistributions in binary form must reproduce the above copyright
192SN/A * notice, this list of conditions and the following disclaimer in the
202SN/A * documentation and/or other materials provided with the distribution;
212SN/A * neither the name of the copyright holders nor the names of its
222SN/A * contributors may be used to endorse or promote products derived from
232SN/A * this software without specific prior written permission.
242SN/A *
252SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
262SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272665Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282665Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292665Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
302SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
312SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
321717SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
331717SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
342SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
352SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362SN/A *
37707SN/A * Authors: Andreas Hansson
381858SN/A */
3956SN/A
402856Srdreslin@umich.edu
412109SN/A/**
422SN/A * @file
433520Sgblack@eecs.umich.edu * Declaration of a wrapper for protobuf output streams.
443520Sgblack@eecs.umich.edu */
453520Sgblack@eecs.umich.edu
463520Sgblack@eecs.umich.edu#ifndef __PROTO_PROTOIO_HH__
472190SN/A#define __PROTO_PROTOIO_HH__
482315SN/A
492680Sktlim@umich.edu#include <google/protobuf/io/coded_stream.h>
502SN/A#include <google/protobuf/io/gzip_stream.h>
512856Srdreslin@umich.edu#include <google/protobuf/io/zero_copy_stream_impl.h>
522SN/A#include <google/protobuf/message.h>
532356SN/A
542356SN/A#include <fstream>
552356SN/A
562356SN/A/**
572356SN/A * A ProtoOutputStream wraps a coded stream, potentially with
582356SN/A * compression, based on looking at the file name. Writing to the
592356SN/A * stream is done to enable interaction with the file on a per-message
602356SN/A * basis to avoid having to deal with huge data structures. The latter
613126Sktlim@umich.edu * is made possible by encoding the length of each message in the
622356SN/A * stream.
632356SN/A */
642356SN/Aclass ProtoOutputStream
652356SN/A{
662356SN/A
672356SN/A  public:
682856Srdreslin@umich.edu
692SN/A    /**
701634SN/A     * Create an output stream for a given file name. If the filename
711634SN/A     * ends with .gz then the file will be compressed accordinly.
721695SN/A     *
731634SN/A     * @param filename Path to the file to create or truncate
741634SN/A     */
752359SN/A    ProtoOutputStream(const std::string& filename);
761695SN/A
771695SN/A    /**
781695SN/A     * Destruct the output stream, and also flush and close the
791634SN/A     * underlying file streams and coded streams.
801858SN/A     */
812SN/A    ~ProtoOutputStream();
823520Sgblack@eecs.umich.edu
833520Sgblack@eecs.umich.edu    /**
843520Sgblack@eecs.umich.edu     * Write a message to the stream, preprending it with the message
852SN/A     * size.
862SN/A     *
872SN/A     * @param msg Message to write to the stream
882SN/A     */
892SN/A    void write(const google::protobuf::Message& msg);
901133SN/A
912SN/A  private:
922SN/A
933520Sgblack@eecs.umich.edu    /// Underlying file output stream
942SN/A    std::ofstream fileStream;
952SN/A
963520Sgblack@eecs.umich.edu    /// Zero Copy stream wrapping the STL output stream
973520Sgblack@eecs.umich.edu    google::protobuf::io::OstreamOutputStream* zeroCopyStream;
981917SN/A
991917SN/A    /// Optional Gzip stream to wrap the Zero Copy stream
1001917SN/A    google::protobuf::io::GzipOutputStream* gzipStream;
1011917SN/A
1021917SN/A    /// Top-level coded stream that messages are written to
1031917SN/A    google::protobuf::io::CodedOutputStream* codedStream;
1041917SN/A
1051917SN/A};
1061917SN/A
1071917SN/A#endif //__PROTO_PROTOIO_HH
1081917SN/A