SMTK  @SMTK_VERSION@
Simulation Modeling Tool Kit
PythonAutoInit.h
Go to the documentation of this file.
1 //=========================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //=========================================================================
10 #ifndef smtk_PythonAutoInit_h
11 #define smtk_PythonAutoInit_h
12 
14 // This file contains macros used to initialize components of SMTK
15 // that may defined in separate libraries but which need to be
16 // exposed to SMTK's core components.
17 //
18 // See smtk/model/Session.h and its subclasses for an example of how
19 // these macros are used to register components with the model Manager.
20 
21 #include "smtk/common/CompilerInformation.h"
22 
23 SMTK_THIRDPARTY_PRE_INCLUDE
24 #include "pybind11/pybind11.h"
25 SMTK_THIRDPARTY_POST_INCLUDE
26 
27 #include "smtk/common/PythonInterpreter.h"
28 
29 #include <iostream>
30 
31 #define smtkPythonInitMacro(C, ModuleName, WarnOnFailure) \
32  static struct C##_PythonComponentInit \
33  { \
34  /* Call <mod>_AutoInit_Construct during initialization. */ \
35  C##_PythonComponentInit() \
36  { \
37  smtk::common::PythonInterpreter::instance().initialize(); \
38  if (!smtk::common::PythonInterpreter::instance().canFindModule(#ModuleName)) \
39  { \
40  if (WarnOnFailure) \
41  { \
42  std::cerr << "WARNING: \"" << #ModuleName \
43  << "\" has been requested but cannot be imported." << std::endl; \
44  std::cerr << std::endl << "Paths searched:" << std::endl; \
45  auto paths = smtk::common::PythonInterpreter::instance().pythonPath(); \
46  for (auto path : paths) \
47  { \
48  std::cerr << path << std::endl; \
49  } \
50  } \
51  } \
52  else \
53  { \
54  pybind11::module mod = pybind11::module::import(#ModuleName); \
55  } \
56  } \
57  /* Call <mod>_AutoInit_Destruct during finalization. */ \
58  ~C##_PythonComponentInit() {} \
59  } C##_PythonComponentInit_Instance;
60 
61 #endif // smtk_PythonAutoInit_h