All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyModule.h
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 TF_PYMODULE_H
25 #error This file should only be included once in any given source (.cpp) file.
26 #endif
27 #define TF_PYMODULE_H
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxr/base/arch/attributes.h"
32 #include "pxr/base/tf/api.h"
33 
34 #include <boost/preprocessor/cat.hpp>
35 #include <boost/preprocessor/stringize.hpp>
36 #include <boost/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 
52 PXR_NAMESPACE_OPEN_SCOPE
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 
61 ARCH_EXPORT
62 void BOOST_PP_CAT(init_module_, MFB_PACKAGE_NAME)() {
63 
64  Tf_PyInitWrapModule(
65  WrapModule,
66  BOOST_PP_STRINGIZE(MFB_PACKAGE_MODULE),
67  BOOST_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME),
68  "Wrap " BOOST_PP_STRINGIZE(MFB_ALT_PACKAGE_NAME),
69  BOOST_PP_STRINGIZE(MFB_PACKAGE_NAME)
70  );
71 }
72 
73 PXR_NAMESPACE_CLOSE_SCOPE
74 
75 // When we generate boost 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 init routine
80 // must be named init_Foo. This little block produces that function.
81 //
82 extern "C"
83 ARCH_EXPORT
84 void BOOST_PP_CAT(init_, MFB_PACKAGE_NAME)() {
85  PXR_NAMESPACE_USING_DIRECTIVE
86  boost::python::detail::init_module
87  (BOOST_PP_STRINGIZE(BOOST_PP_CAT(_,MFB_PACKAGE_NAME)),
88  BOOST_PP_CAT(&init_module_, MFB_PACKAGE_NAME));
89 }
90 
91 // We also support the case where both the library contents and the
92 // python bindings go into libfoo.so. We still generate a package named foo
93 // but the __init__.py file in the package foo imports libfoo and
94 // publishes it's symbols as its own. Since the module with the
95 // bindings is named libfoo, the init routine must be named initlibfoo.
96 // This little block produces that function.
97 //
98 // So there are two init routines in every library, but only the one
99 // that matches the name of the python module will be called by python
100 // when the module is imported. So the total cost is a 1-line
101 // function that doesn't get called.
102 //
103 extern "C"
104 ARCH_EXPORT
105 void BOOST_PP_CAT(initlib, MFB_PACKAGE_NAME)() {
106  PXR_NAMESPACE_USING_DIRECTIVE
107  boost::python::detail::init_module
108  (BOOST_PP_STRINGIZE(BOOST_PP_CAT(lib,MFB_PACKAGE_NAME)),
109  BOOST_PP_CAT(&init_module_, MFB_PACKAGE_NAME));
110 }
111 
112 #define TF_WRAP_MODULE static void WrapModule()
113 
114 // Declares and calls the class wrapper for x
115 #define TF_WRAP(x) ARCH_HIDDEN void wrap ## x (); wrap ## x ()