-
Notifications
You must be signed in to change notification settings - Fork 298
/
SceneLoaderFactory.h
150 lines (112 loc) · 6.08 KB
/
SceneLoaderFactory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_SIMULATION_SCENELOADERFACTORY_H
#define SOFA_SIMULATION_SCENELOADERFACTORY_H
#include <set>
#include <vector>
#include <sofa/simulation/config.h>
#include <sofa/simulation/fwd.h>
#include <string>
namespace sofa::simulation
{
/**
* \brief Main class used to register scene file loaders
*
* It uses the Factory design pattern, where each class is registered in a map,
* and dynamically retrieved given the type name.
*
*/
/// Abstract interface of a scene loader
class SOFA_SIMULATION_CORE_API SceneLoader
{
public:
typedef std::vector<std::string> ExtensionList;
/// Pre-loading check
virtual bool canLoadFileName(const char *filename);
/// Pre-saving check
virtual bool canWriteFileName(const char *filename);
virtual bool canLoadFileExtension(const char *extension) = 0;
virtual bool canWriteFileExtension(const char * /*extension*/) { return false; }
/// load the file
sofa::simulation::NodeSPtr load(const std::string& filename, bool reload = false, const std::vector<std::string>& sceneArgs = std::vector<std::string>(0));
virtual sofa::simulation::NodeSPtr doLoad(const std::string& filename, const std::vector<std::string>& sceneArgs) = 0;
/// write scene graph in the file
virtual void write(sofa::simulation::Node* /*node*/, const char * /*filename*/) {}
/// get the file type description
virtual std::string getFileTypeDesc() = 0;
/// get the list of file extensions
virtual void getExtensionList(ExtensionList* list) = 0;
/// Write into a ostream the syntax to add a RequiredPlugin component in the scene file. The
/// syntax depends on the file format, hence the SceneLoader. The function returns true if the
/// derived SceneLoader implements this function, false otherwise.
virtual bool syntaxForAddingRequiredPlugin(const std::string& pluginName,
const std::vector<std::string>& listComponents,
std::ostream& ss, sofa::simulation::Node* nodeWhereAdded);
/// to be able to inform when a scene is loaded
struct SOFA_SIMULATION_CORE_API Listener
{
virtual void rightBeforeLoadingScene(SceneLoader* sceneLoader); ///< callback called just before loading the scene file
virtual void rightAfterLoadingScene(sofa::simulation::NodeSPtr, SceneLoader* sceneLoader); ///< callback called just after loading the scene file
virtual void rightBeforeReloadingScene(SceneLoader* sceneLoader); ///< callback called just before reloading the scene file
virtual void rightAfterReloadingScene(sofa::simulation::NodeSPtr root, SceneLoader* sceneLoader); ///< callback called just after reloading the scene file
};
/// adding a listener
static void addListener( Listener* l );
/// removing a listener
static void removeListener( Listener* l );
protected:
/// the list of listeners
typedef std::set<Listener*> Listeners;
static Listeners s_listeners;
static void notifyLoadingSceneBefore(SceneLoader* sceneLoader);
static void notifyReloadingSceneBefore(SceneLoader* sceneLoader);
static void notifyLoadingSceneAfter(sofa::simulation::NodeSPtr node, SceneLoader* sceneLoader);
static void notifyReloadingSceneAfter(sofa::simulation::NodeSPtr node, SceneLoader* sceneLoader);
};
class SOFA_SIMULATION_CORE_API SceneLoaderFactory
{
public:
typedef std::vector<SceneLoader*> SceneLoaderList;
/// Get the ObjectFactory singleton instance
static SceneLoaderFactory* getInstance();
protected:
/// Main class registry
SceneLoaderList registry;
public:
/// Get an entry given a file extension
SceneLoader* getEntryFileExtension(std::string extension);
/// Get an entry given a file name
SceneLoader* getEntryFileName(std::string filename);
/// Get an exporter entry given a file extension
SceneLoader* getExporterEntryFileExtension(std::string extension);
/// Get an exporter entry given a file name
SceneLoader* getExporterEntryFileName(std::string filename);
/// Add a scene loader
SceneLoader* addEntry(SceneLoader *loader);
/// Get the list of loaders
SceneLoaderList* getEntries() {return ®istry;}
/// Get the list of supported extension. It returns a vector of string
/// but it is RVO optimized so the overhead is reduced.
std::vector<std::string> extensions() ;
};
} // namespace sofa::simulation
#endif // SOFA_SIMULATION_SCENELOADERFACTORY_H