/**************************************************************************** * VCGLib o o * * Visual and Computer Graphics Library o o * * _ O _ * * Copyright(C) 2004 \/)\/ * * Visual Computing Lab /\/| * * ISTI - Italian National Research Council | * * \ * * All rights reserved. * * * * 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 (http://www.gnu.org/licenses/gpl.txt) * * for more details. * * * ****************************************************************************/ /**************************************************************************** History $Log: not supported by cvs2svn $ Revision 1.3 2004/03/31 22:36:44 ganovelli First Working Release (with this comment) /****************************************************************************/ #ifndef __VCGLIB_TRACED_VECTOR__ #define __VCGLIB_TRACED_VECTOR__ #include #include #include namespace vcg { template class TVector: public std::vector{ typedef typename TVector ThisType; public: TVector():std::vector(){reserve(1);} ~TVector(); std::list < CATBase* > attributes; // override di tutte le funzioni che possono spostare // l'allocazione in memoria del container void push_back(const VALUE_TYPE & v); void pop_back(); void resize(const unsigned int & size); void reserve(const unsigned int & size); template void EnableAttribute(){ CAT * cat = new CAT(); cat->Insert(*this); attributes.push_back(cat); } template void DisableAttribute(){ std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) if((*ia)->Id() == CAT::Id()) { (*ia)->Remove(*this); delete (*ia); attributes.erase(ia); break; } } template TempData NewTempData(){ typedef typename CATEntry >::EntryType EntryTypeMulti; CATEntry::Insert(*this); EntryTypeMulti entry = CATEntry::GetEntry(&*begin()); entry.Data().push_back(new Wrap< ATTR_TYPE>); ((Wrap*)entry.Data().back())->reserve(capacity()); ((Wrap*)entry.Data().back())->resize(size()); return TempData((Wrap*) entry.Data().back()); } template void DeleteTempData(TempData & td){ typedef typename CATEntry >::EntryType EntryTypeMulti; CATEntry::RemoveIfEmpty(*this); EntryTypeMulti entry = CATEntry >::GetEntry(&*begin()); entry.Data().remove((Wrap*)td.Item()); delete ((Wrap*)td.Item()); } private: VALUE_TYPE * old_start; void Update(); }; template void TVector::push_back(const VALUE_TYPE & v){ std::vector::push_back(v); Update(); std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)->AddDataElem(&(*(this->begin())),1); } template void TVector::pop_back(){ std::vector::pop_back(); Update(); } template void TVector::resize(const unsigned int & size){ std::vector::resize(size); std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)-> Update(); } template void TVector::reserve(const unsigned int & size){ std::vector::reserve(size); Update(); } template void TVector:: Update(){ std::list < CATBase * >::iterator ia; if(&(*begin()) != old_start) for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)->Resort(old_start,&(*begin())); old_start = &(*begin()); } template TVector::~TVector(){ std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) { (*ia)->Remove(*this); delete *ia; } } }; // end namespace #endif