PythonAutoInit.h 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//=========================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//=========================================================================
#ifndef __smtk_PythonAutoInit_h
#define __smtk_PythonAutoInit_h
/*! \file */

// This file contains macros used to initialize components of SMTK
// that may defined in separate libraries but which need to be
// exposed to SMTK's core components.
//
// See smtk/model/Session.h and its subclasses for an example of how
// these macros are used to register components with the model Manager.

21
#include "smtk/common/CompilerInformation.h"
22

23
SMTK_THIRDPARTY_PRE_INCLUDE
24
#include "pybind11/pybind11.h"
25 26 27
SMTK_THIRDPARTY_POST_INCLUDE

#include "smtk/common/PythonInterpreter.h"
28 29 30

#include <iostream>

31
#define smtkPythonInitMacro(C, ModuleName, WarnOnFailure)                                          \
32 33 34 35 36 37
  static struct C##_PythonComponentInit                                                            \
  {                                                                                                \
    /* Call <mod>_AutoInit_Construct during initialization.  */                                    \
    C##_PythonComponentInit()                                                                      \
    {                                                                                              \
      smtk::common::PythonInterpreter::instance().initialize();                                    \
38 39 40 41 42
      if (!smtk::common::PythonInterpreter::instance().canFindModule(#ModuleName))                 \
      {                                                                                            \
        if (WarnOnFailure)                                                                         \
        {                                                                                          \
          std::cerr << "WARNING: \"" << #ModuleName                                                \
43
                    << "\" has been requested but cannot be imported." << std::endl;               \
44 45 46 47 48 49 50 51 52 53 54 55
          std::cerr << std::endl << "Paths searched:" << std::endl;                                \
          auto paths = smtk::common::PythonInterpreter::instance().pythonPath();                   \
          for (auto path : paths)                                                                  \
          {                                                                                        \
            std::cerr << path << std::endl;                                                        \
          }                                                                                        \
        }                                                                                          \
      }                                                                                            \
      else                                                                                         \
      {                                                                                            \
        pybind11::module mod = pybind11::module::import(#ModuleName);                              \
      }                                                                                            \
56 57 58 59 60 61
    }                                                                                              \
    /* Call <mod>_AutoInit_Destruct during finalization.  */                                       \
    ~C##_PythonComponentInit() {}                                                                  \
  } C##_PythonComponentInit_Instance;

#endif // __smtk_PythonAutoInit_h