netgen-mesher 6.2.2506.post35.dev0__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (340) hide show
  1. netgen/NgOCC.py +7 -0
  2. netgen/__init__.py +114 -0
  3. netgen/__init__.pyi +22 -0
  4. netgen/__main__.py +53 -0
  5. netgen/cmake/NetgenConfig.cmake +79 -0
  6. netgen/cmake/netgen-targets-release.cmake +69 -0
  7. netgen/cmake/netgen-targets.cmake +146 -0
  8. netgen/config/__init__.py +1 -0
  9. netgen/config/__init__.pyi +52 -0
  10. netgen/config/__main__.py +4 -0
  11. netgen/config/config.py +68 -0
  12. netgen/config/config.pyi +54 -0
  13. netgen/csg.py +25 -0
  14. netgen/geom2d.py +178 -0
  15. netgen/gui.py +82 -0
  16. netgen/include/core/archive.hpp +1256 -0
  17. netgen/include/core/array.hpp +1760 -0
  18. netgen/include/core/autodiff.hpp +1131 -0
  19. netgen/include/core/autodiffdiff.hpp +733 -0
  20. netgen/include/core/bitarray.hpp +240 -0
  21. netgen/include/core/concurrentqueue.h +3619 -0
  22. netgen/include/core/exception.hpp +145 -0
  23. netgen/include/core/flags.hpp +199 -0
  24. netgen/include/core/hashtable.hpp +1281 -0
  25. netgen/include/core/localheap.hpp +318 -0
  26. netgen/include/core/logging.hpp +117 -0
  27. netgen/include/core/memtracer.hpp +221 -0
  28. netgen/include/core/mpi4py_pycapi.h +245 -0
  29. netgen/include/core/mpi_wrapper.hpp +643 -0
  30. netgen/include/core/ng_mpi.hpp +94 -0
  31. netgen/include/core/ng_mpi_generated_declarations.hpp +155 -0
  32. netgen/include/core/ng_mpi_native.hpp +25 -0
  33. netgen/include/core/ngcore.hpp +32 -0
  34. netgen/include/core/ngcore_api.hpp +152 -0
  35. netgen/include/core/ngstream.hpp +115 -0
  36. netgen/include/core/paje_trace.hpp +279 -0
  37. netgen/include/core/profiler.hpp +382 -0
  38. netgen/include/core/python_ngcore.hpp +457 -0
  39. netgen/include/core/ranges.hpp +109 -0
  40. netgen/include/core/register_archive.hpp +100 -0
  41. netgen/include/core/signal.hpp +82 -0
  42. netgen/include/core/simd.hpp +160 -0
  43. netgen/include/core/simd_arm64.hpp +407 -0
  44. netgen/include/core/simd_avx.hpp +394 -0
  45. netgen/include/core/simd_avx512.hpp +285 -0
  46. netgen/include/core/simd_generic.hpp +1053 -0
  47. netgen/include/core/simd_math.hpp +178 -0
  48. netgen/include/core/simd_sse.hpp +289 -0
  49. netgen/include/core/statushandler.hpp +37 -0
  50. netgen/include/core/symboltable.hpp +153 -0
  51. netgen/include/core/table.hpp +810 -0
  52. netgen/include/core/taskmanager.hpp +1161 -0
  53. netgen/include/core/type_traits.hpp +65 -0
  54. netgen/include/core/utils.hpp +385 -0
  55. netgen/include/core/version.hpp +102 -0
  56. netgen/include/core/xbool.hpp +47 -0
  57. netgen/include/csg/algprim.hpp +563 -0
  58. netgen/include/csg/brick.hpp +150 -0
  59. netgen/include/csg/csg.hpp +43 -0
  60. netgen/include/csg/csgeom.hpp +389 -0
  61. netgen/include/csg/csgparser.hpp +101 -0
  62. netgen/include/csg/curve2d.hpp +67 -0
  63. netgen/include/csg/edgeflw.hpp +112 -0
  64. netgen/include/csg/explicitcurve2d.hpp +113 -0
  65. netgen/include/csg/extrusion.hpp +185 -0
  66. netgen/include/csg/gencyl.hpp +70 -0
  67. netgen/include/csg/geoml.hpp +16 -0
  68. netgen/include/csg/identify.hpp +213 -0
  69. netgen/include/csg/manifold.hpp +29 -0
  70. netgen/include/csg/meshsurf.hpp +46 -0
  71. netgen/include/csg/polyhedra.hpp +121 -0
  72. netgen/include/csg/revolution.hpp +180 -0
  73. netgen/include/csg/singularref.hpp +84 -0
  74. netgen/include/csg/solid.hpp +295 -0
  75. netgen/include/csg/specpoin.hpp +194 -0
  76. netgen/include/csg/spline3d.hpp +99 -0
  77. netgen/include/csg/splinesurface.hpp +85 -0
  78. netgen/include/csg/surface.hpp +394 -0
  79. netgen/include/csg/triapprox.hpp +63 -0
  80. netgen/include/csg/vscsg.hpp +34 -0
  81. netgen/include/general/autodiff.hpp +356 -0
  82. netgen/include/general/autoptr.hpp +39 -0
  83. netgen/include/general/gzstream.h +121 -0
  84. netgen/include/general/hashtabl.hpp +1692 -0
  85. netgen/include/general/myadt.hpp +48 -0
  86. netgen/include/general/mystring.hpp +226 -0
  87. netgen/include/general/netgenout.hpp +205 -0
  88. netgen/include/general/ngarray.hpp +797 -0
  89. netgen/include/general/ngbitarray.hpp +149 -0
  90. netgen/include/general/ngpython.hpp +74 -0
  91. netgen/include/general/optmem.hpp +44 -0
  92. netgen/include/general/parthreads.hpp +138 -0
  93. netgen/include/general/seti.hpp +50 -0
  94. netgen/include/general/sort.hpp +47 -0
  95. netgen/include/general/spbita2d.hpp +59 -0
  96. netgen/include/general/stack.hpp +114 -0
  97. netgen/include/general/table.hpp +280 -0
  98. netgen/include/general/template.hpp +509 -0
  99. netgen/include/geom2d/csg2d.hpp +750 -0
  100. netgen/include/geom2d/geometry2d.hpp +280 -0
  101. netgen/include/geom2d/spline2d.hpp +234 -0
  102. netgen/include/geom2d/vsgeom2d.hpp +28 -0
  103. netgen/include/gprim/adtree.hpp +1392 -0
  104. netgen/include/gprim/geom2d.hpp +858 -0
  105. netgen/include/gprim/geom3d.hpp +749 -0
  106. netgen/include/gprim/geomfuncs.hpp +212 -0
  107. netgen/include/gprim/geomobjects.hpp +544 -0
  108. netgen/include/gprim/geomops.hpp +404 -0
  109. netgen/include/gprim/geomtest3d.hpp +101 -0
  110. netgen/include/gprim/gprim.hpp +33 -0
  111. netgen/include/gprim/spline.hpp +778 -0
  112. netgen/include/gprim/splinegeometry.hpp +73 -0
  113. netgen/include/gprim/transform3d.hpp +216 -0
  114. netgen/include/include/acisgeom.hpp +3 -0
  115. netgen/include/include/csg.hpp +1 -0
  116. netgen/include/include/geometry2d.hpp +1 -0
  117. netgen/include/include/gprim.hpp +1 -0
  118. netgen/include/include/incopengl.hpp +62 -0
  119. netgen/include/include/inctcl.hpp +13 -0
  120. netgen/include/include/incvis.hpp +6 -0
  121. netgen/include/include/linalg.hpp +1 -0
  122. netgen/include/include/meshing.hpp +1 -0
  123. netgen/include/include/myadt.hpp +1 -0
  124. netgen/include/include/mydefs.hpp +70 -0
  125. netgen/include/include/mystdlib.h +59 -0
  126. netgen/include/include/netgen_config.hpp +27 -0
  127. netgen/include/include/netgen_version.hpp +9 -0
  128. netgen/include/include/nginterface_v2_impl.hpp +395 -0
  129. netgen/include/include/ngsimd.hpp +1 -0
  130. netgen/include/include/occgeom.hpp +1 -0
  131. netgen/include/include/opti.hpp +1 -0
  132. netgen/include/include/parallel.hpp +1 -0
  133. netgen/include/include/stlgeom.hpp +1 -0
  134. netgen/include/include/visual.hpp +1 -0
  135. netgen/include/interface/rw_medit.hpp +11 -0
  136. netgen/include/interface/writeuser.hpp +80 -0
  137. netgen/include/linalg/densemat.hpp +414 -0
  138. netgen/include/linalg/linalg.hpp +29 -0
  139. netgen/include/linalg/opti.hpp +142 -0
  140. netgen/include/linalg/polynomial.hpp +47 -0
  141. netgen/include/linalg/vector.hpp +217 -0
  142. netgen/include/meshing/adfront2.hpp +274 -0
  143. netgen/include/meshing/adfront3.hpp +332 -0
  144. netgen/include/meshing/basegeom.hpp +370 -0
  145. netgen/include/meshing/bcfunctions.hpp +53 -0
  146. netgen/include/meshing/bisect.hpp +72 -0
  147. netgen/include/meshing/boundarylayer.hpp +113 -0
  148. netgen/include/meshing/classifyhpel.hpp +1984 -0
  149. netgen/include/meshing/clusters.hpp +46 -0
  150. netgen/include/meshing/curvedelems.hpp +274 -0
  151. netgen/include/meshing/delaunay2d.hpp +73 -0
  152. netgen/include/meshing/fieldlines.hpp +103 -0
  153. netgen/include/meshing/findip.hpp +198 -0
  154. netgen/include/meshing/findip2.hpp +103 -0
  155. netgen/include/meshing/geomsearch.hpp +69 -0
  156. netgen/include/meshing/global.hpp +54 -0
  157. netgen/include/meshing/hpref_hex.hpp +330 -0
  158. netgen/include/meshing/hpref_prism.hpp +3405 -0
  159. netgen/include/meshing/hpref_pyramid.hpp +154 -0
  160. netgen/include/meshing/hpref_quad.hpp +2082 -0
  161. netgen/include/meshing/hpref_segm.hpp +122 -0
  162. netgen/include/meshing/hpref_tet.hpp +4230 -0
  163. netgen/include/meshing/hpref_trig.hpp +848 -0
  164. netgen/include/meshing/hprefinement.hpp +366 -0
  165. netgen/include/meshing/improve2.hpp +178 -0
  166. netgen/include/meshing/improve3.hpp +151 -0
  167. netgen/include/meshing/localh.hpp +223 -0
  168. netgen/include/meshing/meshclass.hpp +1076 -0
  169. netgen/include/meshing/meshfunc.hpp +47 -0
  170. netgen/include/meshing/meshing.hpp +63 -0
  171. netgen/include/meshing/meshing2.hpp +163 -0
  172. netgen/include/meshing/meshing3.hpp +123 -0
  173. netgen/include/meshing/meshtool.hpp +90 -0
  174. netgen/include/meshing/meshtype.hpp +1930 -0
  175. netgen/include/meshing/msghandler.hpp +62 -0
  176. netgen/include/meshing/paralleltop.hpp +172 -0
  177. netgen/include/meshing/python_mesh.hpp +206 -0
  178. netgen/include/meshing/ruler2.hpp +172 -0
  179. netgen/include/meshing/ruler3.hpp +211 -0
  180. netgen/include/meshing/soldata.hpp +141 -0
  181. netgen/include/meshing/specials.hpp +17 -0
  182. netgen/include/meshing/surfacegeom.hpp +73 -0
  183. netgen/include/meshing/topology.hpp +1003 -0
  184. netgen/include/meshing/validate.hpp +21 -0
  185. netgen/include/meshing/visual_interface.hpp +71 -0
  186. netgen/include/mydefs.hpp +70 -0
  187. netgen/include/nginterface.h +474 -0
  188. netgen/include/nginterface_v2.hpp +406 -0
  189. netgen/include/nglib.h +697 -0
  190. netgen/include/nglib_occ.h +50 -0
  191. netgen/include/occ/occ_edge.hpp +47 -0
  192. netgen/include/occ/occ_face.hpp +52 -0
  193. netgen/include/occ/occ_solid.hpp +23 -0
  194. netgen/include/occ/occ_utils.hpp +376 -0
  195. netgen/include/occ/occ_vertex.hpp +30 -0
  196. netgen/include/occ/occgeom.hpp +659 -0
  197. netgen/include/occ/occmeshsurf.hpp +168 -0
  198. netgen/include/occ/vsocc.hpp +33 -0
  199. netgen/include/pybind11/LICENSE +29 -0
  200. netgen/include/pybind11/attr.h +722 -0
  201. netgen/include/pybind11/buffer_info.h +208 -0
  202. netgen/include/pybind11/cast.h +2361 -0
  203. netgen/include/pybind11/chrono.h +228 -0
  204. netgen/include/pybind11/common.h +2 -0
  205. netgen/include/pybind11/complex.h +74 -0
  206. netgen/include/pybind11/conduit/README.txt +15 -0
  207. netgen/include/pybind11/conduit/pybind11_conduit_v1.h +116 -0
  208. netgen/include/pybind11/conduit/pybind11_platform_abi_id.h +87 -0
  209. netgen/include/pybind11/conduit/wrap_include_python_h.h +72 -0
  210. netgen/include/pybind11/critical_section.h +56 -0
  211. netgen/include/pybind11/detail/class.h +823 -0
  212. netgen/include/pybind11/detail/common.h +1348 -0
  213. netgen/include/pybind11/detail/cpp_conduit.h +75 -0
  214. netgen/include/pybind11/detail/descr.h +226 -0
  215. netgen/include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h +39 -0
  216. netgen/include/pybind11/detail/exception_translation.h +71 -0
  217. netgen/include/pybind11/detail/function_record_pyobject.h +191 -0
  218. netgen/include/pybind11/detail/init.h +538 -0
  219. netgen/include/pybind11/detail/internals.h +799 -0
  220. netgen/include/pybind11/detail/native_enum_data.h +209 -0
  221. netgen/include/pybind11/detail/pybind11_namespace_macros.h +82 -0
  222. netgen/include/pybind11/detail/struct_smart_holder.h +378 -0
  223. netgen/include/pybind11/detail/type_caster_base.h +1591 -0
  224. netgen/include/pybind11/detail/typeid.h +65 -0
  225. netgen/include/pybind11/detail/using_smart_holder.h +22 -0
  226. netgen/include/pybind11/detail/value_and_holder.h +90 -0
  227. netgen/include/pybind11/eigen/common.h +9 -0
  228. netgen/include/pybind11/eigen/matrix.h +723 -0
  229. netgen/include/pybind11/eigen/tensor.h +521 -0
  230. netgen/include/pybind11/eigen.h +12 -0
  231. netgen/include/pybind11/embed.h +320 -0
  232. netgen/include/pybind11/eval.h +161 -0
  233. netgen/include/pybind11/functional.h +147 -0
  234. netgen/include/pybind11/gil.h +199 -0
  235. netgen/include/pybind11/gil_safe_call_once.h +102 -0
  236. netgen/include/pybind11/gil_simple.h +37 -0
  237. netgen/include/pybind11/iostream.h +265 -0
  238. netgen/include/pybind11/native_enum.h +67 -0
  239. netgen/include/pybind11/numpy.h +2312 -0
  240. netgen/include/pybind11/operators.h +202 -0
  241. netgen/include/pybind11/options.h +92 -0
  242. netgen/include/pybind11/pybind11.h +3645 -0
  243. netgen/include/pybind11/pytypes.h +2680 -0
  244. netgen/include/pybind11/stl/filesystem.h +114 -0
  245. netgen/include/pybind11/stl.h +666 -0
  246. netgen/include/pybind11/stl_bind.h +858 -0
  247. netgen/include/pybind11/subinterpreter.h +299 -0
  248. netgen/include/pybind11/trampoline_self_life_support.h +65 -0
  249. netgen/include/pybind11/type_caster_pyobject_ptr.h +61 -0
  250. netgen/include/pybind11/typing.h +298 -0
  251. netgen/include/pybind11/warnings.h +75 -0
  252. netgen/include/stlgeom/meshstlsurface.hpp +67 -0
  253. netgen/include/stlgeom/stlgeom.hpp +491 -0
  254. netgen/include/stlgeom/stlline.hpp +193 -0
  255. netgen/include/stlgeom/stltool.hpp +331 -0
  256. netgen/include/stlgeom/stltopology.hpp +419 -0
  257. netgen/include/stlgeom/vsstl.hpp +58 -0
  258. netgen/include/visualization/meshdoc.hpp +42 -0
  259. netgen/include/visualization/mvdraw.hpp +325 -0
  260. netgen/include/visualization/vispar.hpp +128 -0
  261. netgen/include/visualization/visual.hpp +28 -0
  262. netgen/include/visualization/visual_api.hpp +10 -0
  263. netgen/include/visualization/vssolution.hpp +399 -0
  264. netgen/lib/libnggui.lib +0 -0
  265. netgen/lib/ngcore.lib +0 -0
  266. netgen/lib/nglib.lib +0 -0
  267. netgen/lib/togl.lib +0 -0
  268. netgen/libnggui.dll +0 -0
  269. netgen/libngguipy.lib +0 -0
  270. netgen/libngguipy.pyd +0 -0
  271. netgen/libngpy/_NgOCC.pyi +1545 -0
  272. netgen/libngpy/__init__.pyi +7 -0
  273. netgen/libngpy/_csg.pyi +259 -0
  274. netgen/libngpy/_geom2d.pyi +323 -0
  275. netgen/libngpy/_meshing.pyi +1111 -0
  276. netgen/libngpy/_stl.pyi +131 -0
  277. netgen/libngpy.lib +0 -0
  278. netgen/libngpy.pyd +0 -0
  279. netgen/meshing.py +65 -0
  280. netgen/ngcore.dll +0 -0
  281. netgen/nglib.dll +0 -0
  282. netgen/occ.py +52 -0
  283. netgen/read_gmsh.py +259 -0
  284. netgen/read_meshio.py +22 -0
  285. netgen/stl.py +2 -0
  286. netgen/togl.dll +0 -0
  287. netgen/version.py +2 -0
  288. netgen/webgui.py +529 -0
  289. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boundarycondition.geo +16 -0
  290. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/boxcyl.geo +32 -0
  291. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/circle_on_cube.geo +27 -0
  292. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cone.geo +13 -0
  293. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cube.geo +16 -0
  294. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandring.geo +55 -0
  295. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubeandspheres.geo +21 -0
  296. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemcyl.geo +18 -0
  297. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cubemsphere.geo +19 -0
  298. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylinder.geo +12 -0
  299. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/cylsphere.geo +12 -0
  300. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/doc/ng4.pdf +0 -0
  301. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipsoid.geo +8 -0
  302. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ellipticcyl.geo +10 -0
  303. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/extrusion.geo +99 -0
  304. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/fichera.geo +24 -0
  305. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/frame.step +11683 -0
  306. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/hinge.stl +8486 -0
  307. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/lshape3d.geo +26 -0
  308. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes.geo +26 -0
  309. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/manyholes2.geo +26 -0
  310. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/matrix.geo +27 -0
  311. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/ortho.geo +11 -0
  312. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/part1.stl +2662 -0
  313. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/period.geo +33 -0
  314. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/exportNeutral.py +26 -0
  315. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/mesh.py +19 -0
  316. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/py_tutorials/shaft.geo +65 -0
  317. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/revolution.geo +18 -0
  318. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/screw.step +1694 -0
  319. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sculpture.geo +13 -0
  320. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shaft.geo +65 -0
  321. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/shell.geo +10 -0
  322. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphere.geo +8 -0
  323. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/sphereincube.geo +17 -0
  324. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/square.in2d +35 -0
  325. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarecircle.in2d +48 -0
  326. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/squarehole.in2d +47 -0
  327. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/torus.geo +8 -0
  328. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/trafo.geo +57 -0
  329. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twobricks.geo +15 -0
  330. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocubes.geo +18 -0
  331. netgen_mesher-6.2.2506.post35.dev0.data/data/share/netgen/twocyl.geo +16 -0
  332. netgen_mesher-6.2.2506.post35.dev0.dist-info/METADATA +15 -0
  333. netgen_mesher-6.2.2506.post35.dev0.dist-info/RECORD +340 -0
  334. netgen_mesher-6.2.2506.post35.dev0.dist-info/WHEEL +5 -0
  335. netgen_mesher-6.2.2506.post35.dev0.dist-info/entry_points.txt +2 -0
  336. netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/AUTHORS +1 -0
  337. netgen_mesher-6.2.2506.post35.dev0.dist-info/licenses/LICENSE +504 -0
  338. netgen_mesher-6.2.2506.post35.dev0.dist-info/top_level.txt +2 -0
  339. pyngcore/__init__.py +1 -0
  340. pyngcore/pyngcore.cp314-win_amd64.pyd +0 -0
@@ -0,0 +1,666 @@
1
+ /*
2
+ pybind11/stl.h: Transparent conversion for STL data types
3
+
4
+ Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
+
6
+ All rights reserved. Use of this source code is governed by a
7
+ BSD-style license that can be found in the LICENSE file.
8
+ */
9
+
10
+ #pragma once
11
+
12
+ #include "pybind11.h"
13
+ #include "detail/common.h"
14
+ #include "detail/descr.h"
15
+ #include "detail/type_caster_base.h"
16
+
17
+ #include <deque>
18
+ #include <initializer_list>
19
+ #include <list>
20
+ #include <map>
21
+ #include <memory>
22
+ #include <ostream>
23
+ #include <set>
24
+ #include <unordered_map>
25
+ #include <unordered_set>
26
+ #include <valarray>
27
+
28
+ // See `detail/common.h` for implementation of these guards.
29
+ #if defined(PYBIND11_HAS_OPTIONAL)
30
+ # include <optional>
31
+ #elif defined(PYBIND11_HAS_EXP_OPTIONAL)
32
+ # include <experimental/optional>
33
+ #endif
34
+
35
+ #if defined(PYBIND11_HAS_VARIANT)
36
+ # include <variant>
37
+ #endif
38
+
39
+ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
40
+ PYBIND11_NAMESPACE_BEGIN(detail)
41
+
42
+ //
43
+ // Begin: Equivalent of
44
+ // https://github.com/google/clif/blob/ae4eee1de07cdf115c0c9bf9fec9ff28efce6f6c/clif/python/runtime.cc#L388-L438
45
+ /*
46
+ The three `object_is_convertible_to_*()` functions below are
47
+ the result of converging the behaviors of pybind11 and PyCLIF
48
+ (http://github.com/google/clif).
49
+
50
+ Originally PyCLIF was extremely far on the permissive side of the spectrum,
51
+ while pybind11 was very far on the strict side. Originally PyCLIF accepted any
52
+ Python iterable as input for a C++ `vector`/`set`/`map` argument, as long as
53
+ the elements were convertible. The obvious (in hindsight) problem was that
54
+ any empty Python iterable could be passed to any of these C++ types, e.g. `{}`
55
+ was accepted for C++ `vector`/`set` arguments, or `[]` for C++ `map` arguments.
56
+
57
+ The functions below strike a practical permissive-vs-strict compromise,
58
+ informed by tens of thousands of use cases in the wild. A main objective is
59
+ to prevent accidents and improve readability:
60
+
61
+ - Python literals must match the C++ types.
62
+
63
+ - For C++ `set`: The potentially reducing conversion from a Python sequence
64
+ (e.g. Python `list` or `tuple`) to a C++ `set` must be explicit, by going
65
+ through a Python `set`.
66
+
67
+ - However, a Python `set` can still be passed to a C++ `vector`. The rationale
68
+ is that this conversion is not reducing. Implicit conversions of this kind
69
+ are also fairly commonly used, therefore enforcing explicit conversions
70
+ would have an unfavorable cost : benefit ratio; more sloppily speaking,
71
+ such an enforcement would be more annoying than helpful.
72
+
73
+ Additional checks have been added to allow types derived from `collections.abc.Set` and
74
+ `collections.abc.Mapping` (`collections.abc.Sequence` is already allowed by `PySequence_Check`).
75
+ */
76
+
77
+ inline bool object_is_instance_with_one_of_tp_names(PyObject *obj,
78
+ std::initializer_list<const char *> tp_names) {
79
+ if (PyType_Check(obj)) {
80
+ return false;
81
+ }
82
+ const char *obj_tp_name = Py_TYPE(obj)->tp_name;
83
+ for (const auto *tp_name : tp_names) {
84
+ if (std::strcmp(obj_tp_name, tp_name) == 0) {
85
+ return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+
91
+ inline bool object_is_convertible_to_std_vector(const handle &src) {
92
+ // Allow sequence-like objects, but not (byte-)string-like objects.
93
+ if (PySequence_Check(src.ptr()) != 0) {
94
+ return !PyUnicode_Check(src.ptr()) && !PyBytes_Check(src.ptr());
95
+ }
96
+ // Allow generators, set/frozenset and several common iterable types.
97
+ return (PyGen_Check(src.ptr()) != 0) || (PyAnySet_Check(src.ptr()) != 0)
98
+ || object_is_instance_with_one_of_tp_names(
99
+ src.ptr(), {"dict_keys", "dict_values", "dict_items", "map", "zip"});
100
+ }
101
+
102
+ inline bool object_is_convertible_to_std_set(const handle &src, bool convert) {
103
+ // Allow set/frozenset and dict keys.
104
+ // In convert mode: also allow types derived from collections.abc.Set.
105
+ return ((PyAnySet_Check(src.ptr()) != 0)
106
+ || object_is_instance_with_one_of_tp_names(src.ptr(), {"dict_keys"}))
107
+ || (convert && isinstance(src, module_::import("collections.abc").attr("Set")));
108
+ }
109
+
110
+ inline bool object_is_convertible_to_std_map(const handle &src, bool convert) {
111
+ // Allow dict.
112
+ if (PyDict_Check(src.ptr())) {
113
+ return true;
114
+ }
115
+ // Allow types conforming to Mapping Protocol.
116
+ // According to https://docs.python.org/3/c-api/mapping.html, `PyMappingCheck()` checks for
117
+ // `__getitem__()` without checking the type of keys. In order to restrict the allowed types
118
+ // closer to actual Mapping-like types, we also check for the `items()` method.
119
+ if (PyMapping_Check(src.ptr()) != 0) {
120
+ PyObject *items = PyObject_GetAttrString(src.ptr(), "items");
121
+ if (items != nullptr) {
122
+ bool is_convertible = (PyCallable_Check(items) != 0);
123
+ Py_DECREF(items);
124
+ if (is_convertible) {
125
+ return true;
126
+ }
127
+ } else {
128
+ PyErr_Clear();
129
+ }
130
+ }
131
+ // In convert mode: Allow types derived from collections.abc.Mapping
132
+ return convert && isinstance(src, module_::import("collections.abc").attr("Mapping"));
133
+ }
134
+
135
+ //
136
+ // End: Equivalent of clif/python/runtime.cc
137
+ //
138
+
139
+ /// Extracts an const lvalue reference or rvalue reference for U based on the type of T (e.g. for
140
+ /// forwarding a container element). Typically used indirect via forwarded_type(), below.
141
+ template <typename T, typename U>
142
+ using forwarded_type = conditional_t<std::is_lvalue_reference<T>::value,
143
+ remove_reference_t<U> &,
144
+ remove_reference_t<U> &&>;
145
+
146
+ /// Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically
147
+ /// used for forwarding a container's elements.
148
+ template <typename T, typename U>
149
+ constexpr forwarded_type<T, U> forward_like(U &&u) {
150
+ return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
151
+ }
152
+
153
+ // Checks if a container has a STL style reserve method.
154
+ // This will only return true for a `reserve()` with a `void` return.
155
+ template <typename C>
156
+ using has_reserve_method = std::is_same<decltype(std::declval<C>().reserve(0)), void>;
157
+
158
+ template <typename Type, typename Key>
159
+ struct set_caster {
160
+ using type = Type;
161
+ using key_conv = make_caster<Key>;
162
+
163
+ private:
164
+ template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
165
+ void reserve_maybe(const anyset &s, Type *) {
166
+ value.reserve(s.size());
167
+ }
168
+ void reserve_maybe(const anyset &, void *) {}
169
+
170
+ bool convert_iterable(const iterable &itbl, bool convert) {
171
+ for (const auto &it : itbl) {
172
+ key_conv conv;
173
+ if (!conv.load(it, convert)) {
174
+ return false;
175
+ }
176
+ value.insert(cast_op<Key &&>(std::move(conv)));
177
+ }
178
+ return true;
179
+ }
180
+
181
+ bool convert_anyset(const anyset &s, bool convert) {
182
+ value.clear();
183
+ reserve_maybe(s, &value);
184
+ return convert_iterable(s, convert);
185
+ }
186
+
187
+ public:
188
+ bool load(handle src, bool convert) {
189
+ if (!object_is_convertible_to_std_set(src, convert)) {
190
+ return false;
191
+ }
192
+ if (isinstance<anyset>(src)) {
193
+ value.clear();
194
+ return convert_anyset(reinterpret_borrow<anyset>(src), convert);
195
+ }
196
+ if (!convert) {
197
+ return false;
198
+ }
199
+ assert(isinstance<iterable>(src));
200
+ value.clear();
201
+ return convert_iterable(reinterpret_borrow<iterable>(src), convert);
202
+ }
203
+
204
+ template <typename T>
205
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
206
+ if (!std::is_lvalue_reference<T>::value) {
207
+ policy = return_value_policy_override<Key>::policy(policy);
208
+ }
209
+ pybind11::set s;
210
+ for (auto &&value : src) {
211
+ auto value_ = reinterpret_steal<object>(
212
+ key_conv::cast(detail::forward_like<T>(value), policy, parent));
213
+ if (!value_ || !s.add(std::move(value_))) {
214
+ return handle();
215
+ }
216
+ }
217
+ return s.release();
218
+ }
219
+
220
+ PYBIND11_TYPE_CASTER(type,
221
+ io_name("collections.abc.Set", "set") + const_name("[") + key_conv::name
222
+ + const_name("]"));
223
+ };
224
+
225
+ template <typename Type, typename Key, typename Value>
226
+ struct map_caster {
227
+ using key_conv = make_caster<Key>;
228
+ using value_conv = make_caster<Value>;
229
+
230
+ private:
231
+ template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
232
+ void reserve_maybe(const dict &d, Type *) {
233
+ value.reserve(d.size());
234
+ }
235
+ void reserve_maybe(const dict &, void *) {}
236
+
237
+ bool convert_elements(const dict &d, bool convert) {
238
+ value.clear();
239
+ reserve_maybe(d, &value);
240
+ for (const auto &it : d) {
241
+ key_conv kconv;
242
+ value_conv vconv;
243
+ if (!kconv.load(it.first.ptr(), convert) || !vconv.load(it.second.ptr(), convert)) {
244
+ return false;
245
+ }
246
+ value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
247
+ }
248
+ return true;
249
+ }
250
+
251
+ public:
252
+ bool load(handle src, bool convert) {
253
+ if (!object_is_convertible_to_std_map(src, convert)) {
254
+ return false;
255
+ }
256
+ if (isinstance<dict>(src)) {
257
+ return convert_elements(reinterpret_borrow<dict>(src), convert);
258
+ }
259
+ if (!convert) {
260
+ return false;
261
+ }
262
+ auto items = reinterpret_steal<object>(PyMapping_Items(src.ptr()));
263
+ if (!items) {
264
+ throw error_already_set();
265
+ }
266
+ assert(isinstance<iterable>(items));
267
+ return convert_elements(dict(reinterpret_borrow<iterable>(items)), convert);
268
+ }
269
+
270
+ template <typename T>
271
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
272
+ dict d;
273
+ return_value_policy policy_key = policy;
274
+ return_value_policy policy_value = policy;
275
+ if (!std::is_lvalue_reference<T>::value) {
276
+ policy_key = return_value_policy_override<Key>::policy(policy_key);
277
+ policy_value = return_value_policy_override<Value>::policy(policy_value);
278
+ }
279
+ for (auto &&kv : src) {
280
+ auto key = reinterpret_steal<object>(
281
+ key_conv::cast(detail::forward_like<T>(kv.first), policy_key, parent));
282
+ auto value = reinterpret_steal<object>(
283
+ value_conv::cast(detail::forward_like<T>(kv.second), policy_value, parent));
284
+ if (!key || !value) {
285
+ return handle();
286
+ }
287
+ d[std::move(key)] = std::move(value);
288
+ }
289
+ return d.release();
290
+ }
291
+
292
+ PYBIND11_TYPE_CASTER(Type,
293
+ io_name("collections.abc.Mapping", "dict") + const_name("[")
294
+ + key_conv::name + const_name(", ") + value_conv::name
295
+ + const_name("]"));
296
+ };
297
+
298
+ template <typename Type, typename Value>
299
+ struct list_caster {
300
+ using value_conv = make_caster<Value>;
301
+
302
+ bool load(handle src, bool convert) {
303
+ if (!object_is_convertible_to_std_vector(src)) {
304
+ return false;
305
+ }
306
+ if (isinstance<sequence>(src)) {
307
+ return convert_elements(src, convert);
308
+ }
309
+ if (!convert) {
310
+ return false;
311
+ }
312
+ // Designed to be behavior-equivalent to passing tuple(src) from Python:
313
+ // The conversion to a tuple will first exhaust the generator object, to ensure that
314
+ // the generator is not left in an unpredictable (to the caller) partially-consumed
315
+ // state.
316
+ assert(isinstance<iterable>(src));
317
+ return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
318
+ }
319
+
320
+ private:
321
+ template <typename T = Type, enable_if_t<has_reserve_method<T>::value, int> = 0>
322
+ void reserve_maybe(const sequence &s, Type *) {
323
+ value.reserve(s.size());
324
+ }
325
+ void reserve_maybe(const sequence &, void *) {}
326
+
327
+ bool convert_elements(handle seq, bool convert) {
328
+ auto s = reinterpret_borrow<sequence>(seq);
329
+ value.clear();
330
+ reserve_maybe(s, &value);
331
+ for (const auto &it : seq) {
332
+ value_conv conv;
333
+ if (!conv.load(it, convert)) {
334
+ return false;
335
+ }
336
+ value.push_back(cast_op<Value &&>(std::move(conv)));
337
+ }
338
+ return true;
339
+ }
340
+
341
+ public:
342
+ template <typename T>
343
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
344
+ if (!std::is_lvalue_reference<T>::value) {
345
+ policy = return_value_policy_override<Value>::policy(policy);
346
+ }
347
+ list l(src.size());
348
+ ssize_t index = 0;
349
+ for (auto &&value : src) {
350
+ auto value_ = reinterpret_steal<object>(
351
+ value_conv::cast(detail::forward_like<T>(value), policy, parent));
352
+ if (!value_) {
353
+ return handle();
354
+ }
355
+ PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
356
+ }
357
+ return l.release();
358
+ }
359
+
360
+ PYBIND11_TYPE_CASTER(Type,
361
+ io_name("collections.abc.Sequence", "list") + const_name("[")
362
+ + value_conv::name + const_name("]"));
363
+ };
364
+
365
+ template <typename Type, typename Alloc>
366
+ struct type_caster<std::vector<Type, Alloc>> : list_caster<std::vector<Type, Alloc>, Type> {};
367
+
368
+ template <typename Type, typename Alloc>
369
+ struct type_caster<std::deque<Type, Alloc>> : list_caster<std::deque<Type, Alloc>, Type> {};
370
+
371
+ template <typename Type, typename Alloc>
372
+ struct type_caster<std::list<Type, Alloc>> : list_caster<std::list<Type, Alloc>, Type> {};
373
+
374
+ template <typename ArrayType, typename V, size_t... I>
375
+ ArrayType vector_to_array_impl(V &&v, index_sequence<I...>) {
376
+ return {{std::move(v[I])...}};
377
+ }
378
+
379
+ // Based on https://en.cppreference.com/w/cpp/container/array/to_array
380
+ template <typename ArrayType, size_t N, typename V>
381
+ ArrayType vector_to_array(V &&v) {
382
+ return vector_to_array_impl<ArrayType, V>(std::forward<V>(v), make_index_sequence<N>{});
383
+ }
384
+
385
+ template <typename ArrayType, typename Value, bool Resizable, size_t Size = 0>
386
+ struct array_caster {
387
+ using value_conv = make_caster<Value>;
388
+
389
+ private:
390
+ std::unique_ptr<ArrayType> value;
391
+
392
+ template <bool R = Resizable, enable_if_t<R, int> = 0>
393
+ bool convert_elements(handle seq, bool convert) {
394
+ auto l = reinterpret_borrow<sequence>(seq);
395
+ value.reset(new ArrayType{});
396
+ // Using `resize` to preserve the behavior exactly as it was before PR #5305
397
+ // For the `resize` to work, `Value` must be default constructible.
398
+ // For `std::valarray`, this is a requirement:
399
+ // https://en.cppreference.com/w/cpp/named_req/NumericType
400
+ value->resize(l.size());
401
+ size_t ctr = 0;
402
+ for (const auto &it : l) {
403
+ value_conv conv;
404
+ if (!conv.load(it, convert)) {
405
+ return false;
406
+ }
407
+ (*value)[ctr++] = cast_op<Value &&>(std::move(conv));
408
+ }
409
+ return true;
410
+ }
411
+
412
+ template <bool R = Resizable, enable_if_t<!R, int> = 0>
413
+ bool convert_elements(handle seq, bool convert) {
414
+ auto l = reinterpret_borrow<sequence>(seq);
415
+ if (l.size() != Size) {
416
+ return false;
417
+ }
418
+ // The `temp` storage is needed to support `Value` types that are not
419
+ // default-constructible.
420
+ // Deliberate choice: no template specializations, for simplicity, and
421
+ // because the compile time overhead for the specializations is deemed
422
+ // more significant than the runtime overhead for the `temp` storage.
423
+ std::vector<Value> temp;
424
+ temp.reserve(l.size());
425
+ for (auto it : l) {
426
+ value_conv conv;
427
+ if (!conv.load(it, convert)) {
428
+ return false;
429
+ }
430
+ temp.emplace_back(cast_op<Value &&>(std::move(conv)));
431
+ }
432
+ value.reset(new ArrayType(vector_to_array<ArrayType, Size>(std::move(temp))));
433
+ return true;
434
+ }
435
+
436
+ public:
437
+ bool load(handle src, bool convert) {
438
+ if (!object_is_convertible_to_std_vector(src)) {
439
+ return false;
440
+ }
441
+ if (isinstance<sequence>(src)) {
442
+ return convert_elements(src, convert);
443
+ }
444
+ if (!convert) {
445
+ return false;
446
+ }
447
+ // Designed to be behavior-equivalent to passing tuple(src) from Python:
448
+ // The conversion to a tuple will first exhaust the generator object, to ensure that
449
+ // the generator is not left in an unpredictable (to the caller) partially-consumed
450
+ // state.
451
+ assert(isinstance<iterable>(src));
452
+ return convert_elements(tuple(reinterpret_borrow<iterable>(src)), convert);
453
+ }
454
+
455
+ template <typename T>
456
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
457
+ list l(src.size());
458
+ ssize_t index = 0;
459
+ for (auto &&value : src) {
460
+ auto value_ = reinterpret_steal<object>(
461
+ value_conv::cast(detail::forward_like<T>(value), policy, parent));
462
+ if (!value_) {
463
+ return handle();
464
+ }
465
+ PyList_SET_ITEM(l.ptr(), index++, value_.release().ptr()); // steals a reference
466
+ }
467
+ return l.release();
468
+ }
469
+
470
+ // Code copied from PYBIND11_TYPE_CASTER macro.
471
+ // Intentionally preserving the behavior exactly as it was before PR #5305
472
+ template <typename T_, enable_if_t<std::is_same<ArrayType, remove_cv_t<T_>>::value, int> = 0>
473
+ static handle cast(T_ *src, return_value_policy policy, handle parent) {
474
+ if (!src) {
475
+ return none().release();
476
+ }
477
+ if (policy == return_value_policy::take_ownership) {
478
+ auto h = cast(std::move(*src), policy, parent);
479
+ delete src; // WARNING: Assumes `src` was allocated with `new`.
480
+ return h;
481
+ }
482
+ return cast(*src, policy, parent);
483
+ }
484
+
485
+ // NOLINTNEXTLINE(google-explicit-constructor)
486
+ operator ArrayType *() { return &(*value); }
487
+ // NOLINTNEXTLINE(google-explicit-constructor)
488
+ operator ArrayType &() { return *value; }
489
+ // NOLINTNEXTLINE(google-explicit-constructor)
490
+ operator ArrayType &&() && { return std::move(*value); }
491
+
492
+ template <typename T_>
493
+ using cast_op_type = movable_cast_op_type<T_>;
494
+
495
+ static constexpr auto name
496
+ = const_name<Resizable>(const_name(""), const_name("typing.Annotated["))
497
+ + io_name("collections.abc.Sequence", "list") + const_name("[") + value_conv::name
498
+ + const_name("]")
499
+ + const_name<Resizable>(const_name(""),
500
+ const_name(", \"FixedSize(") + const_name<Size>()
501
+ + const_name(")\"]"));
502
+ };
503
+
504
+ template <typename Type, size_t Size>
505
+ struct type_caster<std::array<Type, Size>>
506
+ : array_caster<std::array<Type, Size>, Type, false, Size> {};
507
+
508
+ template <typename Type>
509
+ struct type_caster<std::valarray<Type>> : array_caster<std::valarray<Type>, Type, true> {};
510
+
511
+ template <typename Key, typename Compare, typename Alloc>
512
+ struct type_caster<std::set<Key, Compare, Alloc>>
513
+ : set_caster<std::set<Key, Compare, Alloc>, Key> {};
514
+
515
+ template <typename Key, typename Hash, typename Equal, typename Alloc>
516
+ struct type_caster<std::unordered_set<Key, Hash, Equal, Alloc>>
517
+ : set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
518
+
519
+ template <typename Key, typename Value, typename Compare, typename Alloc>
520
+ struct type_caster<std::map<Key, Value, Compare, Alloc>>
521
+ : map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
522
+
523
+ template <typename Key, typename Value, typename Hash, typename Equal, typename Alloc>
524
+ struct type_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>>
525
+ : map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
526
+
527
+ // This type caster is intended to be used for std::optional and std::experimental::optional
528
+ template <typename Type, typename Value = typename Type::value_type>
529
+ struct optional_caster {
530
+ using value_conv = make_caster<Value>;
531
+
532
+ template <typename T>
533
+ static handle cast(T &&src, return_value_policy policy, handle parent) {
534
+ if (!src) {
535
+ return none().release();
536
+ }
537
+ if (!std::is_lvalue_reference<T>::value) {
538
+ policy = return_value_policy_override<Value>::policy(policy);
539
+ }
540
+ // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
541
+ return value_conv::cast(*std::forward<T>(src), policy, parent);
542
+ }
543
+
544
+ bool load(handle src, bool convert) {
545
+ if (!src) {
546
+ return false;
547
+ }
548
+ if (src.is_none()) {
549
+ return true; // default-constructed value is already empty
550
+ }
551
+ value_conv inner_caster;
552
+ if (!inner_caster.load(src, convert)) {
553
+ return false;
554
+ }
555
+
556
+ value.emplace(cast_op<Value &&>(std::move(inner_caster)));
557
+ return true;
558
+ }
559
+
560
+ PYBIND11_TYPE_CASTER(Type, value_conv::name | make_caster<none>::name);
561
+ };
562
+
563
+ #if defined(PYBIND11_HAS_OPTIONAL)
564
+ template <typename T>
565
+ struct type_caster<std::optional<T>> : public optional_caster<std::optional<T>> {};
566
+
567
+ template <>
568
+ struct type_caster<std::nullopt_t> : public void_caster<std::nullopt_t> {};
569
+ #endif
570
+
571
+ #if defined(PYBIND11_HAS_EXP_OPTIONAL)
572
+ template <typename T>
573
+ struct type_caster<std::experimental::optional<T>>
574
+ : public optional_caster<std::experimental::optional<T>> {};
575
+
576
+ template <>
577
+ struct type_caster<std::experimental::nullopt_t>
578
+ : public void_caster<std::experimental::nullopt_t> {};
579
+ #endif
580
+
581
+ /// Visit a variant and cast any found type to Python
582
+ struct variant_caster_visitor {
583
+ return_value_policy policy;
584
+ handle parent;
585
+
586
+ using result_type = handle; // required by boost::variant in C++11
587
+
588
+ template <typename T>
589
+ result_type operator()(T &&src) const {
590
+ return make_caster<T>::cast(std::forward<T>(src), policy, parent);
591
+ }
592
+ };
593
+
594
+ /// Helper class which abstracts away variant's `visit` function. `std::variant` and similar
595
+ /// `namespace::variant` types which provide a `namespace::visit()` function are handled here
596
+ /// automatically using argument-dependent lookup. Users can provide specializations for other
597
+ /// variant-like classes, e.g. `boost::variant` and `boost::apply_visitor`.
598
+ template <template <typename...> class Variant>
599
+ struct visit_helper {
600
+ template <typename... Args>
601
+ static auto call(Args &&...args) -> decltype(visit(std::forward<Args>(args)...)) {
602
+ return visit(std::forward<Args>(args)...);
603
+ }
604
+ };
605
+
606
+ /// Generic variant caster
607
+ template <typename Variant>
608
+ struct variant_caster;
609
+
610
+ template <template <typename...> class V, typename... Ts>
611
+ struct variant_caster<V<Ts...>> {
612
+ static_assert(sizeof...(Ts) > 0, "Variant must consist of at least one alternative.");
613
+
614
+ template <typename U, typename... Us>
615
+ bool load_alternative(handle src, bool convert, type_list<U, Us...>) {
616
+ auto caster = make_caster<U>();
617
+ if (caster.load(src, convert)) {
618
+ value = cast_op<U>(std::move(caster));
619
+ return true;
620
+ }
621
+ return load_alternative(src, convert, type_list<Us...>{});
622
+ }
623
+
624
+ bool load_alternative(handle, bool, type_list<>) { return false; }
625
+
626
+ bool load(handle src, bool convert) {
627
+ // Do a first pass without conversions to improve constructor resolution.
628
+ // E.g. `py::int_(1).cast<variant<double, int>>()` needs to fill the `int`
629
+ // slot of the variant. Without two-pass loading `double` would be filled
630
+ // because it appears first and a conversion is possible.
631
+ if (convert && load_alternative(src, false, type_list<Ts...>{})) {
632
+ return true;
633
+ }
634
+ return load_alternative(src, convert, type_list<Ts...>{});
635
+ }
636
+
637
+ template <typename Variant>
638
+ static handle cast(Variant &&src, return_value_policy policy, handle parent) {
639
+ return visit_helper<V>::call(variant_caster_visitor{policy, parent},
640
+ std::forward<Variant>(src));
641
+ }
642
+
643
+ using Type = V<Ts...>;
644
+ PYBIND11_TYPE_CASTER(Type, ::pybind11::detail::union_concat(make_caster<Ts>::name...));
645
+ };
646
+
647
+ #if defined(PYBIND11_HAS_VARIANT)
648
+ template <typename... Ts>
649
+ struct type_caster<std::variant<Ts...>> : variant_caster<std::variant<Ts...>> {};
650
+
651
+ template <>
652
+ struct type_caster<std::monostate> : public void_caster<std::monostate> {};
653
+ #endif
654
+
655
+ PYBIND11_NAMESPACE_END(detail)
656
+
657
+ inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
658
+ #ifdef PYBIND11_HAS_STRING_VIEW
659
+ os << str(obj).cast<std::string_view>();
660
+ #else
661
+ os << (std::string) str(obj);
662
+ #endif
663
+ return os;
664
+ }
665
+
666
+ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)