Skip to content

Commit

Permalink
ModelExporters are allowed to maintain a state
Browse files Browse the repository at this point in the history
Add virtual clone() method to work around the troubles
  • Loading branch information
codereader committed Jan 23, 2017
1 parent 12cd3b9 commit d02e322
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
12 changes: 11 additions & 1 deletion include/imodel.h
Expand Up @@ -112,6 +112,9 @@ class ModelNode
};
typedef std::shared_ptr<ModelNode> ModelNodePtr;

class IModelExporter;
typedef std::shared_ptr<IModelExporter> IModelExporterPtr;

/**
* Exporter Interface for models (meshes).
*/
Expand All @@ -121,6 +124,11 @@ class IModelExporter
virtual ~IModelExporter()
{}

// Virtual constructor idiom. Use this method to generate a new
// instance of the implementing subclass. This way the model format manager
// can create a fresh instance of this exporter on demand.
virtual IModelExporterPtr clone() = 0;

// Returns the uppercase file extension this exporter is suitable for
virtual const std::string& getExtension() const = 0;

Expand All @@ -130,11 +138,13 @@ class IModelExporter
// Export the model file to the given stream
virtual void exportToStream(std::ostream& stream) = 0;
};
typedef std::shared_ptr<IModelExporter> IModelExporterPtr;

/**
* Importer interface for models. An importer must be able
* to load a model (node) from the VFS.
* The importer instance shouldn't maintain an internal state,
* such that the same instance can be used to load several models,
* from different client code.
*/
class IModelImporter
{
Expand Down
5 changes: 5 additions & 0 deletions plugins/model/AseExporter.cpp
Expand Up @@ -12,6 +12,11 @@ namespace model
AseExporter::AseExporter()
{}

IModelExporterPtr AseExporter::clone()
{
return std::make_shared<AseExporter>();
}

const std::string& AseExporter::getExtension() const
{
static std::string _extension("ASE");
Expand Down
2 changes: 2 additions & 0 deletions plugins/model/AseExporter.h
Expand Up @@ -39,6 +39,8 @@ class AseExporter :
public:
AseExporter();

IModelExporterPtr clone() override;

// Returns the uppercase file extension this exporter is suitable for
const std::string& getExtension() const override;

Expand Down
3 changes: 2 additions & 1 deletion plugins/model/Makefile.am
Expand Up @@ -8,7 +8,8 @@ model_la_LDFLAGS = -module -avoid-version \
model_la_LIBADD = $(top_builddir)/libs/picomodel/libpicomodel.la \
$(top_builddir)/libs/math/libmath.la \
$(top_builddir)/libs/scene/libscenegraph.la
model_la_SOURCES = PicoModelNode.cpp \
model_la_SOURCES = AseExporter.cpp \
PicoModelNode.cpp \
RenderablePicoModel.cpp \
PicoModelLoader.cpp \
RenderablePicoSurface.cpp \
Expand Down
3 changes: 2 additions & 1 deletion radiant/model/ModelFormatManager.cpp
Expand Up @@ -102,7 +102,8 @@ IModelExporterPtr ModelFormatManager::getExporter(const std::string& extension)

ExporterMap::const_iterator found = _exporters.find(extensionUpper);

return found != _exporters.end() ? found->second : IModelExporterPtr();
// Return a cloned instance if we found a matching exporter
return found != _exporters.end() ? found->second->clone() : IModelExporterPtr();
}

module::StaticModule<ModelFormatManager> _staticModelFormatManagerModule;
Expand Down

0 comments on commit d02e322

Please sign in to comment.