{-# LANGUAGE ForeignFunctionInterface, TypeFamilies, MultiParamTypeClasses, 
             FlexibleInstances, TypeSynonymInstances, EmptyDataDecls, 
             OverlappingInstances, IncoherentInstances #-}

module HROOT.Class.TFormula.Implementation where


import HROOT.TypeCast

import HROOT.Class.TFormula.RawType
import HROOT.Class.TFormula.FFI
import HROOT.Class.TFormula.Interface
import HROOT.Class.TFormula.Cast
import HROOT.Class.TClass.RawType
import HROOT.Class.TClass.Cast
import HROOT.Class.TClass.Interface
import HROOT.Class.TNamed.RawType
import HROOT.Class.TNamed.Cast
import HROOT.Class.TNamed.Interface
import HROOT.Class.TObject.RawType
import HROOT.Class.TObject.Cast
import HROOT.Class.TObject.Interface
import HROOT.Class.Deletable.RawType
import HROOT.Class.Deletable.Cast
import HROOT.Class.Deletable.Interface

import Data.Word
-- import Foreign.C            
-- import Foreign.Ptr
import Foreign.ForeignPtr

import System.IO.Unsafe


instance ITFormula TFormula where
  compile = xform1 c_tformula_compile
  clear = xform1 c_tformula_clear
  definedValue = xform1 c_tformula_definedvalue
  eval = xform4 c_tformula_eval
  evalParOld = xform2 c_tformula_evalparold
  evalPar = xform2 c_tformula_evalpar
  getNdim = xform0 c_tformula_getndim
  getNpar = xform0 c_tformula_getnpar
  getNumber = xform0 c_tformula_getnumber
  getParNumber = xform1 c_tformula_getparnumber
  isLinear = xform0 c_tformula_islinear
  isNormalized = xform0 c_tformula_isnormalized
  setNumber = xform1 c_tformula_setnumber
  setParameter = xform2 c_tformula_setparameter
  setParameters = xform1 c_tformula_setparameters
  setParName = xform2 c_tformula_setparname
  setParNames = xform11 c_tformula_setparnames
  update = xform0 c_tformula_update
instance ITNamed TFormula where
  setName = xform1 c_tformula_setname
  setNameTitle = xform2 c_tformula_setnametitle
  setTitle = xform1 c_tformula_settitle
instance ITObject TFormula where
  draw = xform1 c_tformula_draw
  findObject = xform1 c_tformula_findobject
  getName = xform0 c_tformula_getname
  isA = xform0 c_tformula_isa
  isFolder = xform0 c_tformula_isfolder
  isEqual = xform1 c_tformula_isequal
  isSortable = xform0 c_tformula_issortable
  paint = xform1 c_tformula_paint
  printObj = xform1 c_tformula_printobj
  recursiveRemove = xform1 c_tformula_recursiveremove
  saveAs = xform2 c_tformula_saveas
  useCurrentStyle = xform0 c_tformula_usecurrentstyle
  write = xform3 c_tformula_write
instance IDeletable TFormula where
  delete = xform0 c_tformula_delete

instance ITFormula (Exist TFormula) where
  compile (ETFormula x) = compile x
  clear (ETFormula x) = clear x
  definedValue (ETFormula x) = definedValue x
  eval (ETFormula x) = eval x
  evalParOld (ETFormula x) = evalParOld x
  evalPar (ETFormula x) = evalPar x
  getNdim (ETFormula x) = getNdim x
  getNpar (ETFormula x) = getNpar x
  getNumber (ETFormula x) = getNumber x
  getParNumber (ETFormula x) = getParNumber x
  isLinear (ETFormula x) = isLinear x
  isNormalized (ETFormula x) = isNormalized x
  setNumber (ETFormula x) = setNumber x
  setParameter (ETFormula x) = setParameter x
  setParameters (ETFormula x) = setParameters x
  setParName (ETFormula x) = setParName x
  setParNames (ETFormula x) = setParNames x
  update (ETFormula x) = update x
instance ITNamed (Exist TFormula) where
  setName (ETFormula x) = setName x
  setNameTitle (ETFormula x) = setNameTitle x
  setTitle (ETFormula x) = setTitle x
instance ITObject (Exist TFormula) where
  draw (ETFormula x) = draw x
  findObject (ETFormula x) = findObject x
  getName (ETFormula x) = getName x
  isA (ETFormula x) = isA x
  isFolder (ETFormula x) = isFolder x
  isEqual (ETFormula x) = isEqual x
  isSortable (ETFormula x) = isSortable x
  paint (ETFormula x) = paint x
  printObj (ETFormula x) = printObj x
  recursiveRemove (ETFormula x) = recursiveRemove x
  saveAs (ETFormula x) = saveAs x
  useCurrentStyle (ETFormula x) = useCurrentStyle x
  write (ETFormula x) = write x
instance IDeletable (Exist TFormula) where
  delete (ETFormula x) = delete x


newTFormula :: String -> String -> IO TFormula
newTFormula = xform1 c_tformula_newtformula

tFormulaOptimize :: TFormula -> IO ()
tFormulaOptimize = xform0 c_tformula_tformulaoptimize

tFormulaGetParameter :: TFormula -> String -> IO Double
tFormulaGetParameter = xform1 c_tformula_tformulagetparameter



instance FPtr (Exist TFormula) where
  type Raw (Exist TFormula) = RawTFormula
  get_fptr (ETFormula obj) = castForeignPtr (get_fptr obj)
  cast_fptr_to_obj fptr = ETFormula (cast_fptr_to_obj (fptr :: ForeignPtr RawTFormula) :: TFormula)