Use stl algorithms and templates in ScopeBase destructor

This patch applies a more sophisticated method for
cleaning containers in VHDL ScopeBase class.
This commit is contained in:
Pawel Szostek 2011-07-21 11:04:48 +02:00 committed by Stephen Williams
parent a5ca9ea8be
commit a8fae6bbf7
2 changed files with 25 additions and 12 deletions

View File

@ -61,18 +61,10 @@ void ScopeBase::cleanup()
* objects that were defined in this scope, leaving
* objects from the other scopes untouched.
*/
for(map<perm_string, Signal*>::iterator it = new_signals_.begin()
; it != new_signals_.end(); ++it)
delete it->second;
for(map<perm_string, ComponentBase*>::iterator it = new_components_.begin()
; it != new_components_.end(); ++it)
delete it->second;
for(map<perm_string, const VType*>::iterator it = new_types_.begin()
; it != new_types_.end(); ++it)
delete it->second;
for(map<perm_string, const_t*>::iterator it = new_constants_.begin()
; it != new_constants_.end(); ++it)
delete it->second;
delete_all(new_signals_);
delete_all(new_components_);
delete_all(new_types_);
delete_all(new_constants_);
}
const VType*ScopeBase::find_type(perm_string by_name)

View File

@ -19,6 +19,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
# include <algorithm>
# include <list>
# include <map>
# include "StringHeap.h"
@ -30,6 +31,16 @@ class Architecture;
class ComponentBase;
class VType;
template<typename T>
struct delete_object{
void operator()(T* item) { delete item; }
};
template<typename T>
struct delete_pair_second{
void operator()(pair<perm_string, T*> item){ delete item.second; }
};
class ScopeBase {
public:
@ -42,6 +53,16 @@ class ScopeBase {
protected:
void cleanup();
//containers' cleaning helper functions
template<typename T> void delete_all(list<T*>& c)
{
for_each(c.begin(), c.end(), ::delete_object<T>());
}
template<typename T> void delete_all(map<perm_string, T*>& c)
{
for_each(c.begin(), c.end(), ::delete_pair_second<T>());
}
// Signal declarations...
std::map<perm_string,Signal*> old_signals_; //previous scopes
std::map<perm_string,Signal*> new_signals_; //current scope