variable_watch: Store client data as pointers

The STL containers create extra copies which makes keeping track of the
owner of the client data much messier.
This commit is contained in:
Ben Boeckel 2013-08-02 15:43:15 -04:00 committed by Brad King
parent 1e0539cd2c
commit fc7c3b4dc8
2 changed files with 22 additions and 7 deletions

View File

@ -37,21 +37,35 @@ cmVariableWatch::cmVariableWatch()
cmVariableWatch::~cmVariableWatch()
{
cmVariableWatch::StringToVectorOfPairs::iterator svp_it;
for ( svp_it = this->WatchMap.begin();
svp_it != this->WatchMap.end(); ++svp_it )
{
cmVariableWatch::VectorOfPairs::iterator p_it;
for ( p_it = svp_it->second.begin();
p_it != svp_it->second.end(); ++p_it )
{
delete *p_it;
}
}
}
void cmVariableWatch::AddWatch(const std::string& variable,
WatchMethod method, void* client_data /*=0*/)
{
cmVariableWatch::Pair p;
p.Method = method;
p.ClientData = client_data;
cmVariableWatch::Pair* p = new cmVariableWatch::Pair;
p->Method = method;
p->ClientData = client_data;
cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
cmVariableWatch::VectorOfPairs::size_type cc;
for ( cc = 0; cc < vp->size(); cc ++ )
{
cmVariableWatch::Pair* pair = &(*vp)[cc];
cmVariableWatch::Pair* pair = (*vp)[cc];
if ( pair->Method == method )
{
delete pair;
(*vp)[cc] = p;
return;
}
@ -66,8 +80,9 @@ void cmVariableWatch::RemoveWatch(const std::string& variable,
cmVariableWatch::VectorOfPairs::iterator it;
for ( it = vp->begin(); it != vp->end(); ++it )
{
if ( it->Method == method )
if ( (*it)->Method == method )
{
delete *it;
vp->erase(it);
return;
}
@ -87,7 +102,7 @@ void cmVariableWatch::VariableAccessed(const std::string& variable,
cmVariableWatch::VectorOfPairs::const_iterator it;
for ( it = vp->begin(); it != vp->end(); it ++ )
{
it->Method(variable, access_type, it->ClientData,
(*it)->Method(variable, access_type, (*it)->ClientData,
newValue, mf);
}
}

View File

@ -70,7 +70,7 @@ protected:
Pair() : Method(0), ClientData(0) {}
};
typedef std::vector< Pair > VectorOfPairs;
typedef std::vector< Pair* > VectorOfPairs;
typedef std::map<cmStdString, VectorOfPairs > StringToVectorOfPairs;
StringToVectorOfPairs WatchMap;