I'm developing a few image-processing algorithms in C++. To make my code more generalized and to be able to configure everything without recompiling the whole project I've came up with an idea to split the processing algorithms into small parts ("extractors"), make them as objects inherited from a single interface and configure their execution order and parameters from an XML file parsed by factory methods. But the input and output types of these basic processing blocks can be different, so I thought about using boost::any as a generalized type, so every every operation with image would look like:
boost::any Process(const boost::any& feature);
Every object should store the proper input and output types inside and perform boxing-unboxing each time it executes. Is it a good idea to use such technique? It kind of satisfies my needs and would be very natural in Python, but at the same time looks like an ugly hack in C++, which is inherently static typed, so I'm in doubt if I should use it.
UPD: a little example to be more clear
// Base class for all processing
template <typename Input, typename Output>
class Processor {
public:
virtual ~Processor();
virtual Output Process(const Input& input) const = 0;
};
// Generalized type-erased processor
typedef Processor<boost::any, boost::any> TypeErasedProcessor;
// Boxing-unboxing wrapper
template <typename Input, typename Output>
class ProcessorWrapper: public TypeErasedProcessor {
public:
boost::any Process(const boost::any& boxed_input) const {
Input input = boost::any_cast<Input>(boxed_input);
Output output = processor_->Process(input);
boost::any boxed_output = output;
return boxed_output;
}
private:
std::shared_ptr<Processor<Input, Output>> processor_;
};
class SimpleImageProcessingChain: public TypeErasedProcessor {
public:
boost::any Process(const boost::any& input) const {
boost::any result = input;
for (const auto& processor: processors_) {
result = processor->Process(result);
}
return result;
}
private:
std::vector<std::shared_ptr<TypeErasedProcessor>> processors_;
};