summaryrefslogtreecommitdiff
path: root/includes/image_io/base/ostream_ref_data_destination.h
blob: 266e2be715ad12ba4978a79e8ccbcec68a1ded20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT
#define IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT

#include <iostream>
#include <string>

#include "image_io/base/data_destination.h"
#include "image_io/base/message_handler.h"

namespace photos_editing_formats {
namespace image_io {

/// A DataDestination that writes its output to an ostream held as a reference.
class OStreamRefDataDestination : public DataDestination {
 public:
  /// Constructs an OStreamDataDestination using the given ostream.
  /// @param ostream_ref The ostream to which data is written.
  /// @param message_handler An option message handler for writing messages.
  OStreamRefDataDestination(std::ostream& ostream_ref,
                            MessageHandler* message_handler)
      : ostream_ref_(ostream_ref),
        message_handler_(message_handler),
        bytes_transferred_(0),
        has_error_(false) {}
  OStreamRefDataDestination(const OStreamRefDataDestination&) = delete;
  OStreamRefDataDestination& operator=(const OStreamRefDataDestination&) =
      delete;

  /// @param name A name to associate with the ostream. Used for error messages.
  void SetName(const std::string& name) { name_ = name; }

  /// @return The name associated with the ostream.
  const std::string& GetName() const { return name_; }

  /// @return The number of bytes written to the ostream.
  size_t GetBytesTransferred() const override { return bytes_transferred_; }

  /// @return True if errors were encountered while writing to the ostream.
  bool HasError() const { return has_error_; }

  void StartTransfer() override;
  TransferStatus Transfer(const DataRange& transfer_range,
                          const DataSegment& data_segment) override;
  void FinishTransfer() override;

 private:
  /// The ostream written to.
  std::ostream& ostream_ref_;

  /// An optional message handler to write messages to.
  MessageHandler* message_handler_;

  /// The number of bytes written so far.
  size_t bytes_transferred_;

  /// A (file) name to associate with the ostream, used with error messages.
  std::string name_;

  /// If true indicates an error has occurred writing to the ostream.
  bool has_error_;
};

}  // namespace image_io
}  // namespace photos_editing_formats

#endif  // IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_  // NOLINT