Ask Your Question
0

understand source code of LayerFactory

asked 2019-03-21 17:10:46 -0600

_sparkle_eyes gravatar image

updated 2019-03-22 02:28:40 -0600

berak gravatar image

Dear community,

I am having trouble understanding some details in the following chunk of C++ code:

 class CV_EXPORTS LayerFactory
{
public:

    //! Each Layer class must provide this function to the factory
    typedef Ptr<Layer>(*Constructor)(LayerParams &params);

    //! Registers the layer class with typename @p type and specified @p constructor. Thread-safe.
    static void registerLayer(const String &type, Constructor constructor);

    //! Unregisters registered layer with specified type name. Thread-safe.
    static void unregisterLayer(const String &type);

    /** @brief Creates instance of registered layer.
     *  @Param type type name of creating layer.
     *  @Param params parameters which will be used for layer initialization.
     *  @note Thread-safe.
     */
    static Ptr<Layer> createLayerInstance(const String &type, LayerParams& params);

private:
    LayerFactory();
};

In particular, I know this class has a registerLayer and an unregisterLayer member function, but I'm mostly confused about this line

typedef Ptr<Layer>(*Constructor)(LayerParams &params);

How to parse the syntax? What exactly is typedef defining? Why is there a *Constructor --- I can't find its definition anywhere in the source.

Thanks! This piece of code is from opencv/modules/dnn/include/opencv2/dnn/layer.hpp

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-03-22 02:10:06 -0600

berak gravatar image

updated 2019-03-22 02:11:07 -0600

so, this is more a C than a c++ construct:

typedef Ptr<Layer>(*Constructor)(LayerParams &params);

is the definition of a function pointer, called Constructor, which takes LayerParams as arguments and returns a Ptr<Layer> , you can pass it as an argument to other functions, like

void factory(Constructor c, LayerParams lp) {
    Ptr<Layer> lyr = c(lp); // make a layer
    // use the layer for something, e.g. constructing a graph 
}

and later:

// SliceLayer::create() fits the "Constructor" signature
factory(dnn::SliceLayer::create, params);
edit flag offensive delete link more

Comments

apologize for the late getaround, and thanks for the answer!

_sparkle_eyes gravatar image_sparkle_eyes ( 2019-07-24 16:30:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-21 17:10:46 -0600

Seen: 183 times

Last updated: Mar 22 '19