// file: $isip/class/algo/Connection/Connection.h
// version: $Id: Connection.h 8269 2002-07-02 19:14:33Z gao $
//

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

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

#ifndef ISIP_LONG
#include <Long.h>
#endif

#ifndef ISIP_MEMORY_MANAGER
#include <MemoryManager.h>
#endif

// Connection: a class that allows users to combine and manipulate
// multiple feature streams.
//
class Connection : public AlgorithmBase {

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

  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------

  // define algorithm types
  //
  enum ALGORITHM { CHANNEL = 0, STREAM, DEF_ALGORITHM = CHANNEL };

  // define implementation types
  //
  enum IMPLEMENTATION { CONCATENATE = 0, INTERLEAVE, SELECT,
			DEF_IMPLEMENTATION = CONCATENATE };
  
  // define a static NameMap object to get the name of algorithm
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;

  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_CHANNEL;

  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define the default value(s) of the class data
  //
  
  // default arguments to methods
  //
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 72100;
  static const long ERR_LENGTH = 72101;
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // channel specification
  //
  Long channel_d;
  
  // memory manager
  //
  static MemoryManager mgr_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
  //
  ~Connection() {}
  
  // method: default constructor
  //
  Connection(ALGORITHM algorithm = DEF_ALGORITHM,
	     IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
  }

  // method: copy constructor
  //
  Connection(const Connection& arg) {
    algorithm_d = DEF_ALGORITHM;
    implementation_d = DEF_IMPLEMENTATION;
    assign(arg);
  }
  
  // method: assign
  //
  boolean assign(const Connection& arg) {
    algorithm_d = arg.algorithm_d;
    implementation_d = arg.implementation_d;
    return AlgorithmBase::assign(arg);
  }

  // method: operator=
  //
  Connection& operator= (const Connection& 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;
  
  // method: eq
  //
  boolean eq(const Connection& arg) const {
    return ((algorithm_d == arg.algorithm_d) &&
	    (implementation_d == arg.implementation_d));
  }
  
  // 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);
  }

  // method: clear
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE) {
    if (ctype != Integral::RETAIN) {
      algorithm_d = DEF_ALGORITHM;
      implementation_d = DEF_IMPLEMENTATION;
    }
    return AlgorithmBase::clear(ctype);
  }

  //---------------------------------------------------------------------------
  //
  // 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: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
    is_valid_d = false;
    return true;
  }
  
  // method: setChannel
  //
  boolean setChannel(long channel) {
    channel_d = channel;
    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: get
  //
  boolean get(ALGORITHM& algorithm,
	      IMPLEMENTATION& implementation) const {
    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }
    
  // method: getChannel
  //
  long getChannel() const {
    return channel_d;
  }

  //---------------------------------------------------------------------------
  //
  // public methods required by the AlgorithmBase interface contract
  //
  //---------------------------------------------------------------------------

  // method: assign
  //
  boolean assign(const AlgorithmBase& arg) {
    if (typeid(arg) == typeid(Connection)) {
      return assign((Connection&)arg);
    }
    else {
      return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__);
    }
  }
  
  // method: eq
  //
  boolean eq(const AlgorithmBase& arg) const {
    if (typeid(arg) == typeid(Connection)) {
      return eq((Connection&)arg);
    }
    else {
      return Error::handle(name(), L"eq", Error::ARG, __FILE__, __LINE__);
    }
  }

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

  // computational methods
  //
  boolean apply(Vector<AlgorithmData>& output,
		const Vector< CircularBuffer<AlgorithmData> >& input);
  
  // method to get the parser from the parent object
  //
  boolean setParser(SofParser* parser);

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

  // algorithm-specific computation methods: operate on channels
  //
  boolean computeChannelConcatenate(VectorFloat& output,
				    const Vector<AlgorithmData>& input);

  boolean computeChannelInterleave(VectorFloat& output,
				   const Vector<AlgorithmData>& input);

  boolean computeStreamConcatenate(AlgorithmData& output,
				   const AlgorithmData& input);
  
  boolean computeStreamSelect(AlgorithmData& output,
			      const Vector<AlgorithmData>& input);  
};

// end of include file
// 
#endif
