ENH: Create automatic policy push/pop helper

This creates cmMakefile::PolicyPushPop to push and pop policy scope
automatically.  It also enforces balanced push/pop pairs inside the
scope it handles.
This commit is contained in:
Brad King 2009-01-22 10:56:50 -05:00
parent 8997f4760a
commit a01eb6b27b
2 changed files with 41 additions and 0 deletions

View File

@ -3674,6 +3674,35 @@ bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
return true; return true;
} }
//----------------------------------------------------------------------------
cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m): Makefile(m)
{
this->Makefile->PushPolicy();
this->PolicyDepth = this->Makefile->PolicyStack.size();
}
//----------------------------------------------------------------------------
cmMakefile::PolicyPushPop::~PolicyPushPop()
{
// Enforce matching PUSH/POP pairs.
if(this->Makefile->PolicyStack.size() < this->PolicyDepth)
{
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"cmake_policy POP without matching PUSH");
return;
}
while(this->Makefile->PolicyStack.size() > this->PolicyDepth)
{
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"cmake_policy PUSH without matching POP");
this->Makefile->PopPolicy(false);
}
// Pop our scope.
this->Makefile->PopPolicy();
}
//----------------------------------------------------------------------------
bool cmMakefile::PushPolicy() bool cmMakefile::PushPolicy()
{ {
// Allocate a new stack entry. // Allocate a new stack entry.

View File

@ -346,6 +346,18 @@ public:
bool SetPolicyVersion(const char *version); bool SetPolicyVersion(const char *version);
//@} //@}
/** Helper class to push and pop policies automatically. */
class PolicyPushPop
{
public:
PolicyPushPop(cmMakefile* m);
~PolicyPushPop();
private:
cmMakefile* Makefile;
size_t PolicyDepth;
};
friend class PolicyPushPop;
/** /**
* Get the Policies Instance * Get the Policies Instance
*/ */