Logo Search packages:      
Sourcecode: faust version File versions  Download package

sigtype.hh

/************************************************************************
 ************************************************************************
    FAUST compiler
      Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/
 
 
 
#ifndef _SigType_
#define _SigType_

#include <vector>
#include <string>
#include <iostream>
#include "smartpointer.hh"
#include "interval.hh"


/*********************************************************************
*
*                                   Type System for FAUST
*
*     <type>                  ::= <simpletype> ||  table(<type>)
*                                   ||  <type>|<type>  || <type>*<type>
*     <simpletype>      ::= <nature><variability><computability><vectorability>||<boolean>
*     <nature>          ::= TINT || TREAL
*     <variability>     ::= TKONST || TBLOCK || TSAMP
*     <computability>   ::= TCOMP  || TINIT  || TEXEC
*     <vectorability> ::= KVECT  || KSCAL  || KTRUESCAL
*     <boolean>       ::= KNUM   || KBOOL
*
**********************************************************************/


//--------------------------------------------------
// qualite des types simples

enum { kInt = 0, kReal = 1 };                           ///< nature : integer or floating point values
enum { kNum = 0 , kBool = 1};                           ///< boolean : when a signal stands for a boolean value ( while being of c-type int or float )
enum { kKonst = 0, kBlock = 1, kSamp = 3 };             ///< variability : how fast values change
enum { kComp = 0, kInit = 1, kExec = 3 };               ///< computability : when values are available
enum { kVect = 0, kScal = 1, kTrueScal = 3/*, kIndex = 4*/};///< vectorability: when a signal can be vectorized ( actually, only kVect and kScal matter; kTrueScal and kIndex don't denote types but are here to simplify code generation )

/*---------------------------------------------------------------------

      AbstractType : 
      The root class for SimpleType, TableType and TupletType
      
      Type :
      A smartPointer to type

----------------------------------------------------------------------*/

using namespace std;

class AudioType;

typedef P<AudioType> Type;

/**
 * The Root class for all audio data types.
 * All audio types have a "variability" (how fast the values change) and
 * a "computability" (when the values are available). Simple types have
 * also a "nature" (integer or floating point).
 */
00081 class AudioType
{
  protected:
00084       int               fNature;                ///< the kind of data represented
00085       int               fVariability;           ///< how fast values change
00086       int               fComputability;         ///< when are values available
00087     int           fVectorability;     ///< when a signal can be vectorized
00088     int           fBoolean;           ///< when a signal stands for a boolean value
    
00090     interval      fInterval;              ///< Minimal and maximal values the signal can take

      
  public :          
00094       AudioType(int n, int v, int c, int vec = kVect, int b = kNum, interval i=interval()) 
              : fNature(n), fVariability(v), fComputability(c), 
                fVectorability(vec), fBoolean(b), 
                fInterval(i) {}                                                     ///< constructs an abstract audio type    
00098       virtual ~AudioType()                                                    {}    ///< not really useful here, but make compiler happier
      
00100       int   nature()          const { return fNature;             }     ///< returns the kind of values (integre or floating point)
00101       int   variability()     const { return fVariability; }      ///< returns how fast values change (constant, by blocks, by samples)
00102       int   computability() const   { return fComputability;}     ///< returns when values are available (compilation, initialisation, execution)
00103       int   vectorability() const   { return fVectorability;}     ///< returns when a signal can be vectorized
00104       int   boolean()         const { return fBoolean; }          ///< returns when a signal stands for a boolean value
      
00106       interval getInterval()  const { return fInterval; }         ///< returns the interval (min dn max values) of a signal

      
      virtual AudioType* promoteNature(int n)         = 0;              ///< promote the nature of a type
      virtual AudioType* promoteVariability(int n)    = 0;              ///< promote the variability of a type
      virtual AudioType* promoteComputability(int n)  = 0;              ///< promote the computability of a type
    virtual AudioType* promoteVectorability(int n)    = 0;              ///< promote the vectorability of a type
      virtual AudioType* promoteBoolean(int n)        = 0;              ///< promote the booleanity of a type
      //virtual AudioType* promoteInterval(const interval& i) = 0;            ///< promote the interval of a type

      
      virtual ostream& print(ostream& dst) const            = 0;              ///< print nicely a type
      
  protected:      
      void        setInterval(const interval& r)      { fInterval = r;}

};

//printing
inline ostream& operator << (ostream& s, const AudioType& n) { return n.print(s); }


/**
 * Return the nature of a vector of types.
 */
inline int mergenature(const vector<Type>& v)
{
      int r = 0;
      for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->nature(); 
      return r;
}



/**
 * Return the variability of a vector of types.
 */
inline int mergevariability(const vector<Type>& v)
{
      int r = 0;
      for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->variability(); 
      return r;
}



/**
 * Return the computability of a vector of types.
 */
inline int mergecomputability(const vector<Type>& v)
{
      int r = 0;
      for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->computability(); 
      return r;
}



/**
 * Return the vectorability of a vector of types.
 */
inline int mergevectorability(const vector<Type>& v)
{
      int r = 0;
      for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->vectorability();
      return r;
}



/**
 * Return the booleanity of a vector of types.
 */
inline int mergeboolean(const vector<Type>& v)
{
      int r = 0;
      for (unsigned int i = 0; i < v.size(); i++) r |= v[i]->boolean();
      return r;
}



/**
 * Return the interval of a vector of types.
 */
inline interval mergeinterval(const vector<Type>& v)
{
      if (v.size()==0) {
            return interval();
      } else {
            double lo=0, hi=0;
            for (unsigned int i = 0; i < v.size(); i++) {
                  interval r = v[i]->getInterval();
                  if (!r.valid) return r;
                  if (i==0) { 
                        lo = r.lo;
                        hi = r.hi;
                  } else {
                        lo = min(lo,r.lo);
                        hi = max(hi,r.hi);
                  }
            }
            return interval(lo, hi);
      }
}




/**
 * The type of a simple numeric audio signal.
 * Beside a computability and a variability, SimpleTypes have 
 * a "nature" indicating if they represent an integer or floating 
 * point audio signals.
 */
00221 class SimpleType : public AudioType
{     
  public :

00225       SimpleType(int n, int v, int c, int vec, int b, const interval& i) : AudioType(n,v,c,vec,b,i) {
            //cerr << "new simple type " << i << " -> " << *this << endl;
      }                 ///< constructs a SimpleType from a nature a variability and a computability
                  
      virtual ostream& print(ostream& dst) const;                                   ///< print a SimpleType
      
00231       virtual AudioType* promoteNature(int n)                     { return new SimpleType(n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); }          ///< promote the nature of a type
00232       virtual AudioType* promoteVariability(int v)                { return new SimpleType(fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); }          ///< promote the variability of a type
00233       virtual AudioType* promoteComputability(int c)              { return new SimpleType(fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); }          ///< promote the computability of a type
00234       virtual AudioType* promoteVectorability(int vec)            { return new SimpleType(fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval); }  ///< promote the vectorability of a type
00235       virtual AudioType* promoteBoolean(int b)                    { return new SimpleType(fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); }          ///< promote the booleanity of a type
//    virtual AudioType* promoteInterval(const interval& i) { 
//          cerr << "promote to Interval " << i  << endl; 
//          cerr << "for type : " << *this << endl;
//          Type t = new SimpleType(fNature, fVariability, fComputability, fVectorability, fBoolean, i);                      ///< promote the interval of a type
//          cerr << "gives type " << *t << endl;
//          return t;
//    }


};

inline Type intCast (Type t)  { return new SimpleType(kInt, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
inline Type floatCast (Type t)      { return new SimpleType(kReal, t->variability(), t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
inline Type sampCast (Type t) { return new SimpleType(t->nature(), kSamp, t->computability(), t->vectorability(), t->boolean(), t->getInterval()); }
inline Type boolCast (Type t)   { return new SimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kBool, t->getInterval()); }
inline Type numCast (Type t)    { return new SimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), kNum, t->getInterval()); }
inline Type vecCast (Type t)    { return new SimpleType(t->nature(), t->variability(), t->computability(), kVect, t->boolean(), t->getInterval()); }
inline Type scalCast (Type t)   { return new SimpleType(t->nature(), t->variability(), t->computability(), kScal, t->boolean(), t->getInterval()); }
inline Type truescalCast (Type t){ return new SimpleType(t->nature(), t->variability(), t->computability(), kTrueScal, t->boolean(), t->getInterval()); }

inline Type castInterval (Type t, const interval& i)  
{ 
      return new SimpleType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean(), i); 
}

/**
 * The type of a table of audio data.
 * Beside a computability and a variability, TableTypes have 
 * a "content" indicating the type of the data stored in the table.
 */
00266 class TableType : public AudioType 
{     
  protected :             
00269       const Type fContent;                                                                ///< type of that data stored in the table
      
  public :
00272       TableType(const Type& t) : 
              AudioType(t->nature(), t->variability(), t->computability(), t->vectorability(), t->boolean()), 
              fContent(t) {}        ///< construct a TableType with a content of a type t

00276       TableType(const Type& t, int v, int c) : 
              AudioType(t->nature(), t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()), 
              fContent(t) {}        ///< construct a TableType with a content of a type t, promoting variability and computability

00280       TableType(const Type& t, int n, int v, int c) : 
              AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability(), t->boolean()), 
              fContent(t) {}        ///< construct a TableType with a content of a type t, promoting nature, variability and computability

00284       TableType(const Type& t, int n, int v, int c, int vec) :
              AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()),
              fContent(t) {}        ///< construct a TableType with a content of a type t, promoting nature, variability, computability and vectorability

00288       TableType(const Type& t, int n, int v, int c, int vec, int b) :
              AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b),
              fContent(t) {}        ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity

00292       TableType(const Type& t, int n, int v, int c, int vec, int b, const interval& i) :
              AudioType(t->nature()|n, t->variability()|v, t->computability()|c, t->vectorability()|vec, t->boolean()|b, i),
              fContent(t) {}        ///< construct a TableType with a content of a type t, promoting nature, variability, computability, vectorability and booleanity

  

00298       Type content() const                      { return fContent;      }           ///< return the type of data store in the table
      virtual ostream& print(ostream& dst) const;                                   ///< print a TableType
      
00301       virtual AudioType* promoteNature(int n)                     { return new TableType(fContent, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the nature of a type
00302       virtual AudioType* promoteVariability(int v)                { return new TableType(fContent, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); } ///< promote the variability of a type
00303       virtual AudioType* promoteComputability(int c)              { return new TableType(fContent, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); } ///< promote the computability of a type
00304       virtual AudioType* promoteVectorability(int vec)            { return new TableType(fContent, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}///< promote the vectorability of a type
00305       virtual AudioType* promoteBoolean(int b)                    { return new TableType(fContent, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval); } ///< promote the booleanity of a type
      //virtual AudioType* promoteInterval(const interval& i)     { return new TableType(fContent, fNature, fVariability, fComputability, fVectorability, fBoolean, i); }                 ///< promote the interval of a type

};


                  
/**
 * The type of a tuplet of data.
 * Beside a computability and a variability, TupletTypes have 
 * a set of components.
 */
00317 class TupletType : public AudioType
{
  protected:
      vector<Type> fComponents;
      
  public:
      TupletType(const vector<Type>& vt) : 
              AudioType(mergenature(vt),mergevariability(vt),mergecomputability(vt),mergevectorability(vt),mergeboolean(vt), mergeinterval(vt)), 
              fComponents(vt) {}

      TupletType(const vector<Type>& vt, int n, int v, int c) : 
              AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt),mergevectorability(vt),mergeboolean(vt), interval()), 
              fComponents(vt) {}

      TupletType(const vector<Type>& vt, int n, int v, int c, int vec) :
              AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt), vec|mergevectorability(vt), mergeboolean(vt), interval()),
              fComponents(vt) {}

      TupletType(const vector<Type>& vt, int n, int v, int c, int vec, int b, const interval& i) :
              AudioType(n|mergenature(vt), v|mergevariability(vt), c|mergecomputability(vt), vec|mergevectorability(vt), b|mergeboolean(vt), i),
              fComponents(vt) {}

      int arity() const                               { return fComponents.size(); }
      Type operator[](unsigned int i) const     { return fComponents[i]; }
      virtual ostream& print(ostream& dst) const;
      
00343       virtual AudioType* promoteNature(int n)                     { return new TupletType(fComponents, n|fNature, fVariability, fComputability, fVectorability, fBoolean, fInterval); }   ///< promote the nature of a type
00344       virtual AudioType* promoteVariability(int v)                { return new TupletType(fComponents, fNature, v|fVariability, fComputability, fVectorability, fBoolean, fInterval); }   ///< promote the variability of a type
00345       virtual AudioType* promoteComputability(int c)              { return new TupletType(fComponents, fNature, fVariability, c|fComputability, fVectorability, fBoolean, fInterval); }   ///< promote the computability of a type
00346       virtual AudioType* promoteVectorability(int vec)            { return new TupletType(fComponents, fNature, fVariability, fComputability, vec|fVectorability, fBoolean, fInterval);}  ///< promote the vectorability of a type
00347       virtual AudioType* promoteBoolean(int b)                    { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, b|fBoolean, fInterval);  }  ///< promote the booleanity of a type
      //virtual AudioType* promoteInterval(const interval& i)     { return new TupletType(fComponents, fNature, fVariability, fComputability, fVectorability, fBoolean, i);  }                  ///< promote the interval of a type
  

};




//-------------------------------------------------
//-------------------------------------------------
//                      operations sur les types
//-------------------------------------------------
//-------------------------------------------------



//--------------------------------------------------
// liste de types predefinis

extern Type TINT;
extern Type TREAL;

extern Type TKONST;
extern Type TBLOCK;
extern Type TSAMP;

extern Type TCOMP;
extern Type TINIT;
extern Type TEXEC;

extern Type TINPUT;
extern Type TGUI;
extern Type INT_TGUI;
extern Type TREC;


//--------------------------------------------------
// creation de types

Type table  (const Type& t);
Type operator|    (const Type& t1, const Type& t2);
Type operator*    (const Type& t1, const Type& t2);


//--------------------------------------------------
// comparaison de types

bool operator==(const Type& t1, const Type& t2);
bool operator<=(const Type& t1, const Type& t2);

inline bool operator!=(const Type& t1, const Type& t2) { return !(t1==t2); }
inline bool operator< (const Type& t1, const Type& t2) { return t1<=t2 && t1!=t2; }
inline bool operator> (const Type& t1, const Type& t2) { return t2<t1;  }
inline bool operator>=(const Type& t1, const Type& t2) { return t2<=t1; }


//--------------------------------------------------
// predicats-conversion de types

SimpleType*       isSimpleType (AudioType* t);
TableType*        isTableType  (AudioType* t);
TupletType*       isTupletType (AudioType* t);


//--------------------------------------------------
// impressions de types

ostream& operator<<(ostream& dst, const SimpleType& t); 
ostream& operator<<(ostream& dst, const Type& t);
ostream& operator<<(ostream& dst, const TableType& t);
ostream& operator<<(ostream& dst, const TupletType& t);


//--------------------------------------------------
// verification de type

Type checkInt(Type t);                    ///< verifie que t est entier
Type checkKonst(Type t);                  ///< verifie que t est constant
Type checkInit(Type t);                   ///< verifie que t est connu a l'initialisation

Type checkIntParam(Type t);               ///< verifie que t est connu a l'initialisation, constant et entier

Type checkWRTbl(Type tbl, Type wr); ///< verifie que wr est compatible avec le contenu de tbl

int checkDelayInterval(Type t);           ///< Check if the interval of t is appropriate for a delay 


//--------------------------------------------------
// conversion de type

string cType (Type t);


#endif

Generated by  Doxygen 1.6.0   Back to index