@@ -216,3 +216,87 @@ class CapsuleType(BaseType):
216216 def generate_clif_use (self ) -> Generator [str , None , None ]:
217217 yield (f'// CLIF use `{ self .cpp_name } ` as { self .py_name } , '
218218 'PythonCapsule, Pybind11Ignore' )
219+
220+
221+ @dataclasses .dataclass
222+ class ProtoType (BaseType ):
223+ """Wraps a C++ proto Message."""
224+
225+ def generate_header (self ) -> Generator [str , None , None ]:
226+ yield ''
227+ yield from self .generate_clif_use ()
228+ yield (f'PyObject* Clif_PyObjFrom(const { self .cpp_name } &, '
229+ '::clif::py::PostConv);' )
230+ yield (f'PyObject* Clif_PyObjFrom(std::shared_ptr<const { self .cpp_name } >,'
231+ '::clif::py::PostConv);' )
232+ yield (f'PyObject* Clif_PyObjFrom(std::unique_ptr<const { self .cpp_name } >,'
233+ '::clif::py::PostConv);' )
234+ yield ''
235+ yield f'bool Clif_PyObjAs(PyObject* input, { self .cpp_name } * output);'
236+ yield ('bool Clif_PyObjAs(PyObject* input, '
237+ f'std::unique_ptr<{ self .cpp_name } >* output);' )
238+
239+ def generate_converters (self ) -> Generator [str , None , None ]:
240+ yield ''
241+ yield (f'PyObject* Clif_PyObjFrom(const { self .cpp_name } & c, '
242+ '::clif::py::PostConv) {' )
243+ yield I + 'return pybind11::cast(c).release().ptr();'
244+ yield '}'
245+ yield (f'PyObject* Clif_PyObjFrom(std::shared_ptr<const { self .cpp_name } > c'
246+ ', ::clif::py::PostConv) {' )
247+ yield I + 'return pybind11::cast(std::move(c)).release().ptr();'
248+ yield '}'
249+ yield ''
250+ yield (f'PyObject* Clif_PyObjFrom(std::unique_ptr<const { self .cpp_name } > c'
251+ ', ::clif::py::PostConv) {' )
252+ yield I + 'return pybind11::cast(std::move(c)).release().ptr();'
253+ yield '}'
254+ yield ''
255+ yield f'bool Clif_PyObjAs(PyObject* input, { self .cpp_name } * output) {{'
256+ yield I + 'try {'
257+ yield I + I + (f'*output = pybind11::cast<{ self .cpp_name } >'
258+ '(pybind11::handle(input));' )
259+ yield I + '} catch (pybind11::cast_error) {'
260+ yield I + I + 'return false;'
261+ yield I + '}'
262+ yield I + 'return true;'
263+ yield '}'
264+ yield ''
265+ yield ('bool Clif_PyObjAs(PyObject* input, '
266+ f'std::unique_ptr<{ self .cpp_name } >* output) {{' )
267+ yield I + 'try {'
268+ yield I + I + ('*output = pybind11::cast<std::unique_ptr'
269+ f'<{ self .cpp_name } >>(pybind11::handle(input));' )
270+ yield I + '} catch (pybind11::cast_error) {'
271+ yield I + I + 'return false;'
272+ yield I + '}'
273+ yield I + 'return true;'
274+ yield '}'
275+ yield ''
276+
277+
278+ @dataclasses .dataclass
279+ class ProtoEnumType (BaseType ):
280+ """Wraps a C++ proto Enum."""
281+
282+ def generate_header (self ) -> Generator [str , None , None ]:
283+ yield ''
284+ yield from self .generate_clif_use ()
285+ yield f'PyObject* Clif_PyObjFrom({ self .cpp_name } , ::clif::py::PostConv);'
286+ yield f'bool Clif_PyObjAs(PyObject* input, { self .cpp_name } * output);'
287+
288+ def generate_converters (self ) -> Generator [str , None , None ]:
289+ yield ''
290+ yield (f'PyObject* Clif_PyObjFrom({ self .cpp_name } c, '
291+ '::clif::py::PostConv) {' )
292+ yield I + 'return pybind11::cast(c).release().ptr();'
293+ yield '}'
294+ yield f'bool Clif_PyObjAs(PyObject* input, { self .cpp_name } * output) {{'
295+ yield I + 'try {'
296+ yield I + I + (f'*output = pybind11::cast<{ self .cpp_name } >'
297+ '(pybind11::handle(input));' )
298+ yield I + '} catch (pybind11::cast_error) {'
299+ yield I + I + 'return false;'
300+ yield I + '}'
301+ yield I + 'return true;'
302+ yield '}'
0 commit comments