{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Preprocessor and macros\n", "\n", "## Export macro values into the python module\n", "\n", "Some `#define` macros can be exported:\n", "\n", "Simple preprocessor defines can be exported as global variables, e.g.:\n", "```cpp\n", " #define MYLIB_VALUE 1\n", " #define MYLIB_FLOAT 1.5\n", " #define MYLIB_STRING \"abc\"\n", " #define MYLIB_HEX_VALUE 0x00010009\n", "```\n", "This is limited to *simple* defines.\n", "\n", "You can also apply a simple renaming to the macro value: see example below.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
    #define MYLIB_VALUE 1\n",
       "    #define MYLIB_FLOAT 1.5\n",
       "    #define MYLIB_STRING "abc"\n",
       "    #define MYLIB_HEX_VALUE 0x00010009\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
VALUE = 1\n",
       "FLOAT = 1.5\n",
       "STRING = "abc"\n",
       "HEX_VALUE = 0x00010009\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
m.attr("VALUE") = 1;\n",
       "m.attr("FLOAT") = 1.5;\n",
       "m.attr("STRING") = "abc";\n",
       "m.attr("HEX_VALUE") = 0x00010009;\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cpp_code = \"\"\"\n", " #define MYLIB_VALUE 1\n", " #define MYLIB_FLOAT 1.5\n", " #define MYLIB_STRING \"abc\"\n", " #define MYLIB_HEX_VALUE 0x00010009\n", "\"\"\"\n", "\n", "import litgen\n", "from litgen.demo import litgen_demo\n", "\n", "options = litgen.LitgenOptions()\n", "options.macro_define_include_by_name__regex = \"^MYLIB_\"\n", "# Suppress the \"MYLIB_\" prefix:\n", "options.macro_name_replacements.add_first_replacement(r\"^MYLIB_([A-Z_]*)\", r\"\\1\")\n", "litgen_demo.demo(options, cpp_code, show_pydef=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set numeric macro values\n", "\n", "Sometimes, it is necessary to tell litgen the value of certain numeric macros. In the example below, the member `values` can be exposed as a numpy array, but litgen needs to know it size.\n", "\n", "We set it via the option:\n", "```python\n", " options.srcmlcpp_options.named_number_macros[\"MY_COUNT\"] = 256\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
#define MY_COUNT 256\n",
       "\n",
       "struct Foo\n",
       "{\n",
       "    int values[MY_COUNT];\n",
       "};\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", " \n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
class Foo:\n",
       "    values: np.ndarray  # ndarray[type=int, size=256]\n",
       "    def __init__(self) -> None:\n",
       "        """Auto-generated default constructor"""\n",
       "        pass\n",
       "
\n", "\n", "
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
\n", "
auto pyClassFoo =\n",
       "    py::class_<Foo>\n",
       "        (m, "Foo", "")\n",
       "    .def(py::init<>()) // implicit default constructor \n",
       "    .def_property("values",\n",
       "        [](Foo &self) -> pybind11::array\n",
       "        {\n",
       "            auto dtype = pybind11::dtype(pybind11::format_descriptor<int>::format());\n",
       "            auto base = pybind11::array(dtype, {256}, {sizeof(int)});\n",
       "            return pybind11::array(dtype, {256}, {sizeof(int)}, self.values, base);\n",
       "        }, [](Foo& self) {},\n",
       "        "")\n",
       "    ;\n",
       "
\n", "\n", "
\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cpp_code = \"\"\"\n", "#define MY_COUNT 256\n", "\n", "struct Foo\n", "{\n", " int values[MY_COUNT];\n", "};\n", "\"\"\"\n", "\n", "options = litgen.LitgenOptions()\n", "options.srcmlcpp_options.named_number_macros[\"MY_COUNT\"] = 256\n", "\n", "litgen_demo.demo(options, cpp_code)" ] } ], "metadata": { "kernelspec": { "display_name": "venv311", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.0" } }, "nbformat": 4, "nbformat_minor": 2 }