From da0960321b8ca62c217be1b1db0dfb858c9a83ea Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 28 Jun 2017 19:59:02 +0000 Subject: [PATCH] dnn: added "hidden" experimental namespace Main purpose of this namespace is to avoid using of incompatible binaries that will cause applications crashes. This additional namespace will not impact "Source code API". This change allows to maintain ABI checks (with easy filtering out). --- .../dnn/include/opencv2/dnn/all_layers.hpp | 9 ++++----- modules/dnn/include/opencv2/dnn/dict.hpp | 8 ++++---- modules/dnn/include/opencv2/dnn/dnn.hpp | 20 +++++++++++++++---- modules/dnn/include/opencv2/dnn/dnn.inl.hpp | 8 ++++---- .../dnn/include/opencv2/dnn/layer.details.hpp | 11 +++++----- modules/dnn/include/opencv2/dnn/layer.hpp | 9 ++++----- .../dnn/include/opencv2/dnn/shape_utils.hpp | 2 ++ modules/dnn/src/caffe/caffe_importer.cpp | 17 +++++++++++----- modules/dnn/src/dnn.cpp | 14 +++++-------- modules/dnn/src/init.cpp | 10 +++++----- modules/dnn/src/precomp.hpp | 2 ++ modules/dnn/src/tensorflow/tf_importer.cpp | 18 ++++++++++++----- modules/dnn/src/torch/torch_importer.cpp | 6 ++++-- 13 files changed, 81 insertions(+), 53 deletions(-) diff --git a/modules/dnn/include/opencv2/dnn/all_layers.hpp b/modules/dnn/include/opencv2/dnn/all_layers.hpp index 6113090b3e..3e1fbae811 100644 --- a/modules/dnn/include/opencv2/dnn/all_layers.hpp +++ b/modules/dnn/include/opencv2/dnn/all_layers.hpp @@ -43,10 +43,9 @@ #define OPENCV_DNN_DNN_ALL_LAYERS_HPP #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN //! @addtogroup dnn //! @{ @@ -459,7 +458,7 @@ namespace dnn //! @} //! @} - +CV__DNN_EXPERIMENTAL_NS_END } } #endif diff --git a/modules/dnn/include/opencv2/dnn/dict.hpp b/modules/dnn/include/opencv2/dnn/dict.hpp index 9a7efb7134..a9a70c9219 100644 --- a/modules/dnn/include/opencv2/dnn/dict.hpp +++ b/modules/dnn/include/opencv2/dnn/dict.hpp @@ -46,10 +46,9 @@ #include #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN //! @addtogroup dnn //! @{ @@ -140,6 +139,7 @@ public: }; //! @} +CV__DNN_EXPERIMENTAL_NS_END } } diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp index d5b97a3fde..8aa7c70c8c 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.hpp @@ -44,12 +44,23 @@ #include #include + +#if !defined CV_DOXYGEN && !defined CV_DNN_DONT_ADD_EXPERIMENTAL_NS +#define CV__DNN_EXPERIMENTAL_NS_USE using namespace experimental_dnn_v1; +#define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_v1 { +#define CV__DNN_EXPERIMENTAL_NS_END } +#else +#define CV__DNN_EXPERIMENTAL_NS_USE +#define CV__DNN_EXPERIMENTAL_NS_BEGIN +#define CV__DNN_EXPERIMENTAL_NS_END +#endif + #include -namespace cv -{ -namespace dnn //! This namespace is used for dnn module functionlaity. -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_USE +CV__DNN_EXPERIMENTAL_NS_BEGIN //! @addtogroup dnn //! @{ @@ -658,6 +669,7 @@ namespace dnn //! This namespace is used for dnn module functionlaity. Size size = Size(), const Scalar& mean = Scalar(), bool swapRB=true); //! @} +CV__DNN_EXPERIMENTAL_NS_END } } diff --git a/modules/dnn/include/opencv2/dnn/dnn.inl.hpp b/modules/dnn/include/opencv2/dnn/dnn.inl.hpp index 0bfde6f515..debf222514 100644 --- a/modules/dnn/include/opencv2/dnn/dnn.inl.hpp +++ b/modules/dnn/include/opencv2/dnn/dnn.inl.hpp @@ -44,10 +44,9 @@ #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN template DictValue DictValue::arrayInt(TypeIter begin, int size) @@ -351,6 +350,7 @@ inline std::ostream &operator<<(std::ostream &stream, const Dict &dict) return stream; } +CV__DNN_EXPERIMENTAL_NS_END } } diff --git a/modules/dnn/include/opencv2/dnn/layer.details.hpp b/modules/dnn/include/opencv2/dnn/layer.details.hpp index ca2d54cd24..82bd3b11f2 100644 --- a/modules/dnn/include/opencv2/dnn/layer.details.hpp +++ b/modules/dnn/include/opencv2/dnn/layer.details.hpp @@ -7,10 +7,9 @@ #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN /** @brief Registers layer constructor in runtime. * @param type string, containing type name of the layer. @@ -72,6 +71,8 @@ public: } }; -}}} //namespace +} // namespace +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace #endif diff --git a/modules/dnn/include/opencv2/dnn/layer.hpp b/modules/dnn/include/opencv2/dnn/layer.hpp index 7025d5bb94..3fb81f326a 100644 --- a/modules/dnn/include/opencv2/dnn/layer.hpp +++ b/modules/dnn/include/opencv2/dnn/layer.hpp @@ -43,10 +43,9 @@ #define OPENCV_DNN_LAYER_HPP #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN //! @addtogroup dnn //! @{ //! @@ -80,7 +79,7 @@ private: //! @} //! @} - +CV__DNN_EXPERIMENTAL_NS_END } } #endif diff --git a/modules/dnn/include/opencv2/dnn/shape_utils.hpp b/modules/dnn/include/opencv2/dnn/shape_utils.hpp index c0c8ce47f4..e74b53cce8 100644 --- a/modules/dnn/include/opencv2/dnn/shape_utils.hpp +++ b/modules/dnn/include/opencv2/dnn/shape_utils.hpp @@ -48,6 +48,7 @@ namespace cv { namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN //Useful shortcut inline std::ostream &operator<< (std::ostream &s, cv::Range &r) @@ -190,6 +191,7 @@ inline int clamp(int ax, const MatShape& shape) return clamp(ax, (int)shape.size()); } +CV__DNN_EXPERIMENTAL_NS_END } } #endif diff --git a/modules/dnn/src/caffe/caffe_importer.cpp b/modules/dnn/src/caffe/caffe_importer.cpp index 15aae982f5..3f613100fb 100644 --- a/modules/dnn/src/caffe/caffe_importer.cpp +++ b/modules/dnn/src/caffe/caffe_importer.cpp @@ -40,8 +40,6 @@ //M*/ #include "../precomp.hpp" -using namespace cv; -using namespace cv::dnn; #if HAVE_PROTOBUF #include "caffe.pb.h" @@ -54,7 +52,13 @@ using namespace cv::dnn; #include #include #include "caffe_io.hpp" +#endif +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN + +#ifdef HAVE_PROTOBUF using ::google::protobuf::RepeatedField; using ::google::protobuf::RepeatedPtrField; using ::google::protobuf::Message; @@ -357,14 +361,14 @@ public: } -Ptr cv::dnn::createCaffeImporter(const String &prototxt, const String &caffeModel) +Ptr createCaffeImporter(const String &prototxt, const String &caffeModel) { return Ptr(new CaffeImporter(prototxt.c_str(), caffeModel.c_str())); } #else //HAVE_PROTOBUF -Ptr cv::dnn::createCaffeImporter(const String&, const String&) +Ptr createCaffeImporter(const String&, const String&) { CV_Error(cv::Error::StsNotImplemented, "libprotobuf required to import data from Caffe models"); return Ptr(); @@ -372,7 +376,7 @@ Ptr cv::dnn::createCaffeImporter(const String&, const String&) #endif //HAVE_PROTOBUF -Net cv::dnn::readNetFromCaffe(const String &prototxt, const String &caffeModel /*= String()*/) +Net readNetFromCaffe(const String &prototxt, const String &caffeModel /*= String()*/) { Ptr caffeImporter = createCaffeImporter(prototxt, caffeModel); Net net; @@ -380,3 +384,6 @@ Net cv::dnn::readNetFromCaffe(const String &prototxt, const String &caffeModel / caffeImporter->populateNet(net); return net; } + +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 236d089965..745224b9d7 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -50,8 +50,9 @@ #include #include -using namespace cv; -using namespace cv::dnn; +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN using std::vector; using std::map; @@ -74,11 +75,6 @@ namespace }; } -namespace cv -{ -namespace dnn -{ - template static String toString(const T &v) { @@ -2030,5 +2026,5 @@ BackendWrapper::BackendWrapper(const Ptr& base, const MatShape& BackendWrapper::~BackendWrapper() {} -} -} +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace diff --git a/modules/dnn/src/init.cpp b/modules/dnn/src/init.cpp index 1fb0806090..c3f1e30569 100644 --- a/modules/dnn/src/init.cpp +++ b/modules/dnn/src/init.cpp @@ -42,10 +42,9 @@ #include "precomp.hpp" #include -namespace cv -{ -namespace dnn -{ +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN static Mutex* __initialization_mutex = NULL; Mutex& getInitializationMutex() @@ -98,4 +97,5 @@ void initializeLayerFactory() CV_DNN_REGISTER_LAYER_CLASS(Scale, ScaleLayer); } -}} //namespace +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace diff --git a/modules/dnn/src/precomp.hpp b/modules/dnn/src/precomp.hpp index ee3e9dc784..29a787f47a 100644 --- a/modules/dnn/src/precomp.hpp +++ b/modules/dnn/src/precomp.hpp @@ -45,6 +45,8 @@ #include namespace cv { namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN Mutex& getInitializationMutex(); void initializeLayerFactory(); +CV__DNN_EXPERIMENTAL_NS_END }} // namespace diff --git a/modules/dnn/src/tensorflow/tf_importer.cpp b/modules/dnn/src/tensorflow/tf_importer.cpp index 0ff2dd3b5d..8a39d7bc56 100644 --- a/modules/dnn/src/tensorflow/tf_importer.cpp +++ b/modules/dnn/src/tensorflow/tf_importer.cpp @@ -10,8 +10,6 @@ Implementation of Tensorflow models parser */ #include "../precomp.hpp" -using namespace cv; -using namespace cv::dnn; #if HAVE_PROTOBUF #include "graph.pb.h" @@ -24,6 +22,13 @@ using namespace cv::dnn; #include #include #include "tf_io.hpp" +#endif + +namespace cv { +namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN + +#if HAVE_PROTOBUF using ::google::protobuf::RepeatedField; using ::google::protobuf::RepeatedPtrField; @@ -734,14 +739,14 @@ void TFImporter::populateNet(Net dstNet) } // namespace -Ptr cv::dnn::createTensorflowImporter(const String &model) +Ptr createTensorflowImporter(const String &model) { return Ptr(new TFImporter(model.c_str())); } #else //HAVE_PROTOBUF -Ptr cv::dnn::createTensorflowImporter(const String&) +Ptr createTensorflowImporter(const String&) { CV_Error(cv::Error::StsNotImplemented, "libprotobuf required to import data from TensorFlow models"); return Ptr(); @@ -749,7 +754,7 @@ Ptr cv::dnn::createTensorflowImporter(const String&) #endif //HAVE_PROTOBUF -Net cv::dnn::readNetFromTensorflow(const String &model) +Net readNetFromTensorflow(const String &model) { Ptr importer = createTensorflowImporter(model); Net net; @@ -757,3 +762,6 @@ Net cv::dnn::readNetFromTensorflow(const String &model) importer->populateNet(net); return net; } + +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace diff --git a/modules/dnn/src/torch/torch_importer.cpp b/modules/dnn/src/torch/torch_importer.cpp index 02e936d314..7fc9a0c760 100644 --- a/modules/dnn/src/torch/torch_importer.cpp +++ b/modules/dnn/src/torch/torch_importer.cpp @@ -49,6 +49,8 @@ namespace cv { namespace dnn { +CV__DNN_EXPERIMENTAL_NS_BEGIN + #if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER #include "THDiskFile.h" @@ -1021,5 +1023,5 @@ Net readNetFromTorch(const String &model, bool isBinary) return net; } -} -} +CV__DNN_EXPERIMENTAL_NS_END +}} // namespace