Nektar++
Loading...
Searching...
No Matches
NekPyConfig.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekPyConfig.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: NekPy configuration to include boost headers and define
32// commonly-used macros.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIBRARY_LIBUTILITIES_PYTHON_NEKPYCONFIG_HPP
37#define NEKTAR_LIBRARY_LIBUTILITIES_PYTHON_NEKPYCONFIG_HPP
38
39#define PYBIND11_DETAILED_ERROR_MESSAGES
40
41#include <memory>
42#include <pybind11/numpy.h>
43#include <pybind11/pybind11.h>
44#include <pybind11/stl.h>
45#include <pybind11/stl_bind.h>
46
48
49namespace py = pybind11;
50
51// Define some common STL opaque types
52PYBIND11_MAKE_OPAQUE(std::vector<unsigned int>)
53
54#define SIZENAME(s) SIZE_##s
55#define NEKPY_WRAP_ENUM(MOD, ENUMNAME, MAPNAME) \
56 { \
57 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
58 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
59 { \
60 tmp.value(MAPNAME[a], (ENUMNAME)a); \
61 } \
62 tmp.export_values(); \
63 }
64#define NEKPY_WRAP_ENUM_STRING(MOD, ENUMNAME, MAPNAME) \
65 { \
66 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
67 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
68 { \
69 tmp.value(MAPNAME[a].c_str(), (ENUMNAME)a); \
70 } \
71 tmp.export_values(); \
72 }
73#define NEKPY_WRAP_ENUM_STRING_DOCS(MOD, ENUMNAME, MAPNAME, DOCSTRING) \
74 { \
75 py::enum_<ENUMNAME> tmp(MOD, #ENUMNAME); \
76 for (int a = 0; a < (int)SIZENAME(ENUMNAME); ++a) \
77 { \
78 tmp.value(MAPNAME[a].c_str(), (ENUMNAME)a); \
79 } \
80 tmp.export_values(); \
81 PyTypeObject *pto = reinterpret_cast<PyTypeObject *>(tmp.ptr()); \
82 PyDict_SetItemString(pto->tp_dict, "__doc__", \
83 PyUnicode_FromString(DOCSTRING)); \
84 }
85
86/**
87 * @brief Helper structure to construct C++ command line `argc` and `argv`
88 * variables from a Python list.
89 */
91{
92 /**
93 * @brief Constructor.
94 *
95 * @param py_argv List of command line arguments from Python.
96 */
97 PyCppCommandLine(py::list &py_argv) : Nektar::LibUtilities::CppCommandLine()
98 {
99 int argc = py::len(py_argv);
100 std::vector<std::string> argv(argc);
101
102 for (int i = 0; i < argc; ++i)
103 {
104 argv[i] = py::cast<std::string>(py_argv[i]);
105 }
106
107 Setup(argv);
108 }
109};
110
111#endif
PYBIND11_MAKE_OPAQUE(LibUtilities::FieldMetaDataMap)
Helper structure to construct C++ command line argc and argv variables from a C++ vector.
void Setup(std::vector< std::string > &argv)
Helper structure to construct C++ command line argc and argv variables from a Python list.
PyCppCommandLine(py::list &py_argv)
Constructor.