LvArray
PyFunc.hpp
Go to the documentation of this file.
1 /*
2  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3  * Copyright (c) 2019, Lawrence Livermore National Security, LLC.
4  *
5  * Produced at the Lawrence Livermore National Laboratory
6  *
7  * LLNL-CODE-746361
8  *
9  * All rights reserved. See COPYRIGHT for details.
10  *
11  * This file is part of the GEOSX Simulation Framework.
12  *
13  * GEOSX is a free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License (as published by the
15  * Free Software Foundation) version 2.1 dated February 1999.
16  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17  */
18 
23 #pragma once
24 
25 // Source includes
27 #include "numpyHelpers.hpp"
28 #include "PySortedArray.hpp"
29 #include "pythonHelpers.hpp"
30 #include "../typeManipulation.hpp"
31 
32 namespace LvArray
33 {
34 namespace python
35 {
36 
37 namespace internal
38 {
39 
44 bool err();
45 
53 void callPyFunc( PyObject * func, PyObjectRef<> * args, long long const argc );
54 
55 } // namespace internal
56 
61 template< typename ... ARGS >
63 {
64 public:
65 
70  PythonFunction( PyObject * pyfunc ):
71  m_function( pyfunc )
72  { internal::xincref( pyfunc ); }
73 
80  void operator()( ARGS ... args )
81  {
82  constexpr long long ARGC = sizeof ... (args);
83  PyObjectRef<> pyArgs[ ARGC ];
84 
85  long long i = 0;
86  typeManipulation::forEachArg( [&i, &pyArgs]( auto & arg )
87  {
88  pyArgs[ i ] = create( arg );
89  ++i;
90  }, args ... );
91 
92  for( i = 0; i < ARGC; ++i )
93  {
94  if( pyArgs[ i ] == nullptr )
95  { throw PythonError(); }
96  }
97 
98  // callPyFunc steals all of the pyArgs references
99  internal::callPyFunc( m_function, pyArgs, ARGC );
100 
101  if( internal::err() )
102  { throw PythonError(); }
103  }
104 
105 private:
108 };
109 
110 
111 } // namespace python
112 } // namespace LvArray
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(T &value)
Create a NumPy 1D array of length 1 containing the scalar value.
Definition: numpyHelpers.hpp:147
PyObjectRef m_function
A reference to the wrapped python function.
Definition: PyFunc.hpp:107
void operator()(ARGS ... args)
Call the Python function with arguments args.
Definition: PyFunc.hpp:80
PythonFunction(PyObject *pyfunc)
create a PythonFunction around pyfunc.
Definition: PyFunc.hpp:70
Base class for all C++ exceptions related to Python.
Definition: pythonHelpers.hpp:332
DISABLE_HD_WARNING constexpr LVARRAY_HOST_DEVICE void forEachArg(F &&f)
The recursive base case where no argument is provided.
Definition: typeManipulation.hpp:129
Forward declarations of Python Objects.
A class that manages an owned Python reference with RAII semantics.
Definition: pythonHelpers.hpp:149
The top level namespace.
Definition: Array.hpp:24
Contains methods to help with conversion to python objects.
A C++ functor wrapper around a Python function.
Definition: PyFunc.hpp:62