//===========================================================================//
// File:	cmpnnt.cc                                                           //
// Project: MUNGA           Brick: Entity												  //
// Contents: 																					  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 12/14/94  ECH  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1994-1995, Virtual World Entertainment, Inc.                //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Channel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//
// Shared Data Support
//
Channel::ClassData*
	Channel::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			ChannelClassID,
			"Adept::Channel",
			BaseClass::DefaultData,
			0,
			NULL
		);
	Register_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Channel::~Channel()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Channel::Channel(
	ClassData *class_data,
	MemoryStream *stream,
	ComponentWeb *owning_web
):
	Component(class_data, stream, owning_web),
	dependantComponents(NULL)
{
	*stream >> channelCommand;
	ReadOutputsFromStream(stream, owning_web);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::NotifyDependantsOfChange()
{
	Check_Object(this);
	ChainIteratorOf<Component *> components(&dependantComponents);
	Component *component;
	while ((component = components.ReadAndNext()) != NULL)
	{
		Check_Object(component);
		component->ChannelChanged(this);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::TestInstance()
{
	Verify(IsDerivedFrom(DefaultData));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::ReadOutputsFromStream(
		MemoryStream *stream,
		ComponentWeb *owning_web
	)
{
	Check_Object(this);
	Check_Object(stream);
	Check_Object(owning_web);

	int i, number_of_entries;
	*stream >> number_of_entries;
	ComponentID component_id;
	component_id.scriptResourceID = owning_web->GetScriptResourceID();
	for (i = 0; i < number_of_entries; i++)
	{
		*stream >> component_id.componentNumber;
		Component *component = owning_web->FindComponent(component_id);
		Check_Object(component);
		AddDependant(component);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Channel::SkipStreamData(MemoryStream *stream)
{
	Check_Object(this);
	Check_Object(stream);

	BaseClass::SkipStreamData(stream);
	int command;
	*stream >> command;
	Verify(command == channelCommand);

	//
	//----------------------------
	// Skip the output attachments
	//----------------------------
	//
	int i, number_of_entries;
	*stream >> number_of_entries;
	ComponentID component_id;
	for (i = 0; i < number_of_entries; i++)
	{
		*stream >> component_id.componentNumber;
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	ChannelOf<int>::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			ChannelOfIntClassID,
			"Adept::ChannelOfInt",
			BaseClass::DefaultData,
			0,
			NULL
		);
	Register_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ChannelOf<int>::ChannelOf(
	ClassData *class_data,
	MemoryStream *stream,
	ComponentWeb *owning_web
):
	Channel(class_data, stream, owning_web)
{
	channelOutput = INT_MIN;
}
