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 that were defined in this scope, leaving
* objects from the other scopes untouched. * objects from the other scopes untouched.
*/ */
for(map<perm_string, Signal*>::iterator it = new_signals_.begin() delete_all(new_signals_);
; it != new_signals_.end(); ++it) delete_all(new_components_);
delete it->second; delete_all(new_types_);
for(map<perm_string, ComponentBase*>::iterator it = new_components_.begin() delete_all(new_constants_);
; 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;
} }
const VType*ScopeBase::find_type(perm_string by_name) 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 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/ */
# include <algorithm>
# include <list> # include <list>
# include <map> # include <map>
# include "StringHeap.h" # include "StringHeap.h"
@ -30,6 +31,16 @@ class Architecture;
class ComponentBase; class ComponentBase;
class VType; 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 { class ScopeBase {
public: public:
@ -42,6 +53,16 @@ class ScopeBase {
protected: protected:
void cleanup(); 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... // Signal declarations...
std::map<perm_string,Signal*> old_signals_; //previous scopes std::map<perm_string,Signal*> old_signals_; //previous scopes
std::map<perm_string,Signal*> new_signals_; //current scope std::map<perm_string,Signal*> new_signals_; //current scope