Tomographer  v5.0
Tomographer C++ Framework Documentation
common.h
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOPY_COMMON_H
29 #define TOMOPY_COMMON_H
30 
31 #include <cstdio>
32 #include <string>
33 
34 #include <pybind11/pybind11.h>
35 
36 namespace py = pybind11;
37 
38 
39 #ifdef _WIN32
40 # ifdef _tomographer_cxx_EXPORTS
41 # define TOMOGRAPHER_EXPORT __declspec(dllexport)
42 # else
43 # define TOMOGRAPHER_EXPORT __declspec(dllimport)
44 # endif
45 #else
46 # define TOMOGRAPHER_EXPORT __attribute__((visibility("default")))
47 #endif
48 
49 
50 // DEBUGGING ONLY: set TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT to cause eigen_assert() failures to abort() and dump core
51 #ifndef TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT
52 # define TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
53 #endif
55 
56 // include this AFTER eigen_assert_exception
57 #pragma GCC visibility push(default)
58 #include <pybind11/eigen.h>
59 
60 #ifdef EIGEN_NO_DEBUG
61 # error "TomographerPy requires enabled Eigen assertions, otherwise `TomographerCxxError` won't be raised as documented."
62 #endif
63 
64 #include <Eigen/Core>
65 #pragma GCC visibility pop
66 
67 #include <tomographer/tomographer_version.h>
70 
71 #include <tomographerpy/pylogger.h>
72 
73 
74 // get a demangle() function from Boost, either with boost::core::demangle() (boost >=
75 // 1.56) or boost::units::detail::demangle() (boost before that)
76 #include <boost/version.hpp>
77 #if BOOST_VERSION >= 105600
78 #include <boost/core/demangle.hpp>
79 #else
80 #include <boost/units/detail/utility.hpp>
81 namespace boost { namespace core { using boost::units::detail::demangle; } }
82 #endif
83 
84 
85 namespace tpy {
86 
88 typedef double RealType;
90 typedef int CountIntType;
91 
99 typedef Eigen::Matrix<std::complex<RealType>, Eigen::Dynamic, Eigen::Dynamic> CplxMatrixType;
100 
103 
112 inline py::module import_tomographer()
113 {
114  auto tomographer_module = py::module::import("tomographer");
115  if (PyErr_Occurred() != NULL) {
116  throw py::error_already_set();
117  }
118  const std::string module_tomographer_version =
119  tomographer_module.attr("__version__").cast<std::string>();
120  if (module_tomographer_version != TOMOGRAPHER_VERSION) {
121  throw std::runtime_error(
122  "Error: Version of compiled tomographer python module ("+module_tomographer_version +
123  ") does not match version used to compile the current module (" + std::string(TOMOGRAPHER_VERSION)
124  + "). If you updated tomographer, please recompile all dependent modules."
125  ) ;
126  }
127  const std::string this_pybind11_ver =
128  std::to_string(PYBIND11_VERSION_MAJOR) + std::string(".") +
129  std::to_string(PYBIND11_VERSION_MINOR) + std::string(".") +
130  std::to_string(PYBIND11_VERSION_PATCH) ;
131  const std::string module_tomographer_pybind11_ver =
132  tomographer_module.attr("version").attr("compile_info").attr("get")("pybind11", "").cast<std::string>();
133  if (module_tomographer_pybind11_ver != this_pybind11_ver) {
134  throw std::runtime_error(
135  "Error: Compiled tomographer's version of PyBind11 (" + module_tomographer_pybind11_ver +
136  ") does not match version used to compile the current module (" + this_pybind11_ver
137  + "). Please recompile all modules using the same PyBind11 version."
138  ) ;
139  }
140  return tomographer_module;
141 }
142 
143 
144 
145 } // namespace tpy
146 
147 
148 
149 #endif
Definition: common.h:81
T to_string(T... args)
int CountIntType
Integer type for template arguments (int)
Definition: common.h:90
double RealType
Real type for template arguments (double)
Definition: common.h:88
STL class.
Some C++ utilities, with a tad of C++11 tricks.
Define tools for Eigen&#39;s eigen_assert() to throw an exception instead of assert&#39;ing.
C++ Classes and Utilities for Python Modules.
Definition: common.h:85
py::module import_tomographer()
Import tomographer definitions into other Python modules.
Definition: common.h:112
Utilities for logging messages.