1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 #ifndef OPENCV_DNN_LAYER_DETAILS_HPP
6 #define OPENCV_DNN_LAYER_DETAILS_HPP
7 
8 #include <opencv2/dnn/layer.hpp>
9 
10 namespace cv {
11 namespace dnn {
12 CV__DNN_EXPERIMENTAL_NS_BEGIN
13 
14 /** @brief Registers layer constructor in runtime.
15 *   @param type string, containing type name of the layer.
16 *   @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
17 *   @details This macros must be placed inside the function code.
18 */
19 #define CV_DNN_REGISTER_LAYER_FUNC(type, constructorFunc) \
20     cv::dnn::LayerFactory::registerLayer(#type, constructorFunc);
21 
22 /** @brief Registers layer class in runtime.
23  *  @param type string, containing type name of the layer.
24  *  @param class C++ class, derived from Layer.
25  *  @details This macros must be placed inside the function code.
26  */
27 #define CV_DNN_REGISTER_LAYER_CLASS(type, class) \
28     cv::dnn::LayerFactory::registerLayer(#type, cv::dnn::details::_layerDynamicRegisterer<class>);
29 
30 /** @brief Registers layer constructor on module load time.
31 *   @param type string, containing type name of the layer.
32 *   @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
33 *   @details This macros must be placed outside the function code.
34 */
35 #define CV_DNN_REGISTER_LAYER_FUNC_STATIC(type, constructorFunc) \
36 static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, constructorFunc);
37 
38 /** @brief Registers layer class on module load time.
39  *  @param type string, containing type name of the layer.
40  *  @param class C++ class, derived from Layer.
41  *  @details This macros must be placed outside the function code.
42  */
43 #define CV_DNN_REGISTER_LAYER_CLASS_STATIC(type, class)                         \
44 Ptr<Layer> __LayerStaticRegisterer_func_##type(LayerParams &params) \
45     { return Ptr<Layer>(new class(params)); }                       \
46 static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, __LayerStaticRegisterer_func_##type);
47 
48 namespace details {
49 
50 template<typename LayerClass>
_layerDynamicRegisterer(LayerParams & params)51 Ptr<Layer> _layerDynamicRegisterer(LayerParams &params)
52 {
53     return Ptr<Layer>(LayerClass::create(params));
54 }
55 
56 //allows automatically register created layer on module load time
57 class _LayerStaticRegisterer
58 {
59     String type;
60 public:
61 
_LayerStaticRegisterer(const String & layerType,LayerFactory::Constructor layerConstructor)62     _LayerStaticRegisterer(const String &layerType, LayerFactory::Constructor layerConstructor)
63     {
64         this->type = layerType;
65         LayerFactory::registerLayer(layerType, layerConstructor);
66     }
67 
~_LayerStaticRegisterer()68     ~_LayerStaticRegisterer()
69     {
70         LayerFactory::unregisterLayer(type);
71     }
72 };
73 
74 } // namespace
75 CV__DNN_EXPERIMENTAL_NS_END
76 }} // namespace
77 
78 #endif
79