functional.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * compose
  3. * compose a function call
  4. * This macro takes as input a Method object and composes
  5. * a function call by inspecting the types and argument names
  6. */
  7. {% macro compose(fun) %}
  8. {# ----------- Return type ------------- #}
  9. {%- if not fun.rtp|void and not fun.constructor -%} retval = {% endif -%}
  10. {%- if fun.constructor -%}{{fun.clss}} obj = {% endif -%}
  11. {%- if fun.clss and not fun.constructor -%}inst.{%- else -%} cv:: {%- endif -%}
  12. {{fun.name}}(
  13. {#- ----------- Required ------------- -#}
  14. {%- for arg in fun.req -%}
  15. {%- if arg.ref == '*' -%}&{%- endif -%}
  16. {{arg.name}}
  17. {%- if not loop.last %}, {% endif %}
  18. {% endfor %}
  19. {#- ----------- Optional ------------- -#}
  20. {% if fun.req and fun.opt %}, {% endif %}
  21. {%- for opt in fun.opt -%}
  22. {%- if opt.ref == '*' -%}&{%- endif -%}
  23. {{opt.name}}
  24. {%- if not loop.last -%}, {% endif %}
  25. {%- endfor -%}
  26. );
  27. {%- endmacro %}
  28. /*
  29. * composeMatlab
  30. * compose a Matlab function call
  31. * This macro takes as input a Method object and composes
  32. * a Matlab function call by inspecting the types and argument names
  33. */
  34. {% macro composeMatlab(fun) %}
  35. {# ----------- Return type ------------- #}
  36. {%- if fun|noutputs > 1 -%}[{% endif -%}
  37. {%- if not fun.rtp|void -%}LVALUE{% endif -%}
  38. {%- if not fun.rtp|void and fun|noutputs > 1 -%},{% endif -%}
  39. {# ------------- Outputs ------------- -#}
  40. {%- for arg in fun.req|outputs + fun.opt|outputs -%}
  41. {{arg.name}}
  42. {%- if arg.I -%}_out{%- endif -%}
  43. {%- if not loop.last %}, {% endif %}
  44. {% endfor %}
  45. {%- if fun|noutputs > 1 -%}]{% endif -%}
  46. {%- if fun|noutputs %} = {% endif -%}
  47. cv.{{fun.name}}(
  48. {#- ------------ Inputs -------------- -#}
  49. {%- for arg in fun.req|inputs + fun.opt|inputs -%}
  50. {{arg.name}}
  51. {%- if arg.O -%}_in{%- endif -%}
  52. {%- if not loop.last %}, {% endif -%}
  53. {% endfor -%}
  54. );
  55. {%- endmacro %}
  56. /*
  57. * composeVariant
  58. * compose a variant call for the ArgumentParser
  59. */
  60. {% macro composeVariant(fun) %}
  61. addVariant("{{ fun.name }}", {{ fun.req|inputs|length }}, {{ fun.opt|inputs|length }}
  62. {%- if fun.opt|inputs|length %}, {% endif -%}
  63. {%- for arg in fun.opt|inputs -%}
  64. "{{arg.name}}"
  65. {%- if not loop.last %}, {% endif -%}
  66. {% endfor -%}
  67. )
  68. {%- endmacro %}
  69. /*
  70. * composeWithExceptionHandler
  71. * compose a function call wrapped in exception traps
  72. * This macro takes an input a Method object and composes a function
  73. * call through the compose() macro, then wraps the return in traps
  74. * for cv::Exceptions, std::exceptions, and all generic exceptions
  75. * and returns a useful error message to the Matlab interpreter
  76. */
  77. {%- macro composeWithExceptionHandler(fun) -%}
  78. // call the opencv function
  79. // [out =] namespace.fun(src1, ..., srcn, dst1, ..., dstn, opt1, ..., optn);
  80. try {
  81. {{ compose(fun) }}
  82. } catch(const cv::Exception& e) {
  83. error(std::string("cv::exception caught: ").append(e.what()).c_str());
  84. } catch(const std::exception& e) {
  85. error(std::string("std::exception caught: ").append(e.what()).c_str());
  86. } catch(...) {
  87. error("Uncaught exception occurred in {{fun.name}}");
  88. }
  89. {%- endmacro %}
  90. /*
  91. * handleInputs
  92. * unpack input arguments from the Bridge
  93. * Given an input Bridge object, this unpacks the object from the Bridge and
  94. * casts them into the correct type
  95. */
  96. {%- macro handleInputs(fun) %}
  97. {% if fun|ninputs or (fun|noutputs and not fun.constructor) %}
  98. // - inputs
  99. {% for arg in fun.req|inputs %}
  100. {{arg.tp}} {{arg.name}} = inputs[{{ loop.index0 }}].to{{arg.tp|toUpperCamelCase}}();
  101. {% endfor %}
  102. // - inputs (opt)
  103. {% for opt in fun.opt|inputs %}
  104. {{opt.tp}} {{opt.name}} = inputs[{{loop.index0 + fun.req|inputs|length}}].empty() ? ({{opt.tp}}) {% if opt.ref == '*' -%} {{opt.tp}}() {%- else -%} {{opt.default}} {%- endif %} : inputs[{{loop.index0 + fun.req|inputs|length}}].to{{opt.tp|toUpperCamelCase}}();
  105. {% endfor %}
  106. // - outputs
  107. {% for arg in fun.req|only|outputs %}
  108. {{arg.tp}} {{arg.name}};
  109. {% endfor %}
  110. // - outputs (opt)
  111. {% for opt in fun.opt|only|outputs %}
  112. {{opt.tp}} {{opt.name}};
  113. {% endfor %}
  114. // - return
  115. {% if not fun.rtp|void and not fun.constructor %}
  116. {{fun.rtp}} retval;
  117. {% endif %}
  118. {% endif %}
  119. {%- endmacro %}
  120. /*
  121. * handleOutputs
  122. * pack outputs into the bridge
  123. * Given a set of outputs, this methods assigns them into the bridge for
  124. * return to the calling method
  125. */
  126. {%- macro handleOutputs(fun) %}
  127. {% if fun|noutputs %}
  128. // assign the outputs into the bridge
  129. {% if not fun.rtp|void and not fun.constructor %}
  130. outputs[0] = retval;
  131. {% endif %}
  132. {% for arg in fun.req|outputs %}
  133. outputs[{{loop.index0 + fun.rtp|void|not}}] = {{arg.name}};
  134. {% endfor %}
  135. {% for opt in fun.opt|outputs %}
  136. outputs[{{loop.index0 + fun.rtp|void|not + fun.req|outputs|length}}] = {{opt.name}};
  137. {% endfor %}
  138. {% endif %}
  139. {%- endmacro %}