All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pyObjectFinder.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_PY_OBJECT_FINDER_H
8#define PXR_BASE_TF_PY_OBJECT_FINDER_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/base/tf/api.h"
13#include "pxr/base/tf/pyIdentity.h"
14
15#include "pxr/external/boost/python/handle.hpp"
16#include "pxr/external/boost/python/object.hpp"
17
18#include <typeinfo>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22struct Tf_PyObjectFinderBase {
23 TF_API virtual ~Tf_PyObjectFinderBase();
24 virtual pxr_boost::python::object Find(void const *objPtr) const = 0;
25};
26
27template <class T, class PtrType>
28struct Tf_PyObjectFinder : public Tf_PyObjectFinderBase {
29 virtual ~Tf_PyObjectFinder() {}
30 virtual pxr_boost::python::object Find(void const *objPtr) const {
31 using namespace pxr_boost::python;
32 TfPyLock lock;
33 void *p = const_cast<void *>(objPtr);
34 PyObject *obj = Tf_PyGetPythonIdentity(PtrType(static_cast<T *>(p)));
35 return obj ? object(handle<>(obj)) : object();
36 }
37};
38
39TF_API
40void Tf_RegisterPythonObjectFinderInternal(std::type_info const &type,
41 Tf_PyObjectFinderBase const *finder);
42
43template <class T, class PtrType>
44void Tf_RegisterPythonObjectFinder() {
45 Tf_RegisterPythonObjectFinderInternal(typeid(T),
46 new Tf_PyObjectFinder<T, PtrType>());
47}
48
49TF_API pxr_boost::python::object
50Tf_FindPythonObject(void const *objPtr, std::type_info const &type);
51
52PXR_NAMESPACE_CLOSE_SCOPE
53
54#endif // PXR_BASE_TF_PY_OBJECT_FINDER_H
Convenience class for accessing the Python Global Interpreter Lock.
Definition: pyLock.h:105