class MTNumbelApply
{
public:
MTNumbelApply( char m_max=192,char m_min=100,char t_max=192,char t_min=1):MaxMcuNum(m_max),MinMcuNum(m_min),
MaxTerminalNumber(t_max),MinTerminalNumber(t_min)
{
if( (m_max>192) || ( m_max< 1 ) )
MaxMcuNum=192;
if( (m_min>192) || ( m_min< 1 ) )
MinMcuNum=1;
if( (t_max>192) || ( t_max< 1 ) )
MaxTerminalNumber=192;
if( (t_min>192) || ( t_min< 1 ) )
MinTerminalNumber=1;
}
~MTNumbelApply()
{
set<char>* Ptr;
map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();
for(iter_map=McuTerminalSet.begin(); iter_map!=McuTerminalSet.end(); iter_map++)
{
Ptr= iter_map->second;
if( Ptr!=NULL)
delete Ptr;
}
}
char ApplyMcuNumber(void)
{
set<char>::iterator iter=McuSet.end();
for( char i=MinMcuNum;i< MaxMcuNum;i++)
{
//McuSet.find( McuSet.begin(),McuSet.end(),i )
iter=McuSet.find( i );
if( iter==McuSet.end() )
{
McuSet.insert( i );
TerminalSetPtr= new set<char>;
if( NULL)
{
TerminalSetPtr->clear();
McuTerminalSet.insert(pair<char,set<char>*>(i,TerminalSetPtr) );
TerminalSetPtr=NULL;
return i;
}
else
return -1;
}
}
return -1;
}
char RegisterMcuNumber( char Mcu )
{
if( isMcuValid( Mcu ) ==false )
return -1;
set<char>::iterator iter=McuSet.end();
iter=McuSet.find( Mcu );
if( iter==McuSet.end() )
{
TerminalSetPtr= new set<char>;
if( NULL)
{
TerminalSetPtr->clear();
McuTerminalSet.insert(pair<char,set<char>*>(Mcu,TerminalSetPtr) );
TerminalSetPtr=NULL;
return Mcu;
}
else
return -1;
}
else
{
return -1;
}
}
char RelaseMcuNumber( char Mcu)
{
if( isMcuValid( Mcu ) ==false )
return -1;
set<char>::iterator iterator_set=McuSet.end();
map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();
iterator_set=McuSet.find( Mcu );
if( iterator_set !=McuSet.end() )
{
McuSet.erase( iterator_set );
iter_map=McuTerminalSet.find(Mcu);
if( iter_map!=McuTerminalSet.end() )
{
TerminalSetPtr=iter_map->second;
if( TerminalSetPtr!=NULL)
delete TerminalSetPtr;
McuTerminalSet.erase(Mcu);
}
return Mcu;
}
else
{
return -1;
}
}
char ApplyTerminalNumber( char Mcu)
{
if( isMcuValid( Mcu ) ==false )
return -1;
if( McuExist(Mcu)==false )
return -1;
set<char>::iterator iter;
set<char>* Ptr;
map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();
iter_map=McuTerminalSet.find( Mcu );
if( iter_map!=McuTerminalSet.end() )
{
Ptr=iter_map->second;
if( Ptr==NULL )
return -1;
for( char i=MinTerminalNumber;i< MaxTerminalNumber;i++)
{
iter= Ptr->end();
iter= Ptr->find(i);
if( iter == Ptr->end() )
{
Ptr->insert( i );
return i;
}
}
}
return -1;
}
char RegisterTerminalNumber( char Mcu, char TermiNumber)
{
if( isMcuValid( Mcu ) ==false )
return -1;
if( isTerminalValid( TermiNumber ) ==false )
return -1;
if( McuExist(Mcu)==false )
return -1;
set<char>::iterator iter;
set<char>* Ptr;
map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();
iter_map=McuTerminalSet.find( Mcu );
if( iter_map!=McuTerminalSet.end() )
{
Ptr=iter_map->second;
if( Ptr==NULL)
return -1;
iter= Ptr->end();
iter= Ptr->find( TermiNumber );
if( iter==Ptr->end() )
{
Ptr->insert( TermiNumber );
return TermiNumber;
}
else
{
return -1;
}
}
return -1;
}
char RelaseTerminalNumber( char Mcu, char TermiNumber)
{
if( isMcuValid( Mcu ) ==false )
return -1;
if( isTerminalValid( TermiNumber ) ==false )
return -1;
if( McuExist(Mcu)==false )
return -1;
set<char>::iterator iter;
set<char>* Ptr;
map<char,set<char>* >::iterator iter_map=McuTerminalSet.end();
iter_map=McuTerminalSet.find( Mcu );
if( iter_map!=McuTerminalSet.end() )
{
Ptr=iter_map->second;
if( Ptr==NULL)
return -1;
iter= Ptr->end();
iter= Ptr->find( TermiNumber );
if( iter!=Ptr->end())
{
Ptr->erase(iter);
return TermiNumber;
}
}
return -1;
}
private:
int MaxMcuNum;
int MinMcuNum;
int MaxTerminalNumber;
int MinTerminalNumber;
set<char> McuSet;
map<char,set<char>* > McuTerminalSet;
set<char>* TerminalSetPtr;
bool McuExist( char Mcu)
{
set<char>::iterator iter=McuSet.end();
iter=McuSet.find( Mcu );
if( iter==McuSet.end() )
return false;
else
return true;
}
bool isMcuValid( char Mcu )
{
if( (Mcu>MaxMcuNum) || ( Mcu<MinMcuNum ) )
return false;
else
return true;
}
bool isTerminalValid( char Terminal )
{
if( (Terminal>MaxTerminalNumber) || ( Terminal<MinTerminalNumber ) )
return false;
else
return true;
}
};
|