base.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // Copyright (C) 2020-2021 Intel Corporation
  6. #ifndef OPENCV_GAPI_S11N_BASE_HPP
  7. #define OPENCV_GAPI_S11N_BASE_HPP
  8. #include <opencv2/gapi/own/assert.hpp>
  9. #include <opencv2/gapi/own/exports.hpp>
  10. namespace cv {
  11. namespace gapi {
  12. /**
  13. * @brief This namespace contains G-API serialization and
  14. * deserialization functions and data structures.
  15. */
  16. namespace s11n {
  17. struct IOStream;
  18. struct IIStream;
  19. namespace detail {
  20. //! @addtogroup gapi_serialization
  21. //! @{
  22. struct NotImplemented {
  23. };
  24. /** @brief This structure allows to implement serialization routines for custom types.
  25. *
  26. * The default S11N for custom types is not implemented.
  27. *
  28. * @note When providing an overloaded implementation for S11N with your type
  29. * don't inherit it from NotImplemented structure.
  30. *
  31. * @note There are lots of overloaded >> and << operators for basic and OpenCV/G-API types
  32. * which can be utilized when serializing a custom type.
  33. *
  34. * Example of usage:
  35. * @snippet samples/cpp/tutorial_code/gapi/doc_snippets/api_ref_snippets.cpp S11N usage
  36. *
  37. */
  38. template<typename T>
  39. struct S11N: public NotImplemented {
  40. /**
  41. * @brief This function allows user to serialize their custom type.
  42. *
  43. * @note The default overload throws an exception if called. User need to
  44. * properly overload the function to use it.
  45. */
  46. static void serialize(IOStream &, const T &) {
  47. GAPI_Assert(false && "No serialization routine is provided!");
  48. }
  49. /**
  50. * @brief This function allows user to deserialize their custom type.
  51. *
  52. * @note The default overload throws an exception if called. User need to
  53. * properly overload the function to use it.
  54. */
  55. static T deserialize(IIStream &) {
  56. GAPI_Assert(false && "No deserialization routine is provided!");
  57. }
  58. };
  59. /// @private -- Exclude this struct from OpenCV documentation
  60. template<typename T> struct has_S11N_spec {
  61. static constexpr bool value = !std::is_base_of<NotImplemented,
  62. S11N<typename std::decay<T>::type>>::value;
  63. };
  64. //! @} gapi_serialization
  65. } // namespace detail
  66. } // namespace s11n
  67. } // namespace gapi
  68. } // namespace cv
  69. #endif // OPENCV_GAPI_S11N_BASE_HPP