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