layer.details.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <opencv2/dnn/layer.hpp>
  8. namespace cv {
  9. namespace dnn {
  10. CV__DNN_INLINE_NS_BEGIN
  11. /** @brief Registers layer constructor in runtime.
  12. * @param type string, containing type name of the layer.
  13. * @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
  14. * @details This macros must be placed inside the function code.
  15. */
  16. #define CV_DNN_REGISTER_LAYER_FUNC(type, constructorFunc) \
  17. cv::dnn::LayerFactory::registerLayer(#type, constructorFunc);
  18. /** @brief Registers layer class in runtime.
  19. * @param type string, containing type name of the layer.
  20. * @param class C++ class, derived from Layer.
  21. * @details This macros must be placed inside the function code.
  22. */
  23. #define CV_DNN_REGISTER_LAYER_CLASS(type, class) \
  24. cv::dnn::LayerFactory::registerLayer(#type, cv::dnn::details::_layerDynamicRegisterer<class>);
  25. /** @brief Registers layer constructor on module load time.
  26. * @param type string, containing type name of the layer.
  27. * @param constructorFunc pointer to the function of type LayerRegister::Constructor, which creates the layer.
  28. * @details This macros must be placed outside the function code.
  29. */
  30. #define CV_DNN_REGISTER_LAYER_FUNC_STATIC(type, constructorFunc) \
  31. static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, constructorFunc);
  32. /** @brief Registers layer class on module load time.
  33. * @param type string, containing type name of the layer.
  34. * @param class C++ class, derived from Layer.
  35. * @details This macros must be placed outside the function code.
  36. */
  37. #define CV_DNN_REGISTER_LAYER_CLASS_STATIC(type, class) \
  38. Ptr<Layer> __LayerStaticRegisterer_func_##type(LayerParams &params) \
  39. { return Ptr<Layer>(new class(params)); } \
  40. static cv::dnn::details::_LayerStaticRegisterer __LayerStaticRegisterer_##type(#type, __LayerStaticRegisterer_func_##type);
  41. namespace details {
  42. template<typename LayerClass>
  43. Ptr<Layer> _layerDynamicRegisterer(LayerParams &params)
  44. {
  45. return Ptr<Layer>(LayerClass::create(params));
  46. }
  47. //allows automatically register created layer on module load time
  48. class _LayerStaticRegisterer
  49. {
  50. String type;
  51. public:
  52. _LayerStaticRegisterer(const String &layerType, LayerFactory::Constructor layerConstructor)
  53. {
  54. this->type = layerType;
  55. LayerFactory::registerLayer(layerType, layerConstructor);
  56. }
  57. ~_LayerStaticRegisterer()
  58. {
  59. LayerFactory::unregisterLayer(type);
  60. }
  61. };
  62. } // namespace
  63. CV__DNN_INLINE_NS_END
  64. }} // namespace
  65. #endif