traits.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_TRAITS_HPP
  44. #define OPENCV_CORE_TRAITS_HPP
  45. #include "opencv2/core/cvdef.h"
  46. namespace cv
  47. {
  48. //#define OPENCV_TRAITS_ENABLE_DEPRECATED
  49. //! @addtogroup core_basic
  50. //! @{
  51. /** @brief Template "trait" class for OpenCV primitive data types.
  52. @note Deprecated. This is replaced by "single purpose" traits: traits::Type and traits::Depth
  53. A primitive OpenCV data type is one of unsigned char, bool, signed char, unsigned short, signed
  54. short, int, float, double, or a tuple of values of one of these types, where all the values in the
  55. tuple have the same type. Any primitive type from the list can be defined by an identifier in the
  56. form CV_\<bit-depth\>{U|S|F}C(\<number_of_channels\>), for example: uchar \~ CV_8UC1, 3-element
  57. floating-point tuple \~ CV_32FC3, and so on. A universal OpenCV structure that is able to store a
  58. single instance of such a primitive data type is Vec. Multiple instances of such a type can be
  59. stored in a std::vector, Mat, Mat_, SparseMat, SparseMat_, or any other container that is able to
  60. store Vec instances.
  61. The DataType class is basically used to provide a description of such primitive data types without
  62. adding any fields or methods to the corresponding classes (and it is actually impossible to add
  63. anything to primitive C/C++ data types). This technique is known in C++ as class traits. It is not
  64. DataType itself that is used but its specialized versions, such as:
  65. @code
  66. template<> class DataType<uchar>
  67. {
  68. typedef uchar value_type;
  69. typedef int work_type;
  70. typedef uchar channel_type;
  71. enum { channel_type = CV_8U, channels = 1, fmt='u', type = CV_8U };
  72. };
  73. ...
  74. template<typename _Tp> DataType<std::complex<_Tp> >
  75. {
  76. typedef std::complex<_Tp> value_type;
  77. typedef std::complex<_Tp> work_type;
  78. typedef _Tp channel_type;
  79. // DataDepth is another helper trait class
  80. enum { depth = DataDepth<_Tp>::value, channels=2,
  81. fmt=(channels-1)*256+DataDepth<_Tp>::fmt,
  82. type=CV_MAKETYPE(depth, channels) };
  83. };
  84. ...
  85. @endcode
  86. The main purpose of this class is to convert compilation-time type information to an
  87. OpenCV-compatible data type identifier, for example:
  88. @code
  89. // allocates a 30x40 floating-point matrix
  90. Mat A(30, 40, DataType<float>::type);
  91. Mat B = Mat_<std::complex<double> >(3, 3);
  92. // the statement below will print 6, 2 , that is depth == CV_64F, channels == 2
  93. cout << B.depth() << ", " << B.channels() << endl;
  94. @endcode
  95. So, such traits are used to tell OpenCV which data type you are working with, even if such a type is
  96. not native to OpenCV. For example, the matrix B initialization above is compiled because OpenCV
  97. defines the proper specialized template class DataType\<complex\<_Tp\> \> . This mechanism is also
  98. useful (and used in OpenCV this way) for generic algorithms implementations.
  99. @note Default values were dropped to stop confusing developers about using of unsupported types (see #7599)
  100. */
  101. template<typename _Tp> class DataType
  102. {
  103. public:
  104. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  105. typedef _Tp value_type;
  106. typedef value_type work_type;
  107. typedef value_type channel_type;
  108. typedef value_type vec_type;
  109. enum { generic_type = 1,
  110. depth = -1,
  111. channels = 1,
  112. fmt = 0,
  113. type = CV_MAKETYPE(depth, channels)
  114. };
  115. #endif
  116. };
  117. template<> class DataType<bool>
  118. {
  119. public:
  120. typedef bool value_type;
  121. typedef int work_type;
  122. typedef value_type channel_type;
  123. typedef value_type vec_type;
  124. enum { generic_type = 0,
  125. depth = CV_8U,
  126. channels = 1,
  127. fmt = (int)'u',
  128. type = CV_MAKETYPE(depth, channels)
  129. };
  130. };
  131. template<> class DataType<uchar>
  132. {
  133. public:
  134. typedef uchar value_type;
  135. typedef int work_type;
  136. typedef value_type channel_type;
  137. typedef value_type vec_type;
  138. enum { generic_type = 0,
  139. depth = CV_8U,
  140. channels = 1,
  141. fmt = (int)'u',
  142. type = CV_MAKETYPE(depth, channels)
  143. };
  144. };
  145. template<> class DataType<schar>
  146. {
  147. public:
  148. typedef schar value_type;
  149. typedef int work_type;
  150. typedef value_type channel_type;
  151. typedef value_type vec_type;
  152. enum { generic_type = 0,
  153. depth = CV_8S,
  154. channels = 1,
  155. fmt = (int)'c',
  156. type = CV_MAKETYPE(depth, channels)
  157. };
  158. };
  159. template<> class DataType<char>
  160. {
  161. public:
  162. typedef schar value_type;
  163. typedef int work_type;
  164. typedef value_type channel_type;
  165. typedef value_type vec_type;
  166. enum { generic_type = 0,
  167. depth = CV_8S,
  168. channels = 1,
  169. fmt = (int)'c',
  170. type = CV_MAKETYPE(depth, channels)
  171. };
  172. };
  173. template<> class DataType<ushort>
  174. {
  175. public:
  176. typedef ushort value_type;
  177. typedef int work_type;
  178. typedef value_type channel_type;
  179. typedef value_type vec_type;
  180. enum { generic_type = 0,
  181. depth = CV_16U,
  182. channels = 1,
  183. fmt = (int)'w',
  184. type = CV_MAKETYPE(depth, channels)
  185. };
  186. };
  187. template<> class DataType<short>
  188. {
  189. public:
  190. typedef short value_type;
  191. typedef int work_type;
  192. typedef value_type channel_type;
  193. typedef value_type vec_type;
  194. enum { generic_type = 0,
  195. depth = CV_16S,
  196. channels = 1,
  197. fmt = (int)'s',
  198. type = CV_MAKETYPE(depth, channels)
  199. };
  200. };
  201. template<> class DataType<int>
  202. {
  203. public:
  204. typedef int value_type;
  205. typedef value_type work_type;
  206. typedef value_type channel_type;
  207. typedef value_type vec_type;
  208. enum { generic_type = 0,
  209. depth = CV_32S,
  210. channels = 1,
  211. fmt = (int)'i',
  212. type = CV_MAKETYPE(depth, channels)
  213. };
  214. };
  215. template<> class DataType<float>
  216. {
  217. public:
  218. typedef float value_type;
  219. typedef value_type work_type;
  220. typedef value_type channel_type;
  221. typedef value_type vec_type;
  222. enum { generic_type = 0,
  223. depth = CV_32F,
  224. channels = 1,
  225. fmt = (int)'f',
  226. type = CV_MAKETYPE(depth, channels)
  227. };
  228. };
  229. template<> class DataType<double>
  230. {
  231. public:
  232. typedef double value_type;
  233. typedef value_type work_type;
  234. typedef value_type channel_type;
  235. typedef value_type vec_type;
  236. enum { generic_type = 0,
  237. depth = CV_64F,
  238. channels = 1,
  239. fmt = (int)'d',
  240. type = CV_MAKETYPE(depth, channels)
  241. };
  242. };
  243. template<> class DataType<float16_t>
  244. {
  245. public:
  246. typedef float16_t value_type;
  247. typedef float work_type;
  248. typedef value_type channel_type;
  249. typedef value_type vec_type;
  250. enum { generic_type = 0,
  251. depth = CV_16F,
  252. channels = 1,
  253. fmt = (int)'h',
  254. type = CV_MAKETYPE(depth, channels)
  255. };
  256. };
  257. /** @brief A helper class for cv::DataType
  258. The class is specialized for each fundamental numerical data type supported by OpenCV. It provides
  259. DataDepth<T>::value constant.
  260. */
  261. template<typename _Tp> class DataDepth
  262. {
  263. public:
  264. enum
  265. {
  266. value = DataType<_Tp>::depth,
  267. fmt = DataType<_Tp>::fmt
  268. };
  269. };
  270. #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
  271. template<int _depth> class TypeDepth
  272. {
  273. #ifdef OPENCV_TRAITS_ENABLE_LEGACY_DEFAULTS
  274. enum { depth = CV_USRTYPE1 };
  275. typedef void value_type;
  276. #endif
  277. };
  278. template<> class TypeDepth<CV_8U>
  279. {
  280. enum { depth = CV_8U };
  281. typedef uchar value_type;
  282. };
  283. template<> class TypeDepth<CV_8S>
  284. {
  285. enum { depth = CV_8S };
  286. typedef schar value_type;
  287. };
  288. template<> class TypeDepth<CV_16U>
  289. {
  290. enum { depth = CV_16U };
  291. typedef ushort value_type;
  292. };
  293. template<> class TypeDepth<CV_16S>
  294. {
  295. enum { depth = CV_16S };
  296. typedef short value_type;
  297. };
  298. template<> class TypeDepth<CV_32S>
  299. {
  300. enum { depth = CV_32S };
  301. typedef int value_type;
  302. };
  303. template<> class TypeDepth<CV_32F>
  304. {
  305. enum { depth = CV_32F };
  306. typedef float value_type;
  307. };
  308. template<> class TypeDepth<CV_64F>
  309. {
  310. enum { depth = CV_64F };
  311. typedef double value_type;
  312. };
  313. template<> class TypeDepth<CV_16F>
  314. {
  315. enum { depth = CV_16F };
  316. typedef float16_t value_type;
  317. };
  318. #endif
  319. //! @}
  320. namespace traits {
  321. namespace internal {
  322. #define CV_CREATE_MEMBER_CHECK(X) \
  323. template<typename T> class CheckMember_##X { \
  324. struct Fallback { int X; }; \
  325. struct Derived : T, Fallback { }; \
  326. template<typename U, U> struct Check; \
  327. typedef char CV_NO[1]; \
  328. typedef char CV_YES[2]; \
  329. template<typename U> static CV_NO & func(Check<int Fallback::*, &U::X> *); \
  330. template<typename U> static CV_YES & func(...); \
  331. public: \
  332. typedef CheckMember_##X type; \
  333. enum { value = sizeof(func<Derived>(0)) == sizeof(CV_YES) }; \
  334. };
  335. CV_CREATE_MEMBER_CHECK(fmt)
  336. CV_CREATE_MEMBER_CHECK(type)
  337. } // namespace internal
  338. template<typename T>
  339. struct Depth
  340. { enum { value = DataType<T>::depth }; };
  341. template<typename T>
  342. struct Type
  343. { enum { value = DataType<T>::type }; };
  344. /** Similar to traits::Type<T> but has value = -1 in case of unknown type (instead of compiler error) */
  345. template<typename T, bool available = internal::CheckMember_type< DataType<T> >::value >
  346. struct SafeType {};
  347. template<typename T>
  348. struct SafeType<T, false>
  349. { enum { value = -1 }; };
  350. template<typename T>
  351. struct SafeType<T, true>
  352. { enum { value = Type<T>::value }; };
  353. template<typename T, bool available = internal::CheckMember_fmt< DataType<T> >::value >
  354. struct SafeFmt {};
  355. template<typename T>
  356. struct SafeFmt<T, false>
  357. { enum { fmt = 0 }; };
  358. template<typename T>
  359. struct SafeFmt<T, true>
  360. { enum { fmt = DataType<T>::fmt }; };
  361. } // namespace
  362. } // cv
  363. #endif // OPENCV_CORE_TRAITS_HPP