1 #ifndef IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT
2 #define IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT
3 
4 #include <iostream>
5 #include <string>
6 
7 #include "image_io/base/data_destination.h"
8 #include "image_io/base/message_handler.h"
9 
10 namespace photos_editing_formats {
11 namespace image_io {
12 
13 /// A DataDestination that writes its output to an ostream held as a reference.
14 class OStreamRefDataDestination : public DataDestination {
15  public:
16   /// Constructs an OStreamDataDestination using the given ostream.
17   /// @param ostream_ref The ostream to which data is written.
18   /// @param message_handler An option message handler for writing messages.
OStreamRefDataDestination(std::ostream & ostream_ref,MessageHandler * message_handler)19   OStreamRefDataDestination(std::ostream& ostream_ref,
20                             MessageHandler* message_handler)
21       : ostream_ref_(ostream_ref),
22         message_handler_(message_handler),
23         bytes_transferred_(0),
24         has_error_(false) {}
25   OStreamRefDataDestination(const OStreamRefDataDestination&) = delete;
26   OStreamRefDataDestination& operator=(const OStreamRefDataDestination&) =
27       delete;
28 
29   /// @param name A name to associate with the ostream. Used for error messages.
SetName(const std::string & name)30   void SetName(const std::string& name) { name_ = name; }
31 
32   /// @return The name associated with the ostream.
GetName()33   const std::string& GetName() const { return name_; }
34 
35   /// @return The number of bytes written to the ostream.
GetBytesTransferred()36   size_t GetBytesTransferred() const override { return bytes_transferred_; }
37 
38   /// @return True if errors were encountered while writing to the ostream.
HasError()39   bool HasError() const { return has_error_; }
40 
41   void StartTransfer() override;
42   TransferStatus Transfer(const DataRange& transfer_range,
43                           const DataSegment& data_segment) override;
44   void FinishTransfer() override;
45 
46  private:
47   /// The ostream written to.
48   std::ostream& ostream_ref_;
49 
50   /// An optional message handler to write messages to.
51   MessageHandler* message_handler_;
52 
53   /// The number of bytes written so far.
54   size_t bytes_transferred_;
55 
56   /// A (file) name to associate with the ostream, used with error messages.
57   std::string name_;
58 
59   /// If true indicates an error has occurred writing to the ostream.
60   bool has_error_;
61 };
62 
63 }  // namespace image_io
64 }  // namespace photos_editing_formats
65 
66 #endif  // IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT
67