summaryrefslogtreecommitdiff
path: root/includes/image_io/base/data_match_result.h
blob: 3bde0814bf3cdb19fca1d365cc9a4b292030405f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#ifndef IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT
#define IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT

#include "image_io/base/message.h"

namespace photos_editing_formats {
namespace image_io {

/// The result of a some sort of match operation of the text in a data segment.
/// The data associated with a match result include the number of bytes
/// consumed to produce the result, type of match, and in the case of an error
/// an optional Message describing the error.
class DataMatchResult {
 public:
  /// The type of match.
  enum Type {
    /// An error occurred while performing the match operation.
    kError = -1,

    /// No match was found.
    kNone = 0,

    /// A partial match of some sort was found.
    kPartial = 1,

    /// A partial match was found, but the end of the data in the segment or
    /// the available range was found.
    kPartialOutOfData = 2,

    /// A full match was found.
    kFull = 3,
  };

  DataMatchResult() : DataMatchResult(kNone, 0) {}
  explicit DataMatchResult(Type type) : DataMatchResult(type, 0) {}
  DataMatchResult(Type type, size_t bytes_consumed)
      : message_(Message::kStatus, 0, ""),
        bytes_consumed_(bytes_consumed),
        type_(type),
        has_message_(false),
        can_continue_(true) {}

  /// @return The type of the match result.
  Type GetType() const { return type_; }

  /// @return Whether the result indicates processing can continue.
  bool CanContinue() const { return can_continue_; }

  /// @return Whether the match result has a message associated with it.
  bool HasMessage() const { return has_message_; }

  /// @return The message associated with the result.
  const Message& GetMessage() const { return message_; }

  /// @return The number of bytes consumed to produce the result.
  size_t GetBytesConsumed() const { return bytes_consumed_; }

  /// @param delta The byte count to increase the bytes consumed value with.
  size_t IncrementBytesConsumed(size_t delta) {
    bytes_consumed_ += delta;
    return bytes_consumed_;
  }

  /// @param type The type to use for this match result.
  /// @return A reference to this match result.
  DataMatchResult& SetType(Type type) {
    type_ = type;
    return *this;
  }

  /// Sets the flag that indicates whether processing can continue.
  /// @param can_continue The new value for the can_continue_ flag.
  DataMatchResult& SetCanContinue(bool can_continue) {
    can_continue_ = can_continue;
    return *this;
  }

  /// @param bytes_consumed The byte count to use for this match result.
  /// @return A reference to this match result.
  DataMatchResult& SetBytesConsumed(size_t bytes_consumed) {
    bytes_consumed_ = bytes_consumed;
    return *this;
  }

  /// @param message The message to use for this match result.
  /// @return A reference to this match result.
  DataMatchResult& SetMessage(const Message& message) {
    message_ = message;
    has_message_ = true;
    return *this;
  }

  /// @param type The message type to use for this match result.
  /// @param text The message text to use for this match result.
  /// @return A reference to this match result.
  DataMatchResult& SetMessage(const Message::Type type,
                              const std::string& text) {
    return SetMessage(Message(type, 0, text));
  }

  /// @param other The other result to test for equality with this one.
  /// @return Whether this and the other results are equal
  bool operator==(const DataMatchResult& other) const {
    return can_continue_ == other.can_continue_ &&
           has_message_ == other.has_message_ && type_ == other.type_ &&
           bytes_consumed_ == other.bytes_consumed_ &&
           message_ == other.message_;
  }

  /// @param other The other result to test for inequality with this one.
  /// @return Whether this and the other results are not equal
  bool operator!=(const DataMatchResult& other) const {
    return !(*this == other);
  }

 private:
  Message message_;
  size_t bytes_consumed_;
  Type type_;
  bool has_message_;
  bool can_continue_;
};

}  // namespace image_io
}  // namespace photos_editing_formats

#endif // IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT