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

#include <iostream> 
#include <string.h>
#include <vector>
#include <list>
using namespace std;
#include "SystemModel.h"


// CSystemModel

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

	model_name = new char[32];					// alloc mem for model_name
	strcpy(model_name, "actual_model_name");	// temp model name
	t_start = 0.0;								// sim start time
	t_stop = 10.0;								// sim stop time

	// -- Block list
	CBlock *p_block1 = new CBlock;			// new returns & of mem. loc. to hold a CBlock obj.
	list<CBlock*>::iterator it_blk;			// iterator for list of CBlock-ptrs

	m_lstBlock.push_front(p_block1);		// insert a new element at the beginning of the list (a ptr-to-CBlock)
	
	for(it_blk = m_lstBlock.begin(); it_blk != m_lstBlock.end(); it_blk++)
	{
		cout << " CSystemModel::CSystemModel(), m_lstBlock item = " << *it_blk << endl;	// deref the iterator to get the ptr-to-CBlock
		cout << " CSystemModel::CSystemModel(), m_lstBlock item name = " << (*it_blk)->GetName() << endl;
	}

	// -- Connection list
	CConnection *p_conn = new CConnection;	// new returns & of mem. loc. to hold a CConnection obj.
	list<CConnection*>::iterator it_conn;	// iterator for list of CConnection-ptrs

	m_lstConnection.push_front(p_conn);		// insert a new element at the beginning of the list (a ptr-to-CConnection)

	for(it_conn = m_lstConnection.begin(); it_conn != m_lstConnection.end(); it_conn++)
	{
		cout << " CSystemModel::CSystemModel(), m_lstConnection item = " << *it_conn << endl; // deref the iterator to get the ptr-to-CConnection
		cout << " CSystemModel::CSystemModel(), signal_name = " << (*it_conn)->GetSignal()->GetName() << endl;
	}
}

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

	// Delete mem
	list<CBlock*>::iterator it_blk;
	list<CConnection*>::iterator it_conn;

	for(it_blk = m_lstBlock.begin(); it_blk != m_lstBlock.end(); it_blk++)
	{
		delete (*it_blk);	// delete what it_blk is pointing to: i.e. deref the it_blk ptr and delete the ptr-to-CBlock.
	}
	// list destr. automatically destroys the list

	for(it_conn = m_lstConnection.begin(); it_conn != m_lstConnection.end(); it_conn++)
	{
		delete (*it_conn);	// delete what it_conn is pointing to: i.e. deref the it_conn ptr and delete the ptr-to-CConnection.
	}
	// list destr. automatically destroys the list
}

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

	return model_name;
}