Blender  V2.59
PyTypeList.cpp
Go to the documentation of this file.
00001 
00004 /* $Id: PyTypeList.cpp 35176 2011-02-25 13:39:34Z jesterking $
00005 -----------------------------------------------------------------------------
00006 This source file is part of blendTex library
00007 
00008 Copyright (c) 2007 The Zdeno Ash Miklas
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 
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 }