diff options
Diffstat (limited to 'includes/image_io/base')
-rw-r--r-- | includes/image_io/base/istream_data_source.h | 30 | ||||
-rw-r--r-- | includes/image_io/base/istream_ref_data_source.h | 46 | ||||
-rw-r--r-- | includes/image_io/base/message.h | 14 | ||||
-rw-r--r-- | includes/image_io/base/message_handler.h | 44 | ||||
-rw-r--r-- | includes/image_io/base/message_stats.h | 21 | ||||
-rw-r--r-- | includes/image_io/base/message_store.h | 2 | ||||
-rw-r--r-- | includes/image_io/base/ostream_data_destination.h | 45 | ||||
-rw-r--r-- | includes/image_io/base/ostream_ref_data_destination.h | 66 | ||||
-rw-r--r-- | includes/image_io/base/types.h | 8 | ||||
-rw-r--r-- | includes/image_io/base/validated_number.h | 38 |
10 files changed, 232 insertions, 82 deletions
diff --git a/includes/image_io/base/istream_data_source.h b/includes/image_io/base/istream_data_source.h index 4564e14..9b87542 100644 --- a/includes/image_io/base/istream_data_source.h +++ b/includes/image_io/base/istream_data_source.h @@ -1,46 +1,28 @@ #ifndef IMAGE_IO_BASE_ISTREAM_DATA_SOURCE_H_ // NOLINT #define IMAGE_IO_BASE_ISTREAM_DATA_SOURCE_H_ // NOLINT -#include <iostream> #include <memory> #include <utility> -#include "image_io/base/data_source.h" +#include "image_io/base/istream_ref_data_source.h" namespace photos_editing_formats { namespace image_io { -/// A DataSource that obtains data from an istream. -class IStreamDataSource : public DataSource { +/// A DataSource that obtains data from an istream that it owns. +class IStreamDataSource : public IStreamRefDataSource { public: /// Constructs an IStreamDataSource using the given istream. /// @param istram_ptr The istream from which to read. explicit IStreamDataSource(std::unique_ptr<std::istream> istream_ptr) - : istream_(std::move(istream_ptr)) {} - - void Reset() override; - std::shared_ptr<DataSegment> GetDataSegment(size_t begin, - size_t min_size) override; - TransferDataResult TransferData(const DataRange& data_range, size_t best_size, - DataDestination* data_destination) override; - - private: - /// The worker function to create a DataSegment and fill it with the given - /// number of bytes read from the istream, starting at the given location. - /// @param begin The location in the istream at which to start reading. - /// @param count The number of bytes to read. - /// @return A DataSegment pointer, or nullptr if the read failed. - std::shared_ptr<DataSegment> Read(size_t begin, size_t count); + : IStreamRefDataSource(*istream_ptr), istream_(std::move(istream_ptr)) {} private: - /// The istream from which to read. + /// The istream that is owned by this data source. std::unique_ptr<std::istream> istream_; - - /// The current data segment that was read in the GetDataSegment() function. - std::shared_ptr<DataSegment> current_data_segment_; }; } // namespace image_io } // namespace photos_editing_formats -#endif // IMAGE_IO_BASE_ISTREAM_DATA_SOURCE_H_ // NOLINT +#endif // IMAGE_IO_BASE_ISTREAM_DATA_SOURCE_H_ // NOLINT diff --git a/includes/image_io/base/istream_ref_data_source.h b/includes/image_io/base/istream_ref_data_source.h new file mode 100644 index 0000000..2460bb1 --- /dev/null +++ b/includes/image_io/base/istream_ref_data_source.h @@ -0,0 +1,46 @@ +#ifndef IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT +#define IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT + +#include <iostream> + +#include "image_io/base/data_source.h" + +namespace photos_editing_formats { +namespace image_io { + +/// A DataSource that obtains data from an istream held as a reference. +class IStreamRefDataSource : public DataSource { + public: + /// Constructs an IStreamDataSource using the given istream. + /// @param istream_ref The istream from which to read. + explicit IStreamRefDataSource(std::istream& istream_ref) + : istream_ref_(istream_ref) {} + IStreamRefDataSource(const IStreamRefDataSource&) = delete; + IStreamRefDataSource& operator=(const IStreamRefDataSource&) = delete; + + void Reset() override; + std::shared_ptr<DataSegment> GetDataSegment(size_t begin, + size_t min_size) override; + TransferDataResult TransferData(const DataRange& data_range, size_t best_size, + DataDestination* data_destination) override; + + private: + /// The worker function to create a DataSegment and fill it with the given + /// number of bytes read from the istream, starting at the given location. + /// @param begin The location in the istream at which to start reading. + /// @param count The number of bytes to read. + /// @return A DataSegment pointer, or nullptr if the read failed. + std::shared_ptr<DataSegment> Read(size_t begin, size_t count); + + private: + /// The istream from which to read. + std::istream& istream_ref_; + + /// The current data segment that was read in the GetDataSegment() function. + std::shared_ptr<DataSegment> current_data_segment_; +}; + +} // namespace image_io +} // namespace photos_editing_formats + +#endif // IMAGE_IO_BASE_ISTREAM_REF_DATA_SOURCE_H_ // NOLINT diff --git a/includes/image_io/base/message.h b/includes/image_io/base/message.h index 8c225d8..fef2fd6 100644 --- a/includes/image_io/base/message.h +++ b/includes/image_io/base/message.h @@ -15,6 +15,9 @@ class Message { /// A Status message. kStatus, + /// A Warning message. + kWarning, + /// An error from the stdlib was detected. The std::errno variable can be /// used to programmatically decide what to do, or use the std::strerror /// function to get a string description of the error. @@ -66,6 +69,17 @@ class Message { /// @return The text of the message. const std::string& GetText() const { return text_; } + /// @return Whether the message is an error message. + bool IsError() const { + return type_ != Message::kStatus && type_ != Message::kWarning; + } + + /// @return Whether the message is a warning message. + bool IsWarning() const { return type_ == Message::kWarning; } + + /// @return Whether the message is a status message. + bool IsStatus() const { return type_ == Message::kStatus; } + private: /// The type of message. Type type_; diff --git a/includes/image_io/base/message_handler.h b/includes/image_io/base/message_handler.h index dc33679..b60a593 100644 --- a/includes/image_io/base/message_handler.h +++ b/includes/image_io/base/message_handler.h @@ -5,6 +5,7 @@ #include <vector> #include "image_io/base/message.h" +#include "image_io/base/message_stats.h" #include "image_io/base/message_store.h" #include "image_io/base/message_writer.h" @@ -15,20 +16,9 @@ namespace image_io { /// to report status and error conditions. class MessageHandler { public: - /// Initializes the MessageHandler for client use. Multithread applications - /// might find this function useful to call in their initialization section, - /// to guarentee that threads will not create race conditions when calling the - /// Get function for the first time. - static void Init(std::unique_ptr<MessageWriter> message_writer, - std::unique_ptr<MessageStore> message_store); - - /// This function is thread-safe as long as the Init() function is called in - /// non-multiple-threaded startup code; if the Init() fucnction was not called - /// there may be race conditions that causes the message handler returned from - /// Get() called in one thread to be different from that returned by the call - /// in a different thread. - /// @return The message handler used by the code in this library. - static MessageHandler* Get(); + /// The default constructor for MessageHandler creates a MessageWriter and + /// VectorMessageStore for handling writing and storing messages. + MessageHandler(); /// Sets the message writer to use when ReportMessage() is called. If client /// code does not call this function, the MessageHandler returned by the Get() @@ -55,14 +45,29 @@ class MessageHandler { /// should call this function again so that memory is not leaked when it is /// done using this library. void ClearMessages() { + message_stats_->Clear(); if (message_store_) { message_store_->ClearMessages(); } } /// @return Whether the message handler's store has error messages or not. - bool HasErrorMessages() const { - return message_store_ ? message_store_->HasErrorMessages() : false; + bool HasErrorMessages() const { return GetErrorMessageCount() > 0; } + + /// @return The number of error messages reported. + size_t GetErrorMessageCount() const { return message_stats_->error_count; } + + /// @return The number of warning messages reported. + size_t GetWarningMessageCount() const { + return message_stats_->warning_count; + } + + /// @return The number of status messages reported. + size_t GetStatusMessageCount() const { return message_stats_->status_count; } + + /// @return The message stats object as a shared pointer. + std::shared_ptr<MessageStats> GetMessageStats() const { + return message_stats_; } /// @return The vector of errors maintained by the message handler's store. @@ -85,15 +90,14 @@ class MessageHandler { void ReportMessage(const Message& message); private: - MessageHandler() = default; - ~MessageHandler(); - - private: /// The message writer used by ReportMessage, or null. std::unique_ptr<MessageWriter> message_writer_; /// The message store for saving messages for later, or null. std::unique_ptr<MessageStore> message_store_; + + /// The message stats for counting messages. + std::shared_ptr<MessageStats> message_stats_; }; } // namespace image_io diff --git a/includes/image_io/base/message_stats.h b/includes/image_io/base/message_stats.h new file mode 100644 index 0000000..6b338f0 --- /dev/null +++ b/includes/image_io/base/message_stats.h @@ -0,0 +1,21 @@ +#ifndef IMAGE_IO_BASE_MESSAGE_STATS_H_ // NOLINT +#define IMAGE_IO_BASE_MESSAGE_STATS_H_ // NOLINT + +#include "image_io/base/types.h" + +namespace photos_editing_formats { +namespace image_io { + +/// A structure for holding message stats. +struct MessageStats { + MessageStats() { Clear(); } + void Clear() { error_count = warning_count = status_count = 0; } + size_t error_count; + size_t warning_count; + size_t status_count; +}; + +} // namespace image_io +} // namespace photos_editing_formats + +#endif // IMAGE_IO_BASE_MESSAGE_STATS_H_ // NOLINT diff --git a/includes/image_io/base/message_store.h b/includes/image_io/base/message_store.h index 1d492f1..8db0b9f 100644 --- a/includes/image_io/base/message_store.h +++ b/includes/image_io/base/message_store.h @@ -61,7 +61,7 @@ class ErrorFlagMessageStore : public MessageStore { ErrorFlagMessageStore() : has_error_(false) {} void ClearMessages() override { has_error_ = false; } void AddMessage(const Message& message) override { - if (message.GetType() != Message::kStatus) { + if (message.IsError()) { has_error_ = true; } } diff --git a/includes/image_io/base/ostream_data_destination.h b/includes/image_io/base/ostream_data_destination.h index 15a1155..d0cf04c 100644 --- a/includes/image_io/base/ostream_data_destination.h +++ b/includes/image_io/base/ostream_data_destination.h @@ -1,58 +1,31 @@ #ifndef IMAGE_IO_BASE_OSTREAM_DATA_DESTINATION_H_ // NOLINT #define IMAGE_IO_BASE_OSTREAM_DATA_DESTINATION_H_ // NOLINT -#include <iostream> #include <memory> -#include <string> #include <utility> -#include "image_io/base/data_destination.h" +#include "image_io/base/ostream_ref_data_destination.h" namespace photos_editing_formats { namespace image_io { /// A DataDestination that writes its output to an ostream. -class OStreamDataDestination : public DataDestination { +class OStreamDataDestination : public OStreamRefDataDestination { public: /// Constructs an OStreamDataDestination using the given ostream. /// @param ostream_ptr The ostream to which data is written. - explicit OStreamDataDestination(std::unique_ptr<std::ostream> ostream_ptr) - : ostream_(std::move(ostream_ptr)), - bytes_transferred_(0), - has_error_(false) {} - - /// @param name A name to associate with the ostream. Used for error messages. - void SetName(const std::string& name) { name_ = name; } - - /// @return The name associated with the ostream. - const std::string& GetName() const { return name_; } - - /// @return The number of bytes written to the ostream. - size_t GetBytesTransferred() const override { return bytes_transferred_; } - - /// @return True if errors were encountered while writing to the ostream. - bool HasError() const { return has_error_; } - - void StartTransfer() override; - TransferStatus Transfer(const DataRange& transfer_range, - const DataSegment& data_segment) override; - void FinishTransfer() override; + /// @param message_handler An option message handler for writing messages. + OStreamDataDestination(std::unique_ptr<std::ostream> ostream_ptr, + MessageHandler* message_handler) + : OStreamRefDataDestination(*ostream_ptr, message_handler), + ostream_(std::move(ostream_ptr)) {} private: - /// The ostream written to. + /// The ostream that is owned by this data destination. std::unique_ptr<std::ostream> ostream_; - - /// The number of bytes written so far. - size_t bytes_transferred_; - - /// A (file) name to associate with the ostream, used with error messages. - std::string name_; - - /// If true indicates an error has occurred writing to the ostream. - bool has_error_; }; } // namespace image_io } // namespace photos_editing_formats -#endif // IMAGE_IO_BASE_OSTREAM_DATA_DESTINATION_H_ // NOLINT +#endif // IMAGE_IO_BASE_OSTREAM_DATA_DESTINATION_H_ // NOLINT diff --git a/includes/image_io/base/ostream_ref_data_destination.h b/includes/image_io/base/ostream_ref_data_destination.h new file mode 100644 index 0000000..266e2be --- /dev/null +++ b/includes/image_io/base/ostream_ref_data_destination.h @@ -0,0 +1,66 @@ +#ifndef IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_ // NOLINT +#define IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_ // NOLINT + +#include <iostream> +#include <string> + +#include "image_io/base/data_destination.h" +#include "image_io/base/message_handler.h" + +namespace photos_editing_formats { +namespace image_io { + +/// A DataDestination that writes its output to an ostream held as a reference. +class OStreamRefDataDestination : public DataDestination { + public: + /// Constructs an OStreamDataDestination using the given ostream. + /// @param ostream_ref The ostream to which data is written. + /// @param message_handler An option message handler for writing messages. + OStreamRefDataDestination(std::ostream& ostream_ref, + MessageHandler* message_handler) + : ostream_ref_(ostream_ref), + message_handler_(message_handler), + bytes_transferred_(0), + has_error_(false) {} + OStreamRefDataDestination(const OStreamRefDataDestination&) = delete; + OStreamRefDataDestination& operator=(const OStreamRefDataDestination&) = + delete; + + /// @param name A name to associate with the ostream. Used for error messages. + void SetName(const std::string& name) { name_ = name; } + + /// @return The name associated with the ostream. + const std::string& GetName() const { return name_; } + + /// @return The number of bytes written to the ostream. + size_t GetBytesTransferred() const override { return bytes_transferred_; } + + /// @return True if errors were encountered while writing to the ostream. + bool HasError() const { return has_error_; } + + void StartTransfer() override; + TransferStatus Transfer(const DataRange& transfer_range, + const DataSegment& data_segment) override; + void FinishTransfer() override; + + private: + /// The ostream written to. + std::ostream& ostream_ref_; + + /// An optional message handler to write messages to. + MessageHandler* message_handler_; + + /// The number of bytes written so far. + size_t bytes_transferred_; + + /// A (file) name to associate with the ostream, used with error messages. + std::string name_; + + /// If true indicates an error has occurred writing to the ostream. + bool has_error_; +}; + +} // namespace image_io +} // namespace photos_editing_formats + +#endif // IMAGE_IO_BASE_OSTREAM_REF_DATA_DESTINATION_H_ // NOLINT diff --git a/includes/image_io/base/types.h b/includes/image_io/base/types.h index aaa88b3..ed49350 100644 --- a/includes/image_io/base/types.h +++ b/includes/image_io/base/types.h @@ -7,8 +7,14 @@ namespace photos_editing_formats { namespace image_io { -/// Byte is the noumenon unit of data. +/// The various integer and byte types used in this package. using Byte = std::uint8_t; +using Int32 = std::int32_t; +using Int64 = std::int64_t; +using UInt8 = std::uint8_t; +using UInt16 = std::uint16_t; +using UInt32 = std::uint32_t; +using UInt64 = std::uint64_t; /// A Byte value and a validity flag. struct ValidatedByte { diff --git a/includes/image_io/base/validated_number.h b/includes/image_io/base/validated_number.h new file mode 100644 index 0000000..ead5ab3 --- /dev/null +++ b/includes/image_io/base/validated_number.h @@ -0,0 +1,38 @@ +#ifndef IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT +#define IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT + +#include <sstream> +#include <string> + +namespace photos_editing_formats { +namespace image_io { + +template <class T> +struct ValidatedNumber { + ValidatedNumber() : ValidatedNumber(T(), false) {} + ValidatedNumber(const T& value_, bool is_valid_) + : value(value_), is_valid(is_valid_) {} + using value_type = T; + T value; + bool is_valid; +}; + +template <class T> +ValidatedNumber<T> GetValidatedNumber(const std::string& str) { + std::stringstream ss(str); + ValidatedNumber<T> result; + ss >> result.value; + if (!ss.fail()) { + std::string extra; + ss >> extra; + if (extra.empty()) { + result.is_valid = true; + } + } + return result; +} + +} // namespace image_io +} // namespace photos_editing_formats + +#endif // IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT |