Browse Source

Trade: using `std::unique_ptr` when returning polymorphic types.

This requires inclusion of heavyweight <memory> header (~25k LOC), but
data import isn't something the user would want to do in every file, so
it hopefully won't hurt compilation times too much.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
df91532ed6
  1. 15
      src/Trade/AbstractImporter.cpp
  2. 22
      src/Trade/AbstractImporter.h

15
src/Trade/AbstractImporter.cpp

@ -28,11 +28,14 @@
#include <Containers/Array.h> #include <Containers/Array.h>
#include <Utility/Assert.h> #include <Utility/Assert.h>
#include "Trade/AbstractMaterialData.h"
#include "Trade/CameraData.h" #include "Trade/CameraData.h"
#include "Trade/ImageData.h" #include "Trade/ImageData.h"
#include "Trade/LightData.h" #include "Trade/LightData.h"
#include "Trade/MeshData2D.h" #include "Trade/MeshData2D.h"
#include "Trade/MeshData3D.h" #include "Trade/MeshData3D.h"
#include "Trade/ObjectData2D.h"
#include "Trade/ObjectData3D.h"
#include "Trade/SceneData.h" #include "Trade/SceneData.h"
#include "Trade/TextureData.h" #include "Trade/TextureData.h"
@ -209,13 +212,13 @@ std::string AbstractImporter::object2DName(const UnsignedInt id) {
std::string AbstractImporter::doObject2DName(UnsignedInt) { return {}; } std::string AbstractImporter::doObject2DName(UnsignedInt) { return {}; }
ObjectData2D* AbstractImporter::object2D(const UnsignedInt id) { std::unique_ptr<ObjectData2D> AbstractImporter::object2D(const UnsignedInt id) {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object2D(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object2D(): no file opened", {});
CORRADE_ASSERT(id < doObject2DCount(), "Trade::AbstractImporter::object2D(): index out of range", {}); CORRADE_ASSERT(id < doObject2DCount(), "Trade::AbstractImporter::object2D(): index out of range", {});
return doObject2D(id); return doObject2D(id);
} }
ObjectData2D* AbstractImporter::doObject2D(UnsignedInt) { return nullptr; } std::unique_ptr<ObjectData2D> AbstractImporter::doObject2D(UnsignedInt) { return nullptr; }
UnsignedInt AbstractImporter::object3DCount() const { UnsignedInt AbstractImporter::object3DCount() const {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object3DCount(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object3DCount(): no file opened", {});
@ -239,13 +242,13 @@ std::string AbstractImporter::object3DName(const UnsignedInt id) {
std::string AbstractImporter::doObject3DName(UnsignedInt) { return {}; } std::string AbstractImporter::doObject3DName(UnsignedInt) { return {}; }
ObjectData3D* AbstractImporter::object3D(const UnsignedInt id) { std::unique_ptr<ObjectData3D> AbstractImporter::object3D(const UnsignedInt id) {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object3D(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::object3D(): no file opened", {});
CORRADE_ASSERT(id < doObject3DCount(), "Trade::AbstractImporter::object3D(): index out of range", {}); CORRADE_ASSERT(id < doObject3DCount(), "Trade::AbstractImporter::object3D(): index out of range", {});
return doObject3D(id); return doObject3D(id);
} }
ObjectData3D* AbstractImporter::doObject3D(UnsignedInt) { return nullptr; } std::unique_ptr<ObjectData3D> AbstractImporter::doObject3D(UnsignedInt) { return nullptr; }
UnsignedInt AbstractImporter::mesh2DCount() const { UnsignedInt AbstractImporter::mesh2DCount() const {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::mesh2DCount(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::mesh2DCount(): no file opened", {});
@ -329,13 +332,13 @@ std::string AbstractImporter::materialName(const UnsignedInt id) {
std::string AbstractImporter::doMaterialName(UnsignedInt) { return {}; } std::string AbstractImporter::doMaterialName(UnsignedInt) { return {}; }
AbstractMaterialData* AbstractImporter::material(const UnsignedInt id) { std::unique_ptr<AbstractMaterialData> AbstractImporter::material(const UnsignedInt id) {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::material(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::material(): no file opened", {});
CORRADE_ASSERT(id < doMaterialCount(), "Trade::AbstractImporter::material(): index out of range", {}); CORRADE_ASSERT(id < doMaterialCount(), "Trade::AbstractImporter::material(): index out of range", {});
return doMaterial(id); return doMaterial(id);
} }
AbstractMaterialData* AbstractImporter::doMaterial(UnsignedInt) { return nullptr; } std::unique_ptr<AbstractMaterialData> AbstractImporter::doMaterial(UnsignedInt) { return nullptr; }
UnsignedInt AbstractImporter::textureCount() const { UnsignedInt AbstractImporter::textureCount() const {
CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::textureCount(): no file opened", {}); CORRADE_ASSERT(isOpened(), "Trade::AbstractImporter::textureCount(): no file opened", {});

22
src/Trade/AbstractImporter.h

@ -28,6 +28,7 @@
* @brief Class Magnum::Trade::AbstractImporter * @brief Class Magnum::Trade::AbstractImporter
*/ */
#include <memory>
#include <Containers/EnumSet.h> #include <Containers/EnumSet.h>
#include <PluginManager/AbstractPlugin.h> #include <PluginManager/AbstractPlugin.h>
@ -240,10 +241,9 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
* @brief Two-dimensional object * @brief Two-dimensional object
* @param id Object ID, from range [0, object2DCount()). * @param id Object ID, from range [0, object2DCount()).
* *
* Returns given object or `nullptr` if importing failed. Deleting the * Returns given object or `nullptr` if importing failed.
* data is user responsibility.
*/ */
ObjectData2D* object2D(UnsignedInt id); std::unique_ptr<ObjectData2D> object2D(UnsignedInt id);
/** @brief Three-dimensional object count */ /** @brief Three-dimensional object count */
UnsignedInt object3DCount() const; UnsignedInt object3DCount() const;
@ -268,10 +268,9 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
* @brief Three-dimensional object * @brief Three-dimensional object
* @param id Object ID, from range [0, object3DCount()). * @param id Object ID, from range [0, object3DCount()).
* *
* Returns given object or `nullptr` if importing failed. Deleting the * Returns given object or `nullptr` if importing failed.
* data is user responsibility.
*/ */
ObjectData3D* object3D(UnsignedInt id); std::unique_ptr<ObjectData3D> object3D(UnsignedInt id);
/** @brief Two-dimensional mesh count */ /** @brief Two-dimensional mesh count */
UnsignedInt mesh2DCount() const; UnsignedInt mesh2DCount() const;
@ -350,10 +349,9 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
* @brief Material * @brief Material
* @param id Material ID, from range [0, materialCount()). * @param id Material ID, from range [0, materialCount()).
* *
* Returns given material or `nullptr` if importing failed. Deleting * Returns given material or `nullptr` if importing failed.
* the data is user responsibility.
*/ */
AbstractMaterialData* material(UnsignedInt id); std::unique_ptr<AbstractMaterialData> material(UnsignedInt id);
/** @brief %Texture count */ /** @brief %Texture count */
UnsignedInt textureCount() const; UnsignedInt textureCount() const;
@ -539,7 +537,7 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
virtual std::string doObject2DName(UnsignedInt id); virtual std::string doObject2DName(UnsignedInt id);
/** @brief Implementation for object2D() */ /** @brief Implementation for object2D() */
virtual ObjectData2D* doObject2D(UnsignedInt id); virtual std::unique_ptr<ObjectData2D> doObject2D(UnsignedInt id);
/** @brief Implementation for object3DCount() */ /** @brief Implementation for object3DCount() */
virtual UnsignedInt doObject3DCount() const; virtual UnsignedInt doObject3DCount() const;
@ -551,7 +549,7 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
virtual std::string doObject3DName(UnsignedInt id); virtual std::string doObject3DName(UnsignedInt id);
/** @brief Implementation for object3D() */ /** @brief Implementation for object3D() */
virtual ObjectData3D* doObject3D(UnsignedInt id); virtual std::unique_ptr<ObjectData3D> doObject3D(UnsignedInt id);
/** @brief Implementation for mesh2DCount() */ /** @brief Implementation for mesh2DCount() */
virtual UnsignedInt doMesh2DCount() const; virtual UnsignedInt doMesh2DCount() const;
@ -587,7 +585,7 @@ class MAGNUM_EXPORT AbstractImporter: public PluginManager::AbstractPlugin {
virtual std::string doMaterialName(UnsignedInt id); virtual std::string doMaterialName(UnsignedInt id);
/** @brief Implementation for material() */ /** @brief Implementation for material() */
virtual AbstractMaterialData* doMaterial(UnsignedInt id); virtual std::unique_ptr<AbstractMaterialData> doMaterial(UnsignedInt id);
/** @brief Implementation for textureCount() */ /** @brief Implementation for textureCount() */
virtual UnsignedInt doTextureCount() const; virtual UnsignedInt doTextureCount() const;

Loading…
Cancel
Save