-
-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathfactory.cxx.in
More file actions
166 lines (144 loc) · 4.6 KB
/
Copy pathfactory.cxx.in
File metadata and controls
166 lines (144 loc) · 4.6 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "factory.h"
#include "log.h"
#include <vtkMemoryResourceStream.h>
// clang-format off
${F3D_STATIC_PLUGIN_EXTERN}
// clang-format on
namespace f3d
{
namespace
{
template<typename F>
reader* pickReader(const std::vector<plugin*>& plugins, std::optional<std::string> forceReader,
file_availability& availability, F&& isValid)
{
int bestScore = -1;
reader* bestReader = nullptr;
for (const auto* plugin : plugins)
{
for (const auto& reader : plugin->getReaders())
{
if (forceReader)
{
if (reader->getName() == *forceReader)
{
availability = file_availability::SUPPORTED;
return reader.get();
}
}
else if (reader->getScore() > bestScore && isValid(reader.get()))
{
bestScore = reader->getScore();
bestReader = reader.get();
}
}
}
return bestReader;
}
}
//----------------------------------------------------------------------------
factory* factory::instance()
{
static factory instance;
return &instance;
}
//----------------------------------------------------------------------------
factory::factory()
{
// clang-format off
${F3D_STATIC_PLUGIN_MAP}
// clang-format on
}
//----------------------------------------------------------------------------
const std::vector<plugin*>& factory::getPlugins()
{
return this->Plugins;
}
//----------------------------------------------------------------------------
factory::plugin_initializer_t factory::getStaticInitializer(const std::string& pluginName)
{
auto it = this->StaticPluginInitializers.find(pluginName);
if (it != this->StaticPluginInitializers.end())
{
return it->second;
}
return nullptr;
}
//----------------------------------------------------------------------------
reader* factory::getReader(const std::string& fileName, std::optional<std::string> forceReader,
const std::optional<bool> skipContentCheck, file_availability& availability)
{
return f3d::pickReader(this->Plugins, forceReader, availability, [&](const reader* reader)
{ return reader->canRead(fileName, skipContentCheck, availability); });
}
//----------------------------------------------------------------------------
reader* factory::getReader(const std::byte* buffer, std::size_t size,
std::optional<std::string> forceReader, const std::optional<bool> skipContentCheck)
{
vtkNew<vtkMemoryResourceStream> stream;
stream->SetBuffer(buffer, size);
file_availability availability;
return f3d::pickReader(this->Plugins, forceReader, availability,
[&](const reader* reader) { return reader->supportsStream() && reader->canRead(stream); });
}
//----------------------------------------------------------------------------
bool factory::setReaderOption(const std::string& name, const std::string& value)
{
// Set the reader option on the first reader that accepts it
return std::any_of(this->Plugins.begin(), this->Plugins.end(),
[&](const f3d::plugin* plugin)
{
const auto& readers = plugin->getReaders();
return std::any_of(readers.begin(), readers.end(),
[&](const auto& reader) { return reader->setReaderOption(name, value); });
});
}
//----------------------------------------------------------------------------
std::vector<std::string> factory::getAllReaderOptionNames()
{
std::vector<std::string> names;
for (const f3d::plugin* plugin : this->Plugins)
{
for (const auto& reader : plugin->getReaders())
{
auto readerNames = reader->getAllReaderOptionNames();
names.insert(names.end(), readerNames.begin(), readerNames.end());
}
}
return names;
}
//----------------------------------------------------------------------------
void factory::load(plugin* plug)
{
if (!this->registerOnce(plug))
{
log::debug("A plugin named \"" + plug->getName() + "\" is already registered.");
}
}
//----------------------------------------------------------------------------
void factory::autoload()
{
for (const auto& [str, init] : this->StaticPluginInitializers)
{
this->registerOnce(init());
}
}
//----------------------------------------------------------------------------
bool factory::registerOnce(plugin* plug)
{
if (std::find(this->Plugins.begin(), this->Plugins.end(), plug) == this->Plugins.end())
{
this->Plugins.push_back(plug);
log::debug("Loading plugin \"" + plug->getName() + "\"");
log::debug(" Version: " + plug->getVersion());
log::debug(" Description: " + plug->getDescription());
log::debug(" Readers:");
for (const auto& read : plug->getReaders())
{
log::debug(" " + read->getLongDescription());
}
return true;
}
return false;
}
}