glc_structinstance.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  http://glc-lib.sourceforge.net
00006 
00007  GLC-lib is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU Lesser General Public License as published by
00009  the Free Software Foundation; either version 3 of the License, or
00010  (at your option) any later version.
00011 
00012  GLC-lib is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU Lesser General Public License for more details.
00016 
00017  You should have received a copy of the GNU Lesser General Public License
00018  along with GLC-lib; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 *****************************************************************************/
00022 
00024 
00025 #include "glc_structinstance.h"
00026 #include "glc_structreference.h"
00027 #include "glc_structoccurence.h"
00028 
00029 // Default constructor
00030 GLC_StructInstance::GLC_StructInstance(GLC_StructReference* pStructReference)
00031 : m_pNumberOfInstance(NULL)
00032 , m_pStructReference(pStructReference)
00033 , m_ListOfOccurences()
00034 , m_RelativeMatrix()
00035 , m_Name()
00036 , m_pAttributes(NULL)
00037 {
00038         if (NULL == m_pStructReference)
00039         {
00040                 m_pStructReference= new GLC_StructReference();
00041         }
00042         m_Name= m_pStructReference->name();
00043 
00044         if (m_pStructReference->hasStructInstance())
00045         {
00046                 m_pNumberOfInstance= m_pStructReference->firstInstanceHandle()->m_pNumberOfInstance;
00047                 ++(*m_pNumberOfInstance);
00048         }
00049         else
00050         {
00051                 m_pNumberOfInstance= new int(1);
00052         }
00053         // Inform reference that an instance has been created
00054         m_pStructReference->structInstanceCreated(this);
00055         //qDebug() << "GLC_StructInstance::GLC_StructInstance : " << (*m_pNumberOfInstance) << " " << m_pNumberOfInstance;
00056 }
00057 
00058 // Create instance with a rep
00059 GLC_StructInstance::GLC_StructInstance(GLC_Rep* pRep)
00060 : m_pNumberOfInstance(NULL)
00061 , m_pStructReference(new GLC_StructReference(pRep))
00062 , m_ListOfOccurences()
00063 , m_RelativeMatrix()
00064 , m_Name(m_pStructReference->name())
00065 , m_pAttributes(NULL)
00066 {
00067         if (m_pStructReference->hasStructInstance())
00068         {
00069                 m_pNumberOfInstance= m_pStructReference->firstInstanceHandle()->m_pNumberOfInstance;
00070                 ++(*m_pNumberOfInstance);
00071         }
00072         else
00073         {
00074                 m_pNumberOfInstance= new int(1);
00075         }
00076         // Inform reference that an instance has been created
00077         m_pStructReference->structInstanceCreated(this);
00078 }
00079 
00080 // Copy constructor
00081 GLC_StructInstance::GLC_StructInstance(const GLC_StructInstance& structInstance)
00082 : m_pNumberOfInstance(structInstance.m_pNumberOfInstance)
00083 , m_pStructReference(structInstance.m_pStructReference)
00084 , m_ListOfOccurences()
00085 , m_RelativeMatrix(structInstance.m_RelativeMatrix)
00086 , m_Name(structInstance.name())
00087 , m_pAttributes(NULL)
00088 {
00089         //qDebug() << "Instance Copy constructor";
00090         Q_ASSERT(NULL != m_pStructReference);
00091         // Copy attributes if necessary
00092         if (NULL != structInstance.m_pAttributes)
00093         {
00094                 m_pAttributes= new GLC_Attributes(*(structInstance.m_pAttributes));
00095         }
00096 
00097         ++(*m_pNumberOfInstance);
00098 
00099         // Inform reference that an instance has been created
00100         m_pStructReference->structInstanceCreated(this);
00101 }
00102 
00103 // Copy constructor
00104 GLC_StructInstance::GLC_StructInstance(GLC_StructInstance* pStructInstance)
00105 : m_pNumberOfInstance(pStructInstance->m_pNumberOfInstance)
00106 , m_pStructReference(pStructInstance->m_pStructReference)
00107 , m_ListOfOccurences()
00108 , m_RelativeMatrix(pStructInstance->m_RelativeMatrix)
00109 , m_Name(pStructInstance->name())
00110 , m_pAttributes(NULL)
00111 {
00112         //qDebug() << "Instance Copy constructor";
00113         Q_ASSERT(NULL != m_pStructReference);
00114         // Copy attributes if necessary
00115         if (NULL != pStructInstance->m_pAttributes)
00116         {
00117                 m_pAttributes= new GLC_Attributes(*(pStructInstance->m_pAttributes));
00118         }
00119 
00120         ++(*m_pNumberOfInstance);
00121 
00122         // Inform reference that an instance has been created
00123         m_pStructReference->structInstanceCreated(this);
00124 }
00125 
00126 // Create empty instance
00127 GLC_StructInstance::GLC_StructInstance(const QString& name)
00128 : m_pNumberOfInstance(NULL)
00129 , m_pStructReference(NULL)
00130 , m_ListOfOccurences()
00131 , m_RelativeMatrix()
00132 , m_Name(name)
00133 , m_pAttributes(NULL)
00134 {
00135 }
00136 
00137 // Set the reference of an empty instance
00138 void GLC_StructInstance::setReference(GLC_StructReference* pStructReference)
00139 {
00140         Q_ASSERT(NULL == m_pStructReference);
00141         Q_ASSERT(NULL == m_pNumberOfInstance);
00142         m_pStructReference= pStructReference;
00143         if (m_pStructReference->hasStructInstance())
00144         {
00145                 m_pNumberOfInstance= m_pStructReference->firstInstanceHandle()->m_pNumberOfInstance;
00146                 ++(*m_pNumberOfInstance);
00147         }
00148         else
00149         {
00150                 m_pNumberOfInstance= new int(1);
00151         }
00152         // Inform reference that an instance has been created
00153         m_pStructReference->structInstanceCreated(this);
00154 
00155         if (m_Name.isEmpty())
00156         {
00157                 m_Name= pStructReference->name();
00158         }
00159 }
00160 
00161 // Destructor
00162 GLC_StructInstance::~GLC_StructInstance()
00163 {
00164         if(m_pNumberOfInstance != NULL)
00165         {
00166                 // Update number of instance
00167                 if ((--(*m_pNumberOfInstance)) == 0)
00168                 {
00169                         delete m_pStructReference;
00170                         delete m_pNumberOfInstance;
00171                 }
00172                 else
00173                 {
00174                         // Inform reference that an instance has been deleted
00175                         m_pStructReference->structInstanceDeleted(this);
00176                         Q_ASSERT(m_pStructReference->hasStructInstance());
00177                 }
00178                 delete m_pAttributes;
00179         }
00180         else qDebug() << "GLC_StructInstance::~GLC_StructInstance() of empty instance";
00181 
00182 }
00183 
00184 void GLC_StructInstance::updateOccurencesAbsoluteMatrix()
00185 {
00186         const int occurenceCount= m_ListOfOccurences.count();
00187         for (int i= 0; i < occurenceCount; ++i)
00188         {
00189                 m_ListOfOccurences.at(i)->updateChildrenAbsoluteMatrix();
00190         }
00191 }

SourceForge.net Logo

©2005-2011 Laurent Ribon