templates.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ###############################################################################
  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) 2013, OpenCV Foundation, all rights reserved.
  14. # Third party copyrights are property of their respective owners.
  15. #
  16. # Redistribution and use in source and binary forms, with or without modification,
  17. # are permitted provided that the following conditions are met:
  18. #
  19. # * Redistribution's of source code must retain the above copyright notice,
  20. # this list of conditions and the following disclaimer.
  21. #
  22. # * Redistribution's in binary form must reproduce the above copyright notice,
  23. # this list of conditions and the following disclaimer in the documentation
  24. # and/or other materials provided with the distribution.
  25. #
  26. # * The name of the copyright holders may not be used to endorse or promote products
  27. # derived from this software without specific prior written permission.
  28. #
  29. # This software is provided by the copyright holders and contributors "as is" and
  30. # any express or implied warranties, including, but not limited to, the implied
  31. # warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. # In no event shall the Intel Corporation or contributors be liable for any direct,
  33. # indirect, incidental, special, exemplary, or consequential damages
  34. # (including, but not limited to, procurement of substitute goods or services;
  35. # loss of use, data, or profits; or business interruption) however caused
  36. # and on any theory of liability, whether in contract, strict liability,
  37. # or tort (including negligence or otherwise) arising in any way out of
  38. # the use of this software, even if advised of the possibility of such damage.
  39. #
  40. ###############################################################################
  41. # AUTHOR: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
  42. #
  43. # LICENSE AGREEMENT
  44. # Copyright (c) 2015, 2015 The Regents of the University of California (Regents)
  45. #
  46. # Redistribution and use in source and binary forms, with or without
  47. # modification, are permitted provided that the following conditions are met:
  48. # 1. Redistributions of source code must retain the above copyright
  49. # notice, this list of conditions and the following disclaimer.
  50. # 2. Redistributions in binary form must reproduce the above copyright
  51. # notice, this list of conditions and the following disclaimer in the
  52. # documentation and/or other materials provided with the distribution.
  53. # 3. Neither the name of the University nor the
  54. # names of its contributors may be used to endorse or promote products
  55. # derived from this software without specific prior written permission.
  56. #
  57. # THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
  58. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  59. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  60. # DISCLAIMED. IN NO EVENT SHALL COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
  61. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  63. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  64. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  65. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  66. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  67. ##############################################################################
  68. from string import Template
  69. wrapper_codes_template = Template("namespace $ns {\n$defs\n}")
  70. call_template = Template("""$func($args)""")
  71. class_call_template = Template("""$obj.$func($args)""")
  72. static_class_call_template = Template("""$scope$func($args)""")
  73. wrapper_function_template = Template(""" $ret_val $func($signature)$const {
  74. return $cpp_call;
  75. }
  76. """)
  77. wrapper_function_with_def_args_template = Template(""" $ret_val $func($signature)$const {
  78. $check_args
  79. }
  80. """)
  81. wrapper_overload_def_values = [
  82. Template("""return $cpp_call;"""), Template("""if ($arg0.isUndefined())
  83. return $cpp_call;
  84. else
  85. $next"""),
  86. Template("""if ($arg0.isUndefined() && $arg1.isUndefined())
  87. return $cpp_call;
  88. else $next"""),
  89. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined())
  90. return $cpp_call;
  91. else $next"""),
  92. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined())
  93. return $cpp_call;
  94. else $next"""),
  95. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  96. $arg4.isUndefined())
  97. return $cpp_call;
  98. else $next"""),
  99. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  100. $arg4.isUndefined() && $arg5.isUndefined() )
  101. return $cpp_call;
  102. else $next"""),
  103. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  104. $arg4.isUndefined() && $arg5.isUndefined() && $arg6.isUndefined() )
  105. return $cpp_call;
  106. else $next"""),
  107. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  108. $arg4.isUndefined() && $arg5.isUndefined()&& $arg6.isUndefined() && $arg7.isUndefined())
  109. return $cpp_call;
  110. else $next"""),
  111. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  112. $arg4.isUndefined() && $arg5.isUndefined()&& $arg6.isUndefined() && $arg7.isUndefined() &&
  113. $arg8.isUndefined())
  114. return $cpp_call;
  115. else $next"""),
  116. Template("""if ($arg0.isUndefined() && $arg1.isUndefined() && $arg2.isUndefined() && $arg3.isUndefined() &&
  117. $arg4.isUndefined() && $arg5.isUndefined()&& $arg6.isUndefined() && $arg7.isUndefined()&&
  118. $arg8.isUndefined() && $arg9.isUndefined())
  119. return $cpp_call;
  120. else $next""")]
  121. emscripten_binding_template = Template("""
  122. EMSCRIPTEN_BINDINGS($binding_name) {$bindings
  123. }
  124. """)
  125. simple_function_template = Template("""
  126. emscripten::function("$js_name", &$cpp_name);
  127. """)
  128. smart_ptr_reg_template = Template("""
  129. .smart_ptr<Ptr<$cname>>("Ptr<$name>")
  130. """)
  131. overload_function_template = Template("""
  132. function("$js_name", select_overload<$ret($args)$const>(&$cpp_name)$optional);
  133. """)
  134. overload_class_function_template = Template("""
  135. .function("$js_name", select_overload<$ret($args)$const>(&$cpp_name)$optional)""")
  136. overload_class_static_function_template = Template("""
  137. .class_function("$js_name", select_overload<$ret($args)$const>(&$cpp_name)$optional)""")
  138. class_property_template = Template("""
  139. .property("$js_name", &$cpp_name)""")
  140. class_property_enum_template = Template("""
  141. .property("$js_name", binding_utils::underlying_ptr(&$cpp_name))""")
  142. ctr_template = Template("""
  143. .constructor(select_overload<$ret($args)$const>(&$cpp_name)$optional)""")
  144. smart_ptr_ctr_overload_template = Template("""
  145. .smart_ptr_constructor("$ptr_type", select_overload<$ret($args)$const>(&$cpp_name)$optional)""")
  146. function_template = Template("""
  147. .function("$js_name", &$cpp_name)""")
  148. static_function_template = Template("""
  149. .class_function("$js_name", &$cpp_name)""")
  150. constructor_template = Template("""
  151. .constructor<$signature>()""")
  152. enum_item_template = Template("""
  153. .value("$val", $cpp_val)""")
  154. enum_template = Template("""
  155. emscripten::enum_<$cpp_name>("$js_name")$enum_items;
  156. """)
  157. const_template = Template("""
  158. constant("$js_name", static_cast<long>($value));
  159. """)
  160. vector_template = Template("""
  161. emscripten::register_vector<$cType>("$js_name");
  162. """)
  163. map_template = Template("""
  164. emscripten::register_map<cpp_type_key,$cpp_type_val>("$js_name");
  165. """)
  166. class_template = Template("""
  167. emscripten::class_<$cpp_name $derivation>("$js_name")$class_templates;
  168. """)