#include "cdutils_c.h"
#include "cdutils_core.h"

using namespace CDUtils;

CInterface C;


CInterface::CInterface(): Interface(CDType::cdC) {
}


void CInterface::sendCommand(const CDType & c) {
	core->sendCommand(c);
}


void CInterface::connect(const CDType & c, PIObject * o, Handler eh) {
	core->registerCHandler(c, o, eh);
}


void CInterface::autoConnect(PIObject * o, const PIString & prefix) {
	if (!o) return;
	uint cid = o->classNameID();
	if (!PIObject::__meta_data().contains(cid)) return;
	PIMap<PIString, Handler> eh_map;
	PIObject::__MetaData & md(PIObject::__meta_data()[cid]);
	PIMap<const void * , __MetaFunc>::const_iterator it;
	for (it = md.eh_func.constBegin(); it != md.eh_func.constEnd(); ++it) {
		eh_map[it.value().func_name] = (Handler)it.value().addr;
		//piCout << "func" << it.value().func_name;
	}
	PIVector<CDType * > cl = C.root().children();
	piForeachC (CDType * c, cl) {
		PIString cp = prefix + c->pathString().join("_");
		if (cp.isEmpty()) continue;
		if (!eh_map.contains(cp)) continue;
		connect(*c, o, eh_map[cp]);
	}
}
