// file: $isip_ifc/class/algo/Constant/Constant.h
// version: $Id: Constant.h 10532 2006-03-29 21:26:30Z raghavan $
//

// make sure definitions are only made once
//
#ifndef ISIP_CONSTANT
#define ISIP_CONSTANT

// isip include files
//
#ifndef ISIP_ALGORITHM_BASE
#include <AlgorithmBase.h>
#endif

#ifndef ISIP_HASH_TABLE
#include <HashTable.h>
#endif

// Constant: a class is used to get constants from a file, it works
// similarly with Generator class(no input data needed). Users can get
// constants from this class, which get the constants from a file. These
// constants can be used to continue calculation. Constant class supports
// all data type defined in AlgorithmData class.
//
class Constant : public AlgorithmBase {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
public:
  
  // define the class name
  //
  static const String CLASS_NAME;

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define the algorithm choices
  //
  enum ALGORITHM { DATA = 0, DEF_ALGORITHM = DATA};

  // define the implementation choices
  //
  enum IMPLEMENTATION { READ = 0, WRITE, DEF_IMPLEMENTATION = READ};

  // define output filename mode choices
  //
  enum OUT_FILENAME_MODE { NORMAL = 0, TEMPORARY, DEF_OUT_FILENAME_MODE = NORMAL};
  
  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  static const NameMap OUT_FILENAME_MODE_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String DEF_FILENAME;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_OUT_FILENAME_MODE;
  static const String PARAM_DATATYPE;  
  static const String PARAM_FILENAME;
  static const String PARAM_CHANNEL;
  static const String PARAM_CHANNEL_INDEX;    
  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define a default value for the channel
  //
  static const boolean DEF_DATA_EXIST = false;

  // define default argument(s)
  //
  static const AlgorithmData::COEF_TYPE DEF_COEF_TYPE =
  AlgorithmData::DEF_CTYPE;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 70500;

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // output filename mode
  //
  OUT_FILENAME_MODE out_filename_mode_d;
  
  // specify a filename to read/write a constant value
  //
  String filename_d;

  // specify the total number of channels
  //
  Long channels_d;

  // specify a channel index
  //
  Vector<Long> channel_index_d;

  // define the type of data to be stored
  //
  AlgorithmData::DATA_TYPE data_type_d;
  
  // define a variable to hold the data
  //
  Vector<AlgorithmData> data_d;

  // define a flag that indicates whether the data has already been read
  //
  boolean data_exist_d;
  
  // static memory manager
  //
  static MemoryManager mgr_d;

  // define the static HashTable object
  //
  static  HashTable<String, String> hash_files_d;
  
  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
  
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //  setDebug is inherited from the base class
  //
  boolean debug(const unichar* msg) const;

  // method: destructor
  //
  ~Constant() {}

  // method: default constructor
  //
  Constant(ALGORITHM algorithm = DEF_ALGORITHM,
	   IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    data_type_d = AlgorithmData::DEF_DTYPE;
    out_filename_mode_d = DEF_OUT_FILENAME_MODE;
    filename_d.assign(DEF_FILENAME);
    data_exist_d = DEF_DATA_EXIST;
    channels_d = DEF_NUM_CHANNELS;
    is_valid_d = false;
  }

  // method: copy constructor
  //
  Constant(const Constant& arg) {
    assign(arg);
  }

  // assign methods
  //
  boolean assign(const Constant& arg);

  // method: operator=
  //
  Constant& operator= (const Constant& arg) {
    assign(arg);
    return *this;
  }
  
  // i/o methods
  //
  long sofSize() const;
  
  boolean read(Sof& sof, long tag, const String& name = CLASS_NAME);
  boolean write(Sof& sof, long tag, const String& name = CLASS_NAME) const;

  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT,
		   boolean param = true,
                   boolean nested = false);
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;

  // equality methods
  //
  boolean eq(const Constant& arg) const;
  
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    is_valid_d = false;
    return true;
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    is_valid_d = false;
    return true;  
  }

  // method: setOutFilenameMode
  //
  boolean setOutFilenameMode(OUT_FILENAME_MODE out_filename_mode) {
    out_filename_mode_d = out_filename_mode;
    is_valid_d = false;
    return true;  
  }
  
  // method: setChannel
  //
  boolean setChannel(long channels) {
    channels_d = channels;
    is_valid_d = false;
    return true; 
  }

  // method: setDataType
  //
  boolean setDataType(AlgorithmData::DATA_TYPE type) {
    data_type_d = type;
    is_valid_d = false;
    return true; 
  }
  
  // method: setFilename
  //
  boolean setFilename(String filename) {
    filename_d = filename;
    is_valid_d = false;
    return true; 
  }
 
  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    is_valid_d = false;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }  

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }

  // method: getImplementation
  //
  OUT_FILENAME_MODE getOutFilenameMode() const {
    return out_filename_mode_d;
  }
  
  // method: getChannel
  //
  long getChannel() const {
    return channels_d;
  }

  // method: getFilename
  //
  boolean getFilename(String& filename) const {
    filename = filename_d;
    return true; 
  }

  // method: getInputFilename
  //
  boolean getInputFilename(String& filename) const {
    if (implementation_d == READ) {
      filename = filename_d;
    }
    else
      filename.assign(String::NULL_STRING);
    return true; 
  }

  // method: getOutputFilename
  //
  boolean getOutputFilename(String& filename) const {
    
    if (implementation_d == WRITE) {
      filename = filename_d;
    }
    else
      filename.assign(String::NULL_STRING);
    return true; 
  }
  
  
  // method: getDataType
  //
  AlgorithmData::DATA_TYPE getDataType() const {
    return data_type_d; 
  }
  
  // method: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation) const{
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
  
  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // assign method
  //
  boolean assign(const AlgorithmBase& arg);

  // equality method
  //
  boolean eq(const AlgorithmBase& arg) const;

  // method: className
  //
  const String& className() const {
    return CLASS_NAME;
  }

  // initialization method
  //
  boolean init();

  // apply method
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);
  
  // method to set the parser
  //
  boolean setParser(SofParser* parser);

  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // i/o methods
  //
  boolean readDataConstant(Sof& sof, const String& pname,
			   long size, boolean param, boolean nested);
  boolean writeDataConstant(Sof& sof, const String& pname) const;
  
  
  // implementation specific compute methods
  //
  boolean computeRead(Vector<AlgorithmData>& output,
		      const Vector< CircularBuffer<AlgorithmData> >& input);
  
  boolean computeWrite(Vector<AlgorithmData>& output,
		       const Vector< CircularBuffer<AlgorithmData> >& input);
  
  boolean getFromHash();
  
};

// end of include file
// 
#endif
