From d6f5560262bade9660457413fc1703d71e7e74d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 29 Mar 2013 14:26:39 +0100 Subject: [PATCH] Trade: got rid of opening input streams in AbstractImporter. Now it is possible to open either pure data arrays or arrays specified with filename. The function calls might be ambiguous now (i.e. open("image.tga") would open data array instead of handling it as filename), renamed them to avoid that. Default implementations now fire an assertion. Bumped plugin interface string version as this is pretty incompatible change. --- src/Trade/AbstractImporter.cpp | 20 +++++++------- src/Trade/AbstractImporter.h | 48 +++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/Trade/AbstractImporter.cpp b/src/Trade/AbstractImporter.cpp index 5d7dcf24d..46a6abc8e 100644 --- a/src/Trade/AbstractImporter.cpp +++ b/src/Trade/AbstractImporter.cpp @@ -24,9 +24,7 @@ #include "AbstractImporter.h" -#include - -#include "Magnum.h" +#include namespace Magnum { namespace Trade { @@ -34,14 +32,18 @@ AbstractImporter::AbstractImporter() = default; AbstractImporter::AbstractImporter(Corrade::PluginManager::AbstractPluginManager* manager, std::string plugin): AbstractPlugin(manager, std::move(plugin)) {} -bool AbstractImporter::open(const std::string&) { - Error() << plugin() << "doesn't support opening files"; - return false; +bool AbstractImporter::openData(const void* const, const std::size_t) { + CORRADE_ASSERT(features() & Feature::OpenData, + "Trade::AbstractImporter::openData(): feature advertised but not implemented", nullptr); + + CORRADE_ASSERT(false, "Trade::AbstractImporter::openData(): feature not implemented", nullptr); } -bool AbstractImporter::open(std::istream&) { - Error() << plugin() << "doesn't support opening input streams"; - return false; +bool AbstractImporter::openFile(const std::string&) { + CORRADE_ASSERT(features() & Feature::OpenFile, + "Trade::AbstractImporter::openFile(): feature advertised but not implemented", nullptr); + + CORRADE_ASSERT(false, "Trade::AbstractImporter::openFile(): feature not implemented", nullptr); } }} diff --git a/src/Trade/AbstractImporter.h b/src/Trade/AbstractImporter.h index 1c6f1f754..0af1e45db 100644 --- a/src/Trade/AbstractImporter.h +++ b/src/Trade/AbstractImporter.h @@ -54,7 +54,7 @@ some data. This is obviously not the case for single-data formats like images, as the file contains all data user wants to import. */ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::AbstractPlugin { - PLUGIN_INTERFACE("cz.mosra.magnum.Trade.AbstractImporter/0.2") + PLUGIN_INTERFACE("cz.mosra.magnum.Trade.AbstractImporter/0.2.1") public: /** @@ -62,13 +62,13 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::AbstractPlu * * @see Features, features() */ - enum class Feature { - OpenFile = 0x01, /**< Can open files specified by filename */ - OpenStream = 0x02 /**< Can open files from input streams */ + enum class Feature: UnsignedByte { + OpenData = 1 << 0, /**< Opening files from raw data */ + OpenFile = 1 << 1 /**< Opening files specified by filename */ }; /** @brief Set of features supported by this importer */ - typedef Corrade::Containers::EnumSet Features; + typedef Corrade::Containers::EnumSet Features; /** @brief Default constructor */ explicit AbstractImporter(); @@ -80,26 +80,38 @@ class MAGNUM_EXPORT AbstractImporter: public Corrade::PluginManager::AbstractPlu virtual Features features() const = 0; /** - * @brief Open file - * @param filename Filename - * @return Whether the file was successfully opened + * @brief Open raw data + * @param data Data + * @param size Data size * * Closes previous file, if it was opened, and tries to open given - * file. See also @ref Feature "Feature::OpenFile". Default - * implementation prints message to error output and returns false. + * file. Available only if @ref Feature "Feature::OpenData" is + * supported. Returns `true` on success, `false` otherwise. + * @see features(), openFile() */ - virtual bool open(const std::string& filename); + virtual bool openData(const void* const data, const std::size_t size); /** - * @brief Open stream - * @param in Input stream - * @return Whether the file was successfully opened + * @brief Open raw data + * @param data Data * - * See also open(const std::string&), @ref Feature - * "Feature::OpenStream". Default implementation prints message to - * error output and returns false. + * Convenience alternative to above function useful when array size is + * known at compile-time. + */ + template inline bool openData(const T(&data)[size]) { + return openData(data, size*sizeof(T)); + } + + /** + * @brief Open file + * @param filename Filename + * + * Closes previous file, if it was opened, and tries to open given + * file. Available only if @ref Feature "Feature::OpenFile" is + * supported. Returns `true` on success, `false` otherwise. + * @see features(), openData() */ - virtual bool open(std::istream& in); + virtual bool openFile(const std::string& filename); /** @brief Close file */ virtual void close() = 0;