variable_watch: Add a deleter for the client data

The client data is arbitrary and the callback may be called an
unspecified number of times, so the cmVariableWatch must be the one to
delete the client data in the end (if it is needed at all).
This commit is contained in:
Ben Boeckel 2013-08-02 15:44:15 -04:00 committed by Brad King
parent fc7c3b4dc8
commit 0d6acb1df8
2 changed files with 14 additions and 3 deletions

View File

@ -53,11 +53,13 @@ cmVariableWatch::~cmVariableWatch()
} }
void cmVariableWatch::AddWatch(const std::string& variable, void cmVariableWatch::AddWatch(const std::string& variable,
WatchMethod method, void* client_data /*=0*/) WatchMethod method, void* client_data /*=0*/,
DeleteData delete_data /*=0*/)
{ {
cmVariableWatch::Pair* p = new cmVariableWatch::Pair; cmVariableWatch::Pair* p = new cmVariableWatch::Pair;
p->Method = method; p->Method = method;
p->ClientData = client_data; p->ClientData = client_data;
p->DeleteDataCall = delete_data;
cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable]; cmVariableWatch::VectorOfPairs* vp = &this->WatchMap[variable];
cmVariableWatch::VectorOfPairs::size_type cc; cmVariableWatch::VectorOfPairs::size_type cc;
for ( cc = 0; cc < vp->size(); cc ++ ) for ( cc = 0; cc < vp->size(); cc ++ )

View File

@ -26,6 +26,7 @@ class cmVariableWatch
public: public:
typedef void (*WatchMethod)(const std::string& variable, int access_type, typedef void (*WatchMethod)(const std::string& variable, int access_type,
void* client_data, const char* newValue, const cmMakefile* mf); void* client_data, const char* newValue, const cmMakefile* mf);
typedef void (*DeleteData)(void* client_data);
cmVariableWatch(); cmVariableWatch();
~cmVariableWatch(); ~cmVariableWatch();
@ -34,7 +35,7 @@ public:
* Add watch to the variable * Add watch to the variable
*/ */
void AddWatch(const std::string& variable, WatchMethod method, void AddWatch(const std::string& variable, WatchMethod method,
void* client_data=0); void* client_data=0, DeleteData delete_data=0);
void RemoveWatch(const std::string& variable, WatchMethod method); void RemoveWatch(const std::string& variable, WatchMethod method);
/** /**
@ -67,7 +68,15 @@ protected:
{ {
WatchMethod Method; WatchMethod Method;
void* ClientData; void* ClientData;
Pair() : Method(0), ClientData(0) {} DeleteData DeleteDataCall;
Pair() : Method(0), ClientData(0), DeleteDataCall(0) {}
~Pair()
{
if (this->DeleteDataCall && this->ClientData)
{
this->DeleteDataCall(this->ClientData);
}
}
}; };
typedef std::vector< Pair* > VectorOfPairs; typedef std::vector< Pair* > VectorOfPairs;