Blender V2.61 - r43446

PyTypeList.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of blendTex library
00004 
00005 Copyright (c) 2007 The Zdeno Ash Miklas
00006 
00007 This program is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU Lesser General Public License as published by the Free Software
00009 Foundation; either version 2 of the License, or (at your option) any later
00010 version.
00011 
00012 This program is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License along with
00017 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00018 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00019 http://www.gnu.org/copyleft/lesser.txt.
00020 -----------------------------------------------------------------------------
00021 */
00022 
00027 #include "PyTypeList.h"
00028 
00029 #include <memory>
00030 #include <vector>
00031 
00032 #include <PyObjectPlus.h>
00033 
00035 PyTypeList::~PyTypeList()
00036 {
00037     // if list exists
00038     if (m_list.get() != NULL)
00039         for (PyTypeListType::iterator it = m_list->begin(); it != m_list->end(); ++it)
00040             delete *it;
00041 }
00042 
00044 bool PyTypeList::in (PyTypeObject * type)
00045 {
00046     // if list exists
00047     if (m_list.get() != NULL)
00048         // iterate items in list
00049         for (PyTypeListType::iterator it = m_list->begin(); it != m_list->end(); ++it)
00050             // if item is found, return with success
00051             if ((*it)->getType() == type) return true;
00052     // otherwise return not found
00053     return false;
00054 }
00055 
00057 void PyTypeList::add (PyTypeObject * type, const char * name)
00058 {
00059     // if list doesn't exist, create it
00060     if (m_list.get() == NULL) 
00061         m_list.reset(new PyTypeListType());
00062     if (!in(type))
00063         // add new item to list
00064         m_list->push_back(new PyTypeListItem(type, name));
00065 }
00066 
00068 bool PyTypeList::ready (void)
00069 {
00070     // if list exists
00071     if (m_list.get() != NULL)
00072         // iterate items in list
00073         for (PyTypeListType::iterator it = m_list->begin(); it != m_list->end(); ++it)
00074             // if preparation failed, report it
00075             if (PyType_Ready((*it)->getType()) < 0) return false;
00076     // success
00077     return true;
00078 }
00079 
00081 void PyTypeList::reg (PyObject * module)
00082 {
00083     // if list exists
00084     if (m_list.get() != NULL)
00085         // iterate items in list
00086         for (PyTypeListType::iterator it = m_list->begin(); it != m_list->end(); ++it)
00087         {
00088             // increase ref count
00089             Py_INCREF((*it)->getType());
00090             // add type to module
00091             PyModule_AddObject(module, (char*)(*it)->getName(), (PyObject*)(*it)->getType());
00092         }
00093 }