1 #ifndef IMAGE_IO_BASE_MESSAGE_HANDLER_H_ // NOLINT 2 #define IMAGE_IO_BASE_MESSAGE_HANDLER_H_ // NOLINT 3 4 #include <memory> 5 #include <string> 6 #include <vector> 7 8 #include "image_io/base/message.h" 9 #include "image_io/base/message_stats.h" 10 #include "image_io/base/message_store.h" 11 #include "image_io/base/message_writer.h" 12 13 namespace photos_editing_formats { 14 namespace image_io { 15 16 /// MessageHandler provides the functions that all the code in this library uses 17 /// to report status and error conditions. 18 class MessageHandler { 19 public: 20 /// The default constructor for MessageHandler creates a MessageWriter and 21 /// VectorMessageStore for handling writing and storing messages. 22 MessageHandler(); 23 24 /// Sets the message writer to use when ReportMessage() is called. If client 25 /// code does not call this function, the MessageHandler returned by the Get() 26 /// function will have a CoutMessageWriter by default. If client code calls 27 /// this function with a null, then ReportMessage() will not write messages at 28 /// all, but just add them to the messages store. 29 /// @param message_writer The message writer that ReportMessage uses, or null. 30 void SetMessageWriter(std::unique_ptr<MessageWriter> message_writer); 31 32 /// Sets the message store to use when ReportMessage() is called. If client 33 /// code does not call this function, the MessageHandler returned by the Get() 34 /// function will have a VectorMessageStore by default. If client code calls 35 /// this function with a null, then ReportMessage() will not save messages at 36 /// all, but just write them to the messages writer. 37 /// @param message_store The message store that ReportMessage uses, or null. 38 void SetMessageStore(std::unique_ptr<MessageStore> message_store); 39 40 /// Clears the messages maintained by the message handler's store. Client code 41 /// should call this function before calling any other standalone or class 42 /// function in this library so as to provide a clean starting point with 43 /// respect to error and status messages. Once all the calls have been made, 44 /// client code should examine the messages or call HasErrorMessages() to 45 /// determine the whether the calls succeeded or not. Finally client code 46 /// should call this function again so that memory is not leaked when it is 47 /// done using this library. ClearMessages()48 void ClearMessages() { 49 message_stats_->Clear(); 50 if (message_store_) { 51 message_store_->ClearMessages(); 52 } 53 } 54 55 /// @return Whether the message handler's store has error messages or not. HasErrorMessages()56 bool HasErrorMessages() const { return GetErrorMessageCount() > 0; } 57 58 /// @return The number of error messages reported. GetErrorMessageCount()59 size_t GetErrorMessageCount() const { return message_stats_->error_count; } 60 61 /// @return The number of warning messages reported. GetWarningMessageCount()62 size_t GetWarningMessageCount() const { 63 return message_stats_->warning_count; 64 } 65 66 /// @return The number of status messages reported. GetStatusMessageCount()67 size_t GetStatusMessageCount() const { return message_stats_->status_count; } 68 69 /// @return The message stats object as a shared pointer. GetMessageStats()70 std::shared_ptr<MessageStats> GetMessageStats() const { 71 return message_stats_; 72 } 73 74 /// @return The vector of errors maintained by the message handler's store. GetMessages()75 std::vector<Message> GetMessages() const { 76 return message_store_ ? message_store_->GetMessages() 77 : std::vector<Message>(); 78 } 79 80 /// Reports an error or a status message. This function is called from library 81 /// code when it detects an error condition or wants to report status. If the 82 /// message type is Message::kStdLibError, then the current value of the 83 /// system's errno variable is used when the message is created. The message 84 /// is added to the messages vector and if the message writer is not null, its 85 /// WriteMessage function is called. 86 /// @param type The type of message. 87 /// @param text Text associated with the message. 88 void ReportMessage(Message::Type type, const std::string& text); 89 90 /// @param message The message to report. 91 void ReportMessage(const Message& message); 92 93 private: 94 /// The message writer used by ReportMessage, or null. 95 std::unique_ptr<MessageWriter> message_writer_; 96 97 /// The message store for saving messages for later, or null. 98 std::unique_ptr<MessageStore> message_store_; 99 100 /// The message stats for counting messages. 101 std::shared_ptr<MessageStats> message_stats_; 102 }; 103 104 } // namespace image_io 105 } // namespace photos_editing_formats 106 107 #endif // IMAGE_IO_BASE_MESSAGE_HANDLER_H_ // NOLINT 108