/
StageFactory.hpp
174 lines (144 loc) · 6.75 KB
/
StageFactory.hpp
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
167
168
169
170
171
172
173
174
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#ifndef INCLUDED_STAGEFACTORY_HPP
#define INCLUDED_STAGEFACTORY_HPP
#include <pdal/pdal_internal.hpp>
#include <pdal/pdal_macros.hpp>
#include <pdal/Stage.hpp>
#include <pdal/Reader.hpp>
#include <pdal/Filter.hpp>
#include <pdal/MultiFilter.hpp>
#include <pdal/Writer.hpp>
#include <pdal/StageInfo.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <map>
#include <string>
namespace pdal
{
class Options;
// This class provides a mechanism for creating Stage objects using only a
// string (the stage type name) and an Options block.
//
// We keep a list of (stage type name, creation function) pairs, which
// acts as a registry of creator functions. The list is initialized with
// the core stages we know about (I wish C++ had anonymous functions.).
// We allow the user to add his own "external" drivers to the registry list
// as well.
//
// We use 4 different functions for each kind of operation, since we have
// 4 types of derived classes from Stage and they all have slightly different
// parameters. That makes it kinda messy.
class PDAL_DLL StageFactory
{
public:
typedef Reader* ReaderCreator(const Options&);
typedef Filter* FilterCreator(Stage& prevStage, const Options&);
typedef MultiFilter* MultiFilterCreator(const std::vector<Stage*>& prevStages, const Options&);
typedef Writer* WriterCreator(Stage& prevStage, const Options&);
typedef std::map<std::string, ReaderCreator*> ReaderCreatorList;
typedef std::map<std::string, FilterCreator*> FilterCreatorList;
typedef std::map<std::string, MultiFilterCreator*> MultiFilterCreatorList;
typedef std::map<std::string, WriterCreator*> WriterCreatorList;
public:
StageFactory();
// infer the driver to use based on filename extension
// returns "" if no driver found
//
// this may also add on an option to pass to the driver, such as the filename
static std::string inferReaderDriver(const std::string& filename, pdal::Options& options);
// infer the driver to use based on filename extension
// returns "" if no driver found
static std::string inferWriterDriver(const std::string& filename);
// modify options based upon expectations implicit in a given filename
// e.g. output files ending in .laz should be compressed
static void inferWriterOptionsChanges(const std::string& filename,
pdal::Options& options);
Reader* createReader(const std::string& type, const Options& options);
Filter* createFilter(const std::string& type, Stage& prevStage, const Options& options);
MultiFilter* createMultiFilter(const std::string& type, const std::vector<Stage*>& prevStages, const Options& options);
Writer* createWriter(const std::string& type, Stage& prevStage, const Options& options);
void registerReader(const std::string& type, ReaderCreator* f);
void registerFilter(const std::string& type, FilterCreator* f);
void registerMultiFilter(const std::string& type, MultiFilterCreator* f);
void registerWriter(const std::string& type, WriterCreator* f);
void loadPlugins();
void registerPlugin(std::string const& filename);
std::map<std::string, pdal::StageInfo> const& getStageInfos() const;
template<class T> void registerDriverInfo();
std::string toRST(std::string driverName="") const;
private:
// callers take ownership of returned stages
ReaderCreator* getReaderCreator(const std::string& type) const;
FilterCreator* getFilterCreator(const std::string& type) const;
MultiFilterCreator* getMultiFilterCreator(const std::string& type) const;
WriterCreator* getWriterCreator(const std::string& type) const;
void registerKnownReaders();
void registerKnownFilters();
void registerKnownMultiFilters();
void registerKnownWriters();
// these are the "registries" of the factory creator functions
ReaderCreatorList m_readerCreators;
FilterCreatorList m_filterCreators;
MultiFilterCreatorList m_multifilterCreators;
WriterCreatorList m_writerCreators;
// driver name + driver description
std::map<std::string, pdal::StageInfo> m_driver_info;
StageFactory& operator=(const StageFactory&); // not implemented
StageFactory(const StageFactory&); // not implemented
};
template <class T>
inline void StageFactory::registerDriverInfo()
{
pdal::StageInfo info(T::s_getName(), T::s_getDescription());
info.setInfoLink(T::s_getInfoLink());
info.setIsEnabled(T::s_isEnabled());
std::vector<Dimension> dimensions = T::getDefaultDimensions();
for (std::vector<Dimension>::const_iterator i = dimensions.begin();
i != dimensions.end();
++i)
{
info.addProvidedDimension(*i);
}
std::vector<Option> options = T::getDefaultOptions().getOptions();
for (std::vector<Option>::const_iterator i = options.begin();
i != options.end();
++i)
{
info.addProvidedOption(*i);
}
m_driver_info.insert(std::pair<std::string, pdal::StageInfo>(T::s_getName(), info));
}
} // namespace pdal
#endif