Mantid
Loading...
Searching...
No Matches
Copyable.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8#include <boost/python/class.hpp>
9#include <boost/python/dict.hpp>
10#include <boost/python/extract.hpp>
11#include <boost/python/import.hpp>
12#include <boost/python/manage_new_object.hpp>
13namespace Mantid {
14namespace PythonInterface {
15
16// Implementation based on
17// https://mail.python.org/pipermail/cplusplus-sig/2009-May/014505.html
18namespace bp = boost::python;
19
23template <typename T> inline PyObject *managingPyObject(T *p) {
24 return typename bp::manage_new_object::apply<T *>::type()(p);
25}
26
31template <typename Copyable> boost::python::object generic__copy__(const bp::object &copyable) {
32 Copyable *newCopyable(new Copyable(bp::extract<const Copyable &>(copyable)));
33 bp::object result(bp::detail::new_reference(managingPyObject(newCopyable)));
34
35 bp::extract<bp::dict>(result.attr("__dict__"))().update(copyable.attr("__dict__"));
36
37 return result;
38}
39
44template <typename Copyable> bp::object generic__deepcopy__(const bp::object &copyable, bp::dict &memo) {
45 bp::object copyMod = bp::import("copy");
46 bp::object deepcopy = copyMod.attr("deepcopy");
47
48 Copyable *newCopyable(new Copyable(bp::extract<const Copyable &>(copyable)));
49 bp::object result(bp::detail::new_reference(managingPyObject(newCopyable)));
50
51 // HACK: copyableId shall be the same as the result of id(copyable)
52 auto copyableId = (std::ptrdiff_t)(copyable.ptr());
53 memo[copyableId] = result;
54
55 bp::extract<bp::dict>(result.attr("__dict__"))().update(
56 deepcopy(bp::extract<bp::dict>(copyable.attr("__dict__"))(), memo));
57
58 return result;
59}
60} // namespace PythonInterface
61} // namespace Mantid
bp::object generic__deepcopy__(const bp::object &copyable, bp::dict &memo)
Create a deep copy of type Copyable providing Copyable is newable and has a public copy constructor.
Definition: Copyable.h:44
boost::python::object generic__copy__(const bp::object &copyable)
Create a shallow copy of type Copyable providing Copyable is newable and has a public copy constructo...
Definition: Copyable.h:31
PyObject * managingPyObject(T *p)
Create a new object handle around arg.
Definition: Copyable.h:23
Helper class which provides the Collimation Length for SANS instruments.