CVObjcUtil.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // CVObjcUtil.h
  3. //
  4. // Created by Giles Payne on 2020/01/02.
  5. //
  6. #pragma once
  7. #ifndef CV_EXPORTS
  8. #ifdef __cplusplus
  9. #define CV_EXPORTS __attribute__ ((visibility ("default")))
  10. #else
  11. #define CV_EXPORTS
  12. #endif
  13. #endif
  14. #ifdef __cplusplus
  15. #import <vector>
  16. template <typename CV, typename OBJC> std::vector<CV> objc2cv(NSArray<OBJC*>* _Nonnull array, CV& (* _Nonnull converter)(OBJC* _Nonnull)) {
  17. std::vector<CV> ret;
  18. for (OBJC* obj in array) {
  19. ret.push_back(converter(obj));
  20. }
  21. return ret;
  22. }
  23. #define OBJC2CV(CV_CLASS, OBJC_CLASS, v, a) \
  24. std::vector<CV_CLASS> v = objc2cv<CV_CLASS, OBJC_CLASS>(a, [](OBJC_CLASS* objc) -> CV_CLASS& { return objc.nativeRef; })
  25. #define OBJC2CV_CUSTOM(CV_CLASS, OBJC_CLASS, v, a, CONV) \
  26. std::vector<CV_CLASS> v; \
  27. for (OBJC_CLASS* obj in a) { \
  28. CV_CLASS tmp = CONV(obj); \
  29. v.push_back(tmp); \
  30. }
  31. template <typename CV, typename OBJC> void cv2objc(std::vector<CV>& vector, NSMutableArray<OBJC*>* _Nonnull array, OBJC* _Nonnull (* _Nonnull converter)(CV&)) {
  32. [array removeAllObjects];
  33. for (size_t index = 0; index < vector.size(); index++) {
  34. [array addObject:converter(vector[index])];
  35. }
  36. }
  37. #define CV2OBJC(CV_CLASS, OBJC_CLASS, v, a) \
  38. cv2objc<CV_CLASS, OBJC_CLASS>(v, a, [](CV_CLASS& cv) -> OBJC_CLASS* { return [OBJC_CLASS fromNative:cv]; })
  39. #define CV2OBJC_CUSTOM(CV_CLASS, OBJC_CLASS, v, a, UNCONV) \
  40. [a removeAllObjects]; \
  41. for (size_t index = 0; index < v.size(); index++) { \
  42. OBJC_CLASS *tmp = UNCONV(v[index]); \
  43. [a addObject:tmp]; \
  44. }
  45. template <typename CV, typename OBJC> std::vector<std::vector<CV>> objc2cv2(NSArray<NSArray<OBJC*>*>* _Nonnull array, CV& (* _Nonnull converter)(OBJC* _Nonnull)) {
  46. std::vector<std::vector<CV>> ret;
  47. for (NSArray<OBJC*>* innerArray in array) {
  48. std::vector<CV> innerVector;
  49. for (OBJC* obj in innerArray) {
  50. innerVector.push_back(converter(obj));
  51. }
  52. ret.push_back(innerVector);
  53. }
  54. return ret;
  55. }
  56. #define OBJC2CV2(CV_CLASS, OBJC_CLASS, v, a) \
  57. std::vector<std::vector<CV_CLASS>> v = objc2cv2<CV_CLASS, OBJC_CLASS>(a, [](OBJC_CLASS* objc) -> CV_CLASS& { return objc.nativeRef; })
  58. template <typename CV, typename OBJC> void cv2objc2(std::vector<std::vector<CV>>& vector, NSMutableArray<NSMutableArray<OBJC*>*>* _Nonnull array, OBJC* _Nonnull (* _Nonnull converter)(CV&)) {
  59. [array removeAllObjects];
  60. for (size_t index = 0; index < vector.size(); index++) {
  61. std::vector<CV>& innerVector = vector[index];
  62. NSMutableArray<OBJC*>* innerArray = [NSMutableArray arrayWithCapacity:innerVector.size()];
  63. for (size_t index2 = 0; index2 < innerVector.size(); index2++) {
  64. [innerArray addObject:converter(innerVector[index2])];
  65. }
  66. [array addObject:innerArray];
  67. }
  68. }
  69. #define CV2OBJC2(CV_CLASS, OBJC_CLASS, v, a) \
  70. cv2objc2<CV_CLASS, OBJC_CLASS>(v, a, [](CV_CLASS& cv) -> OBJC_CLASS* { return [OBJC_CLASS fromNative:cv]; })
  71. #endif