//------------------------------------------------------------
// $Id: objectDatas.cc,v 1.1.1.1 1999/11/25 06:26:09 yamauchi Exp $ 
//------------------------------------------------------------
// 	object data container implementation
//	(M\pi)^2 data format viewer
//	(M\pi)^2 data file is derived mgf data format
//
//	mpi2 data を OpenGL を用いて表示する
//	Plex というクラスが info にあるのだが、ヘッダがみつからないた
//	めに簡易的なものを作成した。 
//	1996/6/30
//	Copyright (C) 1996--1998 YAMAUCHI Hitoshi 山内 斉
//------------------------------------------------------------
#include <assert.h>
#include <iostream.h>

// #include <stdlib.h>

#include "ProcObj.h"
#include "mpi2exit.h"
#include "objectArray.h"

// #include "materialArray.h"


//------------------------------------------------------------
// object array
//------------------------------------------------------------
objectsArrayClass::
~objectsArrayClass(void)
{
    this->init();
}

void objectsArrayClass::
init(void)
{
    if(this->mLen > 0)
    {
	delete [] this->mObjArray; // object そのものは消去しない
    }
    this->mLen = 0;
    this->mObjArray = NULL;
}

void objectsArrayClass::
setLength(int aLen)
{
    if(this->mLen > 0)
    {
	cerr << "Error!: already set object length " << 
	    "at objectsArrayClass::setLength, len = " << this->mLen << endl;
	commMpi2_exit(1);
    }
    if(aLen < 0)
    {
	cerr << "Error!: illegal length at objectsArrayClass::setLength, "
	    << "No Object? input length = " << aLen << endl;
	commMpi2_exit(1);
    }
    this->mLen = aLen;

    if(this->mLen > 0)
    {
	this->mObjArray = new objBaseClass*[this->mLen];
	int i;
	for(i = 0; i < this->mLen; i++)
	{
	    this->mObjArray[i] = NULL; // for add
	}
    }else 
    {
	assert(this->mLen == 0); // 長さがない場合もある
	this->mObjArray = NULL;
    }
};

void objectsArrayClass::
set(objBaseClass* pObj, int idx)
{
    assert((idx >= 0) && (idx < this->mLen));
    this->mObjArray[idx] = pObj;
}

objBaseClass *objectsArrayClass::
get(int anIdx)
{
    assert((anIdx >= 0) && (anIdx < this->mLen));
    return(this->mObjArray[anIdx]);
}

void objectsArrayClass::
add(objBaseClass* pObj)
{
    int i;
    for(i = 0; i < this->mLen; i++)
    {
	if(this->mObjArray[i] == NULL)
	{
	    this->mObjArray[i] = pObj;
	    return;
	}
    }
    cerr << "Error! : can not add object at objectsArrayClass::add." 
	"length = " << this->mLen << endl;
    cerr << "PE [" << GpPE->getMyRank() << "]" << endl; // debug 
    commMpi2_exit(1);
}

//------------------------------------------------------------
// material array
//------------------------------------------------------------
materialArrayClass::
~materialArrayClass(void)
{
    this->init();
}

void materialArrayClass::
init(void)
{
    if(this->mLen > 0)
    {
	delete [] this->mppMatArray; // material そのものは消去しない
    }
    this->mLen = 0;
    this->mppMatArray = NULL;
}

void materialArrayClass::
setLength(int aLen)
{
    if(this->mLen > 0)
    {
	cerr << "Error!: already set material length " << 
	    "at materialArrayClass::setLength" << endl;
	commMpi2_exit(1);
    }
    if(aLen <= 0)
    {
	cerr << "Error!: illegal length at materialArrayClass::setLength, "
	    << "No material?" << aLen << endl;
	commMpi2_exit(1);
    }
    this->mLen = aLen;
    this->mppMatArray = new materialClass*[this->mLen];
};

void materialArrayClass::
set(materialClass* pMat, int idx)
{
    assert((idx >= 0) && (idx < this->mLen));
    this->mppMatArray[idx] = pMat;
}

materialClass *materialArrayClass::
get(int anIdx)
{
    assert((anIdx >= 0) && (anIdx < this->mLen));
    return(this->mppMatArray[anIdx]);
}

