
//	Title:		Block.cpp
//	Purpose:	Contains all Block related code.

#include <iostream>
#include <string.h>
#include <vector>
#include <list>
using namespace std;
#include "Block.h" 
#include "SystemModel.h"	// rqd. since Block() destr potentially deletes a ptr-to-CSystemModel (m_pSubSystemModel)


// CBlockShape

CBlockShape::CBlockShape(void)
{
	cout << " CBlockShape::CBlockShape()\n";
	
	// Init
	int		i;
	double	d = 1.0;
	m_eBlockShape = e_rectangle;

	for(i=0; i<3; i++)
	{
		geometry[i] = d;	// add temp elements to the array
	}
}

CBlockShape::~CBlockShape(void)
{
	cout << " CBlockShape::~CBlockShape()\n";
}

// CPort  

CPort::CPort(CBlock &block_ref):
m_rRefToBlock(block_ref)					// must init the m_rRefToBlock rather than assign on CPort obj. creation
{
	cout << " CPort::CPort()\n";
	
	// Init
	port_name = new char[32];				// allocate space for port_name
	strcpy(port_name, "actual_port_name");	// copy actual port name into the var port_name

	port_position[0] = 1.0;
	port_position[1] = 0.0;
	e_port_id = e_right_arrow; 
}

CPort::~CPort(void)
{
	cout << " CPort::~CPort()\n";
}

char *CPort::GetName(void)
{
	cout << " CPort::GetName()\n";

	return port_name;
}


// CBlock

CBlock::CBlock(void)
{
	cout << " CBlock::CBlock()\n";

	// Init
	block_name = new char[32];
	strcpy(block_name, "actual_block_name");
	block_position[0] = 1.0;
	block_position[1] = 1.0;

	// -- Vector of input ptrs-to-CPort
	CPort *p_port_in = new CPort(*this);
	vector<CPort*>::iterator it_in;					// generate an iterator for vec-of-ptrs-to-CPort

	it_in = m_vecInputPorts.begin();				// start of the vec
	m_vecInputPorts.insert(it_in, p_port_in);	// insert a new ptr-to-CPort in the vector

	for(it_in = m_vecInputPorts.begin(); it_in < m_vecInputPorts.end(); it_in++)
	{
		cout << " CBlock::CBlock(), m_vecInputPorts item = " << *it_in << endl; // deref the iterator to get the ptr-to-CPort
		cout << " CBlock::CBlock(), m_vecInputPorts item name = " << (*it_in)->GetName() << endl; // deref the iterator to get the ptr-to-CPort and call GetName()
	}

	// -- Vector of output ptrs-to-CPort
	CPort *p_port_out = new CPort(*this);
	vector<CPort*>::iterator it_out;				// generate an iterator for vec-of-ptrs-to-CPort

	it_out = m_vecOutputPorts.begin();				// start of the vec
	m_vecOutputPorts.insert(it_out, p_port_out);	// insert a new ptr-to-CPort in the vector

	for(it_out = m_vecOutputPorts.begin(); it_out < m_vecOutputPorts.end(); it_out++)
	{
		cout << " CBlock::CBlock(), m_vecOutputPorts item = " << *it_out << endl; // deref the iterator to get the ptr-to-CPort
		cout << " CBlock::CBlock(), m_vecOutputPorts item name = " << (*it_out)->GetName() << endl; // deref the iterator to get the ptr-to-CPort and call GetName()
	}

	m_pSubSystemModel = NULL;	// ptr to a possibly CONTAINED SUB MODEL: ptr = NULL => no contained system model, ptr = !NULL => system model
}

CBlock::~CBlock(void)
{
	cout << " CBlock::~CBlock()\n";

	// Delete mem

	if(block_name != NULL)
	{	
		delete [] block_name;
	}

	vector<CPort*>::iterator it_in;		// generate an iterator for vec-of-ptrs-to-CPort
	vector<CPort*>::iterator it_out;	// generate an iterator for vec-of-ptrs-to-CPort
	
	for(it_in = m_vecInputPorts.begin(); it_in < m_vecInputPorts.end(); it_in++)
	{
		delete (*it_in);	// delete what it_in is pointing to: i.e. deref the it_in ptr and delete the ptr-to-CPort.
	}
	// vector is then destroyed automatically by vector destructor

	for(it_out = m_vecOutputPorts.begin(); it_out < m_vecOutputPorts.end(); it_out++)
	{
		delete (*it_out);	// delete what it_out is pointing to: i.e. deref the it_out ptr and delete the ptr-to-CPort.
	}
	// vector is then destroyed automatically by vector destructor

	if(m_pSubSystemModel != NULL)
	{
		delete m_pSubSystemModel;
		m_pSubSystemModel = NULL;
	}
}

char *CBlock::GetName(void)
{
	cout << " CBlock::GetName()\n";

	return block_name;
}


// CConstantBlock

CConstantBlock::CConstantBlock(void)
{
	cout << " CConstantBlock::CConstantBlock()\n";
}

CConstantBlock::~CConstantBlock(void)
{
	cout << " CConstantBlock::~CConstantBlock()\n";
}


// CDerivativeBlock

CDerivativeBlock::CDerivativeBlock(void)
{
	cout << " CDerivativeBlock::CDerivativeBlock()\n";
}

CDerivativeBlock::~CDerivativeBlock(void)
{
	cout << " CDerivativeBlock::~CDerivativeBlock()\n";
}


// CDivideBlock

CDivideBlock::CDivideBlock(void)
{
	cout << " CDivideBlock::CDivideBlock()\n";	
}

CDivideBlock::~CDivideBlock(void)
{
	cout << " CDivideBlock::~CDivideBlock()\n";	
}


// CGainBlock

CGainBlock::CGainBlock(void)
{
	cout << " CGainBlock::CGainBlock()\n";
}

CGainBlock::~CGainBlock(void)
{
	cout << " CGainBlock::~CGainBlock()\n";
}


// CIntegratorBlock

CIntegratorBlock::CIntegratorBlock(void)
{
	cout << " CIntegratorBlock::CIntegratorBlock()\n";
}

CIntegratorBlock::~CIntegratorBlock(void)
{
	cout << " CIntegratorBlock::~CIntegratorBlock()\n";
}


// CLinearFnBlock

CLinearFnBlock::CLinearFnBlock(void)
{
	cout << " CLinearFnBlock::CLinearFnBlock()\n";
}

CLinearFnBlock::~CLinearFnBlock(void)
{
	cout << " CLinearFnBlock::~CLinearFnBlock()\n";
}


// COutputBlock

COutputBlock::COutputBlock(void)
{
	cout << " COutputBlock::COutputBlock()\n";
}

COutputBlock::~COutputBlock(void)
{
	cout << " COutputBlock::~COutputBlock()\n";
}


// CSignalGeneratorBlock

CSignalGeneratorBlock::CSignalGeneratorBlock(void)
{
	cout << " CSignalGeneratorBlock::CSignalGeneratorBlock()\n";
}

CSignalGeneratorBlock::~CSignalGeneratorBlock(void)
{
	cout << " CSignalGeneratorBlock::~CSignalGeneratorBlock()\n";
}


// CSubsystemBlock

CSubsystemBlock::CSubsystemBlock(void)
{
	cout << " CSubsystemBlock::CSubsystemBlock()\n";
} 

CSubsystemBlock::~CSubsystemBlock(void)
{
	cout << " CSubsystemBlock::~CSubsystemBlock()\n";
} 


// CSubsystemInBlock

CSubsystemInBlock::CSubsystemInBlock(void)
{
	cout << " CSubsystemInBlock::CSubsystemInBlock()\n";
}

CSubsystemInBlock::~CSubsystemInBlock(void)
{
	cout << " CSubsystemInBlock::~CSubsystemInBlock()\n";
}


// CSubsystemOutBlock

CSubsystemOutBlock::CSubsystemOutBlock(void)
{
	cout << " CSubsystemOutBlock::CSubsystemOutBlock()\n";
} 

CSubsystemOutBlock::~CSubsystemOutBlock(void)
{
	cout << " CSubsystemOutBlock::~CSubsystemOutBlock()\n";
} 


// CSumBlock

CSumBlock::CSumBlock(void)
{
	cout << " CSumBlock::CSumBlock()\n";
}

CSumBlock::~CSumBlock(void)
{
	cout << " CSumBlock::~CSumBlock()\n";
}


// CTransferFnBlock

CTransferFnBlock::CTransferFnBlock(void)
{
	cout << " CTransferFnBlock::CTransferFnBlock()\n";
}

CTransferFnBlock::~CTransferFnBlock(void)
{
	cout << " CTransferFnBlock::~CTransferFnBlock()\n";
}