From fc7c3b4dc8522ad489a6fb67ac6030f302c65df3 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Fri, 2 Aug 2013 15:43:15 -0400 Subject: 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. --- Source/cmVariableWatch.cxx | 27 +++++++++++++++++++++------ Source/cmVariableWatch.h | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Source/cmVariableWatch.cxx b/Source/cmVariableWatch.cxx index 3905e9b..21b910d 100644 --- a/Source/cmVariableWatch.cxx +++ b/Source/cmVariableWatch.cxx @@ -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); } } diff --git a/Source/cmVariableWatch.h b/Source/cmVariableWatch.h index 7dd4ac5..45273e5 100644 --- a/Source/cmVariableWatch.h +++ b/Source/cmVariableWatch.h @@ -70,7 +70,7 @@ protected: Pair() : Method(0), ClientData(0) {} }; - typedef std::vector< Pair > VectorOfPairs; + typedef std::vector< Pair* > VectorOfPairs; typedef std::map StringToVectorOfPairs; StringToVectorOfPairs WatchMap; -- cgit v0.12