• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_BASE_DATA_RANGE_TRACKING_DESTINATION_H_  // NOLINT
2 #define IMAGE_IO_BASE_DATA_RANGE_TRACKING_DESTINATION_H_  // NOLINT
3 
4 #include "image_io/base/data_destination.h"
5 #include "image_io/base/data_range.h"
6 
7 namespace photos_editing_formats {
8 namespace image_io {
9 
10 /// A DataDestination that tracks the transfer_range values as they are passed
11 /// from the caller of the Transfer() function to next DataDestination.
12 /// Instances of this class can be used to track the number of bytes transferred
13 /// and/or to ensure that multiple calls to the Transfer() function are called
14 /// with transfer_range values that join in a end-to-begin fashion. This data
15 /// can be used to make sure that the data transferred meets the expectations of
16 /// the client.
17 class DataRangeTrackingDestination : public DataDestination {
18  public:
19   /// @param destination The DataDestination that is next in the chain, or
20   ///     nullptr if there is no destination.
DataRangeTrackingDestination(DataDestination * destination)21   explicit DataRangeTrackingDestination(DataDestination* destination)
22       : destination_(destination),
23         bytes_transferred_(0),
24         has_disjoint_transfer_ranges_(false) {}
25 
26   /// @return The number of bytes written to the data destination. Bytes are
27   /// considered "written" even if the next destination is a nullptr.
GetBytesTransferred()28   size_t GetBytesTransferred() const override { return bytes_transferred_; }
29 
30   /// @return The tracked data range (see the class comment for how this value
31   ///     is computed).
GetTrackedDataRange()32   const DataRange& GetTrackedDataRange() const { return tracked_data_range_; }
33 
34   /// @return Whether disjoint transfer data ranges were detected by the
35   ///     Transfer() function. Disjoint transfer ranges occur when two calls
36   ///     to the Transfer() function occur where first_range.GetEnd() is not
37   ////    equal to the second_range.GetBegin().
HasDisjointTransferRanges()38   bool HasDisjointTransferRanges() const {
39     return has_disjoint_transfer_ranges_;
40   }
41 
42   void StartTransfer() override;
43   TransferStatus Transfer(const DataRange& transfer_range,
44                           const DataSegment& data_segment) override;
45   void FinishTransfer() override;
46 
47  private:
48   DataDestination* destination_;
49   DataRange tracked_data_range_;
50   size_t bytes_transferred_;
51   bool has_disjoint_transfer_ranges_;
52 };
53 
54 }  // namespace image_io
55 }  // namespace photos_editing_formats
56 
57 #endif // IMAGE_IO_BASE_DATA_RANGE_TRACKING_DESTINATION_H_  // NOLINT
58