#include <iostream>
#include "config.h"
#include "hub.h"
#include "attr.h"
#include "arch.h"
#include "entity.h"
#include "behavioral.h"
using namespace std; 
using namespace hub;
// ===========================================================================
//	Library
// ===========================================================================
Entity* Library :: FindOrCreateEntity (const std::string& entn) { 
    Entity* ent = entities_.FindOrCreate<Entity> (entn);
    if (ent) ent->Parent (this); 
    return ent;
}
Entity* Library :: CreateEntity (const std::string& entn) { 
	Entity* ent = entities_.Create (entn); 
    if (ent) ent->Parent (this); 
    return ent;
}
Entity* Library :: CreateUniqueEntity (const std::string& entn) {
	Entity* ent = entities_.CreateUnique (entn); 
    if (ent) ent->Parent (this); 
    return ent;
}
Architecture* Library :: FindArch (BehavAgent& agent, const std::string kind){
    BehavArch* behavArch;
	BehavDescr* behavDescr;
	for (EntIter ent = EntBeg(); ent != EntEnd(); ent++) {
		for (Entity::ArchIter arch = ent->second->ArchBeg(); arch != ent->second->ArchEnd(); arch++) {
			behavArch = dynamic_cast <BehavArch*> (arch->second->Impl());
			if (!behavArch) continue;
			behavDescr = behavArch->description<BehavDescr>();
			if (!behavDescr) continue;
			if (!kind.empty() && behavDescr->getDescrName() != kind) continue;
			if (agent.Eq (behavDescr)) return arch->second;
		}
	}
	return NULL;
}
//---------------------------------------------------------------------------
//	Hub
//---------------------------------------------------------------------------
Design*  Hub::CreateDesign (const std::string& desn, Entity* entity, Library* lib) {
    pair <DesignSpace::iterator, bool> search;
    search = designs_.insert (DesignSpace::value_type (desn, NULL));
    if (!search.second) return NULL;
    (*search.first).second = new Design (desn, entity);
    return (*search.first).second; 
}
//---------------------------------------------------------------------------
//	Architecture
//---------------------------------------------------------------------------
#ifndef __BCPLUSPLUS__
template <> int AttrUnique <Architecture> :: base=0;
#endif
//---------------------------------------------------------------------------
//	Architecture implementation
//---------------------------------------------------------------------------
