Commit 28f310f1 authored by Locatelli's avatar Locatelli
Browse files

Commit custom Python Object

parent 94da3c44
/*
* 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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment