template_class_base.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. {% import 'functional.cpp' as functional %}
  2. /*
  3. * file: {{clss.name}}Bridge.cpp
  4. * author: A trusty code generator
  5. *
  6. * This file was autogenerated, do not modify.
  7. * See LICENSE for full modification and redistribution details.
  8. * Copyright 2018 The OpenCV Foundation
  9. */
  10. #include <mex.h>
  11. #include <vector>
  12. #include <string>
  13. #include <opencv2/matlab/map.hpp>
  14. #include <opencv2/matlab/bridge.hpp>
  15. #include <opencv2/core.hpp>
  16. using namespace cv;
  17. using namespace matlab;
  18. using namespace bridge;
  19. namespace {
  20. typedef std::vector<Bridge> (*)({{clss.name}}&, const std::vector<Bridge>&) MethodSignature;
  21. {% for function in clss.methods %}
  22. {% if function.constructor %}
  23. // wrapper for {{function.name}}() constructor
  24. {{ function.clss }} {{function.name}}(const std::vector<Bridge>& inputs) {
  25. {{ functional.handleInputs(function) }}
  26. {{ functional.compose(function) }}
  27. return obj;
  28. }
  29. {% else %}
  30. // wrapper for {{function.name}}() method
  31. std::vector<Bridge> {{function.name}}({{clss.name}}& inst, const std::vector<Bridge>& inputs) {
  32. std::vector<Bridge> outputs{% if function|noutputs %}({{function|noutputs}}){% endif %};
  33. {{ functional.handleInputs(function) }}
  34. {{ functional.composeWithExceptionHandler(function) }}
  35. {{ functional.handleOutputs(function) }}
  36. return outputs;
  37. }
  38. {% endif %}
  39. {% endfor %}
  40. Map<std::string, MethodSignature> createMethodMap() {
  41. Map<std::string, MethodSignature> m;
  42. {% for function in clss.methods %}
  43. m["{{function.name}}"] = &{{function.name}};
  44. {% endfor %}
  45. return m;
  46. }
  47. static const Map<std::string, MethodSignature> methods = createMethodMap();
  48. // map of created {{clss.name}} instances. Don't trust the user to keep them safe...
  49. static Map<void *, {{clss.name}}> instances;
  50. /*
  51. * {{ clss.name }}
  52. * Gateway routine
  53. * nlhs - number of return arguments
  54. * plhs - pointers to return arguments
  55. * nrhs - number of input arguments
  56. * prhs - pointers to input arguments
  57. */
  58. void mexFunction(int nlhs, mxArray* plhs[],
  59. int nrhs, const mxArray* prhs[]) {
  60. // parse the inputs
  61. Bridge method_name(prhs[0]);
  62. Bridge handle(prhs[1]);
  63. std::vector<Bridge> brhs(prhs+2, prhs+nrhs);
  64. // retrieve the instance of interest
  65. try {
  66. {{clss.name}}& inst = instances.at(handle.address());
  67. } catch (const std::out_of_range& e) {
  68. mexErrMsgTxt("Invalid object instance provided");
  69. }
  70. // invoke the correct method on the data
  71. try {
  72. std::vector<Bridge> blhs = (*methods.at(method_name))(inst, brhs);
  73. } catch (const std::out_of_range& e) {
  74. mexErrMsgTxt("Unknown method specified");
  75. }
  76. {% block postfun %}
  77. {% endblock %}
  78. {% block cleanup %}
  79. {% endblock %}
  80. }
  81. } // end namespace