soma-io
5.1.2
|
Link mechanism between a data source or file and an algorithm operating on arbitrary data types. More...
#include <soma-io/io/readeralgorithm.h>
Public Types | |
typedef bool(* | ProcFunc) (ReaderAlgorithm &, carto::Object header, carto::rc_ptr< DataSource > source) |
Algorithm function type. More... | |
Public Member Functions | |
ReaderAlgorithm (const std::string &algoname) | |
virtual | ~ReaderAlgorithm () |
void | registerAlgorithmType (const std::string &objectType, ProcFunc procFunc) |
Registers the algo function to call on a given object type (just fills the map) More... | |
bool | execute (const std::string &filename) |
Executes the algo on the object type found in the given file. More... | |
bool | execute (carto::rc_ptr< DataSource > stream) |
same as above but uses a DataSource as input More... | |
bool | execute (carto::Object header, carto::rc_ptr< DataSource > source) |
Same as above but the header has already been read (or hand-made to fake it!) More... | |
bool | execute (carto::rc_ptr< DataSourceInfo > dsi) |
same as above but uses DataSourceInfo as input. More... | |
const std::map< std::string, ProcFunc > & | algorithmTypes () const |
Query registered process types. More... | |
![]() | |
Algorithm (const std::string &name) | |
Protected Attributes | |
std::map< std::string, ProcFunc > | _execs |
Additional Inherited Members | |
![]() | |
ParameterModifier< T > | inputParameter (T &ref, const std::string &name, const std::string &documentation) |
ParameterModifier< T > | outputParameter (T &ref, const std::string &name, const std::string &documentation) |
Link mechanism between a data source or file and an algorithm operating on arbitrary data types.
ReaderAlgorithm solves the data-type problem for any Cartograph commandline algorithm. It allows to hide both the file format of processed data (using DataSourceInfo) and the data type dependency. ReaderAlgorithm allows to call an algorithm operation function depending on the correct data type given by its file info (using a functions map).
No type switching needs to be hard-coded anymore. ReaderAlgorithm uses a plugin mechanism for new data types and the types list can be dynamically extended when needed.
Usage: The idea is to plug into a ReaderAlgorithm (generally a subclass) some pointers to type-specialized processing functions. These functions can be either external functions (generally template):
or static member functions:
Instantiate such an algorithm class, and register every needed type with the registerAlgorithmType() function (you can do it in your algo constructor). Then call the execute() function with the file name (or other DataSource) as an argument. The algo function registered for the correct data type will be called, if this type can be guessed by the DataSourceInfo, or an exception will be thrown if it fails.
In fact if your algo doesn't need other information (no data in your ReaderAlgorithm subclass), you don't even really need to subclass it: manually registering algo functions should be enough.
A small example:
Volume
of float
or int
) and on volumes of complex, and use specific operations for the former (ie ordering, comparisons operations or casting to float
or double
) which would not work on the latter.Definition at line 217 of file readeralgorithm.h.
typedef bool(* soma::ReaderAlgorithm::ProcFunc) (ReaderAlgorithm &, carto::Object header, carto::rc_ptr< DataSource > source) |
Algorithm function type.
These functions are stored in a type-to-function map and called by the execute() method. They are called with the DataSource containing the object to read and the corresponding header: here DataSourceInfo::check() has already been successfully called by execute(). Each function must be specialized for a given object and data type (possibly using template functions). Unfortunately these functions pointers can not be pointer to member functions as we will need access to derived classes members (not allowed in C++ spec), so we also provide a reference to this
ReaderAlgorithm object to the algo function (callback-style)
Definition at line 231 of file readeralgorithm.h.
soma::ReaderAlgorithm::ReaderAlgorithm | ( | const std::string & | algoname | ) |
|
virtual |
|
inline |
Query registered process types.
Definition at line 256 of file readeralgorithm.h.
References _execs.
bool soma::ReaderAlgorithm::execute | ( | carto::Object | header, |
carto::rc_ptr< DataSource > | source | ||
) |
Same as above but the header has already been read (or hand-made to fake it!)
bool soma::ReaderAlgorithm::execute | ( | carto::rc_ptr< DataSource > | stream | ) |
same as above but uses a DataSource as input
bool soma::ReaderAlgorithm::execute | ( | carto::rc_ptr< DataSourceInfo > | dsi | ) |
same as above but uses DataSourceInfo as input.
Header may have already been read
bool soma::ReaderAlgorithm::execute | ( | const std::string & | filename | ) |
Executes the algo on the object type found in the given file.
Uses DataSourceInfo to figure out which data should be used. This will either call the registered ProcFunc or throw a datatype_format_error exception if no algo function has been registered on that type
void soma::ReaderAlgorithm::registerAlgorithmType | ( | const std::string & | objectType, |
ProcFunc | procFunc | ||
) |
Registers the algo function to call on a given object type (just fills the map)
|
protected |
Definition at line 260 of file readeralgorithm.h.
Referenced by algorithmTypes().