• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT
2 #define IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT
3 
4 #include "image_io/base/message.h"
5 
6 namespace photos_editing_formats {
7 namespace image_io {
8 
9 /// The result of a some sort of match operation of the text in a data segment.
10 /// The data associated with a match result include the number of bytes
11 /// consumed to produce the result, type of match, and in the case of an error
12 /// an optional Message describing the error.
13 class DataMatchResult {
14  public:
15   /// The type of match.
16   enum Type {
17     /// An error occurred while performing the match operation.
18     kError = -1,
19 
20     /// No match was found.
21     kNone = 0,
22 
23     /// A partial match of some sort was found.
24     kPartial = 1,
25 
26     /// A partial match was found, but the end of the data in the segment or
27     /// the available range was found.
28     kPartialOutOfData = 2,
29 
30     /// A full match was found.
31     kFull = 3,
32   };
33 
DataMatchResult()34   DataMatchResult() : DataMatchResult(kNone, 0) {}
DataMatchResult(Type type)35   explicit DataMatchResult(Type type) : DataMatchResult(type, 0) {}
DataMatchResult(Type type,size_t bytes_consumed)36   DataMatchResult(Type type, size_t bytes_consumed)
37       : message_(Message::kStatus, 0, ""),
38         bytes_consumed_(bytes_consumed),
39         type_(type),
40         has_message_(false),
41         can_continue_(true) {}
42 
43   /// @return The type of the match result.
GetType()44   Type GetType() const { return type_; }
45 
46   /// @return Whether the result indicates processing can continue.
CanContinue()47   bool CanContinue() const { return can_continue_; }
48 
49   /// @return Whether the match result has a message associated with it.
HasMessage()50   bool HasMessage() const { return has_message_; }
51 
52   /// @return The message associated with the result.
GetMessage()53   const Message& GetMessage() const { return message_; }
54 
55   /// @return The number of bytes consumed to produce the result.
GetBytesConsumed()56   size_t GetBytesConsumed() const { return bytes_consumed_; }
57 
58   /// @param delta The byte count to increase the bytes consumed value with.
IncrementBytesConsumed(size_t delta)59   size_t IncrementBytesConsumed(size_t delta) {
60     bytes_consumed_ += delta;
61     return bytes_consumed_;
62   }
63 
64   /// @param type The type to use for this match result.
65   /// @return A reference to this match result.
SetType(Type type)66   DataMatchResult& SetType(Type type) {
67     type_ = type;
68     return *this;
69   }
70 
71   /// Sets the flag that indicates whether processing can continue.
72   /// @param can_continue The new value for the can_continue_ flag.
SetCanContinue(bool can_continue)73   DataMatchResult& SetCanContinue(bool can_continue) {
74     can_continue_ = can_continue;
75     return *this;
76   }
77 
78   /// @param bytes_consumed The byte count to use for this match result.
79   /// @return A reference to this match result.
SetBytesConsumed(size_t bytes_consumed)80   DataMatchResult& SetBytesConsumed(size_t bytes_consumed) {
81     bytes_consumed_ = bytes_consumed;
82     return *this;
83   }
84 
85   /// @param message The message to use for this match result.
86   /// @return A reference to this match result.
SetMessage(const Message & message)87   DataMatchResult& SetMessage(const Message& message) {
88     message_ = message;
89     has_message_ = true;
90     return *this;
91   }
92 
93   /// @param type The message type to use for this match result.
94   /// @param text The message text to use for this match result.
95   /// @return A reference to this match result.
SetMessage(const Message::Type type,const std::string & text)96   DataMatchResult& SetMessage(const Message::Type type,
97                               const std::string& text) {
98     return SetMessage(Message(type, 0, text));
99   }
100 
101   /// @param other The other result to test for equality with this one.
102   /// @return Whether this and the other results are equal
103   bool operator==(const DataMatchResult& other) const {
104     return can_continue_ == other.can_continue_ &&
105            has_message_ == other.has_message_ && type_ == other.type_ &&
106            bytes_consumed_ == other.bytes_consumed_ &&
107            message_ == other.message_;
108   }
109 
110   /// @param other The other result to test for inequality with this one.
111   /// @return Whether this and the other results are not equal
112   bool operator!=(const DataMatchResult& other) const {
113     return !(*this == other);
114   }
115 
116  private:
117   Message message_;
118   size_t bytes_consumed_;
119   Type type_;
120   bool has_message_;
121   bool can_continue_;
122 };
123 
124 }  // namespace image_io
125 }  // namespace photos_editing_formats
126 
127 #endif // IMAGE_IO_BASE_DATA_MATCH_RESULT_H_  // NOLINT
128