cyphesis/rules/python/CyPy_Root.cpp

101 lines
2.7 KiB
C++
Raw Normal View History

/*
Copyright (C) 2018 Erik Ogenvik
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "CyPy_Root.h"
#include "CyPy_Element.h"
#include <Atlas/Objects/Root.h>
using Atlas::Message::Element;
using Atlas::Objects::Root;
CyPy_Root::CyPy_Root(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds)
: WrapperBase(self, args, kwds)
{
m_value = Root();
if (args.size() == 1) {
m_value->setId(verifyString(args.front()));
}
if (kwds.size() > 0) {
for (auto key : kwds.keys()) {
auto keyStr = key.str().as_string();
auto value = kwds.getItem(key);
if (keyStr == "parent") {
m_value->setParent(verifyString(value));
} else if (keyStr == "type") {
m_value->setObjtype(verifyString(value));
} else {
m_value->setAttr(key.str(), CyPy_Element::asElement(value));
}
}
}
}
CyPy_Root::CyPy_Root(Py::PythonClassInstance* self, Atlas::Objects::Root value)
: WrapperBase(self, std::move(value))
{
}
void CyPy_Root::init_type()
{
behaviors().name("Root");
behaviors().doc("");
PYCXX_ADD_NOARGS_METHOD(get_name, get_name, "");
behaviors().readyType();
}
Py::Object CyPy_Root::get_name()
{
return Py::String(m_value->getName());
}
Py::Object CyPy_Root::getattro(const Py::String& name)
{
auto nameStr = name.as_string();
if (nameStr == "name") {
return Py::String(m_value->getName());
} else if (nameStr == "id") {
return Py::String(m_value->getId());
} else {
Element attr;
if (m_value->copyAttr(name, attr) == 0) {
if (attr.isPtr()) {
return Py::Object((PyObject*) attr.Ptr());
}
return CyPy_Element::asPyObject(attr, false);
}
}
return PythonExtensionBase::getattro(name);
}
int CyPy_Root::setattro(const Py::String& name, const Py::Object& attr)
{
auto nameStr = name.as_string();
if (nameStr == "name") {
m_value->setName(verifyString(attr));
}
m_value->setAttr(nameStr, CyPy_Element::asElement(attr));
return 0;
}