Hi,
I've got a class that uses a std::list to store tags that need to be
written out to a device. I've had no problem with the lists in terms
of iterating and reading values. My problem is when I need to pop that
value off the list when I'm done with it.
In this case, I think the iterator is getting "lost" because I pop off
the value it is pointing to at the end of this function. But how do I
pop values off one at a time without the iterator getting lost? The
erase function will just have the same problem.
Instead of hacking at it, I was wondering if anyone could point me
towards the standard solution when dealing with std::list.
Thanks in advance,
T
unsigned int duty_count;
std::list<CFusionTag *> *mylist;
std::list<CFusionTag *>::iterator it;
FU_Tag fu_tag;
FusionPlugin *myplg = m_pDevice->Driver();
CFusionTag *mytag;
mylist = m_pDevice->GetWriteTagList();
for( it=mylist->begin(), duty_count = 0;
it != mylist->end() && duty_count < m_pDevice->DutyCycle();
++it, ++duty_count )
{
mytag = *it;
if ( mytag->IsRegistered() )
{
if ( mytag->ClientAccess() == mytag->AccessWriteOnly
mytag->ClientAccess() == mytag->AccessReadWrite )
{
memcpy( fu_tag.Address, mytag->Address(), strlen( mytag-
Address() ) );
fu_tag.DeviceID = m_pDevice->RKey();
fu_tag.TagID = mytag->RKey();
fu_tag.TagType = mytag->Type();
mytag->CreateFUTagValue( &fu_tag.Value );
mytag->GetValue( &fu_tag.Value );
int nret = myplg->m_PluginModules->SetTagValue( &fu_tag );
if ( nret <= 0 )
{
mytag->Quality( 0 );
}
/* DELETE THE VALUE POINTER WE CREATED */
delete fu_tag.Value;
/* POP THIS OFF THE LIST */
mylist->pop_front();
}
}
}
Okay, you are using pop_front() to delete an element. How do you know that
for loop, it may not be pointing to the first element. Wouldn't it be
item in the list. The normal algorithm for this is:
Notice that it is not being incremented in the for loop. It is only
incremented if the element is not erased.
the first element again after erasing it.