// file: $isip/class/algo/AlgorithmData/algd_04.cc
// version: $Id: algd_04.cc,v 1.9 2002/05/31 21:57:25 picone Exp $
//

// isip include files
//
#include "AlgorithmData.h"

// method: sofSize
//
// arguments: none
//
// return: size of object
//
// this method returns the size of the object in the Sof file and is
// used for binary write
//
long AlgorithmData::sofSize() const {
  
  // start with the space required for the data type
  //
  long bytes = DTYPE_MAP.elementSofSize();

  // add the space required for the coef type
  //
  bytes += CTYPE_MAP.elementSofSize();

  // add the space required for the data
  //
  switch (data_type_d) {
  case VECTOR_FLOAT:
    bytes += (*(reinterpret_cast < VectorFloat* > (data_d))).sofSize();
    break;
  case MATRIX_FLOAT:
    bytes += (*(reinterpret_cast < MatrixFloat* > (data_d))).sofSize();
    break;
  case VECTOR_DOUBLE:
    bytes += (*(reinterpret_cast < VectorDouble* > (data_d))).sofSize();
    break;
  case MATRIX_DOUBLE:
    bytes += (*(reinterpret_cast < MatrixDouble* > (data_d))).sofSize();
    break;
  case VECTOR_COMPLEX_FLOAT:
    bytes += (*(reinterpret_cast < VectorComplexFloat* > (data_d))).sofSize();
    break;
  case MATRIX_COMPLEX_FLOAT:
    bytes += (*(reinterpret_cast < MatrixComplexFloat* > (data_d))).sofSize();
    break;    
  case VECTOR_COMPLEX_DOUBLE:
    bytes += (*(reinterpret_cast < VectorComplexDouble* > (data_d))).sofSize();
    break;
  case MATRIX_COMPLEX_DOUBLE:
    bytes += (*(reinterpret_cast < MatrixComplexDouble* > (data_d))).sofSize();
    break;
    
    //  case COMBINATION:
    //
 
  default:
    return Error::handle(name(), L"sofSize", Error::NOT_IMPLEM,
			 __FILE__, __LINE__);
  }
  
  return bytes;
}

// method: write
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object write itself to an Sof file
//
boolean AlgorithmData::write(Sof& sof_a, long tag_a,
			  const String& name_a) const {

  // declare local variable
  //
  long obj_size;

  if (sof_a.isText()) {

    // set the size to be dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  else {
    
    // set the size to be the size of the object written to the Sof file
    //
    obj_size = sofSize();
  }
  
  // write the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }

  // write data and exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method writes the types and the actual data
//
boolean AlgorithmData::writeData(Sof& sof_a, const String& pname_a) const {

  // declare local variables
  //
  boolean status = false;
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the data type
  //
  DTYPE_MAP.writeElementData(sof_a, PARAM_DATA_TYPE, (long)data_type_d);

  // write the coef type
  //
  CTYPE_MAP.writeElementData(sof_a, PARAM_COEF_TYPE, (long)coef_type_d);
  
  // write the actual data
  //
  switch (data_type_d) {
  case VECTOR_FLOAT:
    status = (*(reinterpret_cast < VectorFloat* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case MATRIX_FLOAT:
    status = (*(reinterpret_cast < MatrixFloat* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case VECTOR_DOUBLE:
    status = (*(reinterpret_cast < VectorDouble* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case MATRIX_DOUBLE:
    status = (*(reinterpret_cast < MatrixDouble* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case VECTOR_COMPLEX_FLOAT:
    status = (*(reinterpret_cast < VectorComplexFloat* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case MATRIX_COMPLEX_FLOAT:
    status = (*(reinterpret_cast < MatrixComplexFloat* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;    
  case VECTOR_COMPLEX_DOUBLE:
    status = (*(reinterpret_cast < VectorComplexDouble* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
  case MATRIX_COMPLEX_DOUBLE:
    status = (*(reinterpret_cast < MatrixComplexDouble* > (data_d))).writeData(sof_a, PARAM_DATA);
    break;
    
    //  case COMBINATION:
    //
 
  default:
    return Error::handle(name(), L"writeData", Error::NOT_IMPLEM,
			 __FILE__, __LINE__);
  }  

  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return status;
}

