HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyModule.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifdef PXR_BASE_TF_PY_MODULE_H
25 #error This file should only be included once in any given source (.cpp) file.
26 #endif
27 #define PXR_BASE_TF_PY_MODULE_H
28 
29 #include "pxr/pxr.h"
30 
32 #include "pxr/base/tf/api.h"
34 
35 #include <hboost/preprocessor/cat.hpp>
36 #include <hboost/python/module.hpp>
37 
38 // Helper macros for module files. If you implement your wrappers for classes
39 // as functions named wrapClassName(), then you can create your module like
40 // this:
41 //
42 // TF_WRAP_MODULE(ModuleName) {
43 // TF_WRAP(ClassName1);
44 // TF_WRAP(ClassName2);
45 // TF_WRAP(ClassName3);
46 // }
47 //
48 
49 // Forward declare the function that will be provided that does the wrapping.
50 static void WrapModule();
51 
53 
54 TF_API
55 void Tf_PyInitWrapModule(void (*wrapModule)(),
56  const char* packageModule,
57  const char* packageName,
58  const char* packageTag,
59  const char* packageTag2);
60 
62 void HBOOST_PP_CAT(init_module_, MFB_PACKAGE_NAME)() {
63 
65  WrapModule,
66  TF_PP_STRINGIZE(MFB_PACKAGE_MODULE),
67  TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME),
68  "Wrap " TF_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME),
69  TF_PP_STRINGIZE(MFB_PACKAGE_NAME)
70  );
71 }
72 
74 
75 // When we generate hboost python bindings for a library named Foo,
76 // we generate a python package that has __init__.py and _Foo.so,
77 // and we put all the python bindings in _Foo.so. The __init__.py
78 // file imports _Foo and then publishes _Foo's symbols as its own.
79 // Since the module with the bindings is named _Foo, the PyInit routine
80 // must be named PyInit_Foo. This little block produces that function.
81 //
82 // See https://docs.python.org/3/c-api/module.html#initializing-c-modules_
83 //
84 extern "C"
86 PyObject* HBOOST_PP_CAT(PyInit__, MFB_PACKAGE_NAME)() {
87 
88  static struct PyModuleDef moduledef = {
89  PyModuleDef_HEAD_INIT,
90  TF_PP_STRINGIZE(HBOOST_PP_CAT(_, MFB_PACKAGE_NAME)), // m_name
91  0, // m_doc
92  -1, // m_size
93  NULL, // m_methods
94  0, // m_reload
95  0, // m_traverse
96  0, // m_clear
97  0, // m_free
98  };
99 
101  return hboost::python::detail::init_module(moduledef,
102  HBOOST_PP_CAT(init_module_, MFB_PACKAGE_NAME));
103 }
104 
105 // We also support the case where both the library contents and the
106 // python bindings go into libfoo.so. We still generate a package named foo
107 // but the __init__.py file in the package foo imports libfoo and
108 // publishes it's symbols as its own. Since the module with the
109 // bindings is named libfoo, the init routine must be named PyInit_libfoo.
110 // This little block produces that function.
111 //
112 // So there are two init routines in every library, but only the one
113 // that matches the name of the python module will be called by python
114 // when the module is imported. So the total cost is a 1-line
115 // function that doesn't get called.
116 //
117 extern "C"
119 PyObject* HBOOST_PP_CAT(PyInit_lib, MFB_PACKAGE_NAME)() {
120 
121  static struct PyModuleDef moduledef = {
122  PyModuleDef_HEAD_INIT,
123  TF_PP_STRINGIZE(HBOOST_PP_CAT(lib, MFB_PACKAGE_NAME)), // m_name
124  0, // m_doc
125  -1, // m_size
126  NULL, // m_methods
127  0, // m_reload
128  0, // m_traverse
129  0, // m_clear
130  0, // m_free
131  };
132 
134  return hboost::python::detail::init_module(moduledef,
135  HBOOST_PP_CAT(init_module_, MFB_PACKAGE_NAME));
136 }
137 
138 
139 #define TF_WRAP_MODULE static void WrapModule()
140 
141 // Declares and calls the class wrapper for x
142 #define TF_WRAP(x) ARCH_HIDDEN void wrap ## x (); wrap ## x ()
#define TF_API
Definition: api.h:40
PXR_NAMESPACE_OPEN_SCOPE TF_API void Tf_PyInitWrapModule(void(*wrapModule)(), const char *packageModule, const char *packageName, const char *packageTag, const char *packageTag2)
#define TF_PP_STRINGIZE(x)
Expand and convert the argument to a string, using a most minimal macro.
#define ARCH_EXPORT
Definition: export.h:181
#define PXR_NAMESPACE_USING_DIRECTIVE
Definition: pxr.h:94
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
ARCH_EXPORT void HBOOST_PP_CAT(init_module_, MFB_PACKAGE_NAME)()
Definition: pyModule.h:62