• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef IMAGE_IO_BASE_MESSAGE_STORE_H_  // NOLINT
2 #define IMAGE_IO_BASE_MESSAGE_STORE_H_  // NOLINT
3 
4 #include <vector>
5 #include "image_io/base/message.h"
6 
7 namespace photos_editing_formats {
8 namespace image_io {
9 
10 /// An abstract base class for storing and reporting on Messages.
11 class MessageStore {
12  public:
13   virtual ~MessageStore() = default;
14 
15   /// Clears the messages maintained by the store.
16   virtual void ClearMessages() = 0;
17 
18   // @message The message to add to the store.
19   virtual void AddMessage(const Message& message) = 0;
20 
21   /// @return A vector of messages maintained by the store; this vector may be
22   ///     empty even if the AddMessage function was called, depending on the
23   ///     concrete subclass is implemented.
24   virtual std::vector<Message> GetMessages() const = 0;
25 
26   /// @return Whether the store has error messages or not. This value is
27   ///     guarenteed to be accurate based on the latest calls to the
28   ///     ClearMessages and AddMessage functions.
29   virtual bool HasErrorMessages() const = 0;
30 };
31 
32 /// A MessageStore that saves the messages in a vector. The implementation of
33 /// this class is not thread safe.
34 class VectorMessageStore : public MessageStore {
35  public:
ClearMessages()36   void ClearMessages() override { messages_.clear(); }
AddMessage(const Message & message)37   void AddMessage(const Message& message) override {
38     messages_.push_back(message);
39   }
GetMessages()40   std::vector<Message> GetMessages() const override { return messages_; }
HasErrorMessages()41   bool HasErrorMessages() const override {
42     for (const auto& message : messages_) {
43       if (message.GetType() != Message::kStatus) {
44         return true;
45       }
46     }
47     return false;
48   }
49 
50  private:
51   std::vector<Message> messages_;
52 };
53 
54 /// A MessageStore that simply keeps track of whether error messages have been
55 /// added or not, but does not store the messages themselves. The implementation
56 /// of this class is should not cause any crashes if run in a multi-threaded
57 /// environment, though there may be some cases where erroneous results are
58 /// returned by the HasErrorMessages function.
59 class ErrorFlagMessageStore : public MessageStore {
60  public:
ErrorFlagMessageStore()61   ErrorFlagMessageStore() : has_error_(false) {}
ClearMessages()62   void ClearMessages() override { has_error_ = false; }
AddMessage(const Message & message)63   void AddMessage(const Message& message) override {
64     if (message.IsError()) {
65       has_error_ = true;
66     }
67   }
GetMessages()68   std::vector<Message> GetMessages() const override {
69     return std::vector<Message>();
70   }
HasErrorMessages()71   bool HasErrorMessages() const override { return has_error_; }
72 
73  private:
74   bool has_error_;
75 };
76 
77 }  // namespace image_io
78 }  // namespace photos_editing_formats
79 
80 #endif // IMAGE_IO_BASE_MESSAGE_STORE_H_  // NOLINT
81