/**************************************************************************** * 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.4 2006/12/04 11:11:07 ganovelli add const to IsEnabledAttribute Revision 1.3 2006/12/03 18:01:01 ganovelli versione compliant vs2005 Revision 1.2 2006/06/08 20:28:38 ganovelli Corretto IsEnabledAttribute Revision 1.1 2005/10/15 16:21:49 ganovelli Working release (compilata solo su MSVC), vector_occ � migrato da component_opt Revision 1.5 2005/07/07 13:33:52 ganovelli some comment Revision 1.4 2004/04/05 18:20:08 ganovelli Aggiunto typename 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 { /*@{*/ /*! * This class represent a vector_occ. A vector_occ is derived by a std::vector. * The characteristic of a vector_occ is that you can add (at run time) new attributes * to the container::value_type elements contained in the vector. (see the example..) * The position in memory of a traced vector is kept by the Container Allocation Table, * which is a (unique) list of vector_occ positions. */ template class vector_occ: public std::vector{ typedef vector_occ ThisType; typedef std::vector TT; public: vector_occ():std::vector(){id = ID(); ID()=ID()+1; reserve(1);} ~vector_occ(); VALUE_TYPE * Pointer2begin(){ if(TT::empty()) return (VALUE_TYPE *)id; else return &*std::vector::begin(); } 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); /// this function enable the use of an optional attribute (see...) template void EnableAttribute(){ CAT * cat = CAT::New(); cat->Insert(*this); attributes.push_back(cat); } /// this function returns true if the attribute in the template parameter is enabled /// Note: once an attribute is disabled, its data is lost (the memory freed) template bool IsEnabledAttribute() const{ typename std::list < CATBase * >::const_iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) if((*ia)->Id() == CAT::Id()) return true; return false; } /// this function disable the use of an optional attribute (see...) /// Note: once an attribute is disabled, its data is lost (the memory freed) template void DisableAttribute(){ typename 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; } } /// this function create a new attribute of type ATTR_TYPE and return an handle to /// access the value of the attribute. Ex: /// vector_occ tv; /// TempData handle = tv.NewTempData(); /// // now handle[&tv[123]] is the value of integer attribute associate with the position 123 on the vector /// // NOTE: it works also if you do some push_back, resize, pop_back, reserve that cause the relocation /// // of the vector_occ template TempData NewTempData(){ typedef typename CATEntry >::EntryType EntryTypeMulti; CATEntry::Insert(*this); EntryTypeMulti entry = CATEntry::GetEntry(Pointer2begin()); entry.Data().push_back(new Wrap< ATTR_TYPE>); ((Wrap*)entry.Data().back())->reserve(TT::capacity()); ((Wrap*)entry.Data().back())->resize(TT::size()); return TempData((Wrap*) entry.Data().back()); } /// reciprocal of NewTempData template void DeleteTempData(TempData & td){ typedef typename CATEntry >::EntryType EntryTypeMulti; CATEntry::RemoveIfEmpty(*this); EntryTypeMulti entry = CATEntry >::GetEntry(Pointer2begin); entry.Data().remove((Wrap*)td.Item()); delete ((Wrap*)td.Item()); } private: VALUE_TYPE * old_start; int id; static int & ID(){static int id; return id;} void Update(); }; /*@}*/ template void vector_occ::push_back(const VALUE_TYPE & v){ std::vector::push_back(v); Update(); typename std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)->AddDataElem(&(*(this->begin())),1); } template void vector_occ::pop_back(){ std::vector::pop_back(); Update(); } template void vector_occ::resize(const unsigned int & size){ std::vector::resize(size); Update(); typename std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)-> Resize(&(*(this->begin())),size); } template void vector_occ::reserve(const unsigned int & size){ std::vector::reserve(size); Update(); } template void vector_occ:: Update(){ typename std::list < CATBase * >::iterator ia; if(Pointer2begin() != old_start) for(ia = attributes.begin(); ia != attributes.end(); ++ia) (*ia)->Resort(old_start,Pointer2begin()); old_start = Pointer2begin(); } template vector_occ::~vector_occ(){ typename std::list < CATBase * >::iterator ia; for(ia = attributes.begin(); ia != attributes.end(); ++ia) { (*ia)->Remove(*this); delete *ia; } } }; // end namespace #endif