0

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_;
};
lizarisk
  • 7,562
  • 10
  • 46
  • 70

2 Answers2

2

In most cases, readability of your code is more important than being able to to avoid recompiling.

If I were to work with your algorithms, I'd definitely prefer having them statically typed.

Reunanen
  • 7,921
  • 2
  • 35
  • 57
0

In this case, no. For that matter, you wouldn't really do this in Python either; your function in Python can't take just any object; it will only work with objects which implement a specific protocol. You're function won't work in Python if you pass it a float, or a list of string; it will only work if you pass it something that behaves like an image, and has the interface of an image. The only difference between Python and C++ here is that in C++, if an object implements a protocol, it must be declared to do so, by inheriting from an abstract base class which defined the interface of the protocol.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • This is obvious, I'm not trying to make my function work with any type of object, I'm declaring the input/output types in XML and parsing them in runtime instead of static typing. – lizarisk Mar 19 '13 at 20:20