PythonObject.h 2.16 KB
Newer Older
Locatelli's avatar
Locatelli committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Nomad Instrument Control Software
 *
 * Copyright 2011 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#ifndef PYTHONOBJECT_H
#define PYTHONOBJECT_H

#include <common/base/BaseTypes.h>
#include <boost/python.hpp>
#include "MplLock.h"
#include <utility>

namespace view {
namespace mpl {

/*!
 * \brief Lock system for managing thread safe operation on Python interpreter
 */
class PythonObject {

public:

	/*!
	 * \brief constructor, lock the GIL
	 */
	PythonObject() {

	}

	PythonObject(const boost::python::object& obj) : m_Object(std::move(obj)) {

	}

	PythonObject(const PythonObject& obj) {
		m_Object = obj();
	}

	/*!
	 * \brief destructor, release the GIL
	 */
	~PythonObject() {
		reset();
	}

//	const boost::python::object& get() const {
//		return m_Object;
//	}

	void reset() {
		m_Object = boost::python::object();
	}

	const boost::python::object& operator() () const {
		return m_Object;
	}

	PythonObject& operator= (const PythonObject& obj) {
		m_Object = boost::python::object();
		m_Object = obj();
		return *this;
	}

	PythonObject& operator= (const boost::python::object& obj) {
		m_Object = boost::python::object();
		m_Object = obj;
		return *this;
	}

	boost::python::object operator[](int32 index) const {
		return m_Object[index];
	}

	boost::python::object operator[](const char* attr) const {
		return m_Object[attr];
	}

	boost::python::object attr(const char* attrstr) const {
		return m_Object.attr(attrstr);
	}

	template<typename Type> void setattr(const char* attrstr, Type set) {
		m_Object.attr(attrstr) = set;
	}

private:

	boost::python::object m_Object; //! State of interpreter to get back
};

}
}

#endif // PYTHONOBJECT_H