 import Foreign
 import Foreign.C.Types
 import System.GPU.OpenCL.Types( 
-  CLEvent, CLint, CLuint, CLulong, CLEventInfo_, CLProfilingInfo_, ErrorCode(..),
-  CLCommandQueue, CLCommandType(..), CLCommandType_, CLCommandExecutionStatus(..), 
-  CLProfilingInfo(..), clSuccess, getCommandType, getCommandExecutionStatus, 
-  getCLValue )
+  CLEvent, CLint, CLuint, CLulong, CLEventInfo_, CLProfilingInfo_, CLError(..),
+  CLCommandQueue, CLCommandType(..), CLCommandType_, 
+  CLCommandExecutionStatus(..), CLProfilingInfo(..), getCommandExecutionStatus, 
+  getCLValue, getEnumCL, wrapCheckSuccess, wrapGetInfo )
 
 #include <CL/cl.h>
 



-- | Return the command-queue associated with event.
clGetEventCommandQueue :: CLEvent -> IO (Maybe CLCommandQueue)
clGetEventCommandQueue ev = alloca $ \(dat :: Ptr CLCommandQueue) -> do
  errcode <- fmap ErrorCode $ raw_clGetEventInfo ev infoid size (castPtr dat) nullPtr
  if errcode == clSuccess
    then fmap Just $ peek dat
    else return Nothing
    where 
      infoid = getCLValue CL_EVENT_COMMAND_QUEUE
      size = fromIntegral $ sizeOf (nullPtr::CLCommandQueue)

-- | Return the command associated with event.
clGetEventCommandType :: CLEvent -> IO (Maybe CLCommandType)
clGetEventCommandType ev = alloca $ \(dat :: Ptr CLCommandType_) -> do
  errcode <- fmap ErrorCode $ raw_clGetEventInfo ev infoid size (castPtr dat) nullPtr
  if errcode == clSuccess
    then fmap getCommandType $ peek dat
    else return Nothing
    where 
      infoid = getCLValue CL_EVENT_COMMAND_TYPE
      size = fromIntegral $ sizeOf (0::CLCommandType_)

-- | Return the event reference count. The reference count returned should be 
-- considered immediately stale. It is unsuitable for general use in applications. 
-- This feature is provided for identifying memory leaks.
clGetEventReferenceCount :: CLEvent -> IO (Maybe CLint)
clGetEventReferenceCount ev = alloca $ \(dat :: Ptr CLint) -> do
  errcode <- fmap ErrorCode $ raw_clGetEventInfo ev infoid size (castPtr dat) nullPtr
  if errcode == clSuccess
    then fmap Just $ peek dat
    else return Nothing
    where 
      infoid = getCLValue CL_EVENT_REFERENCE_COUNT
      size = fromIntegral $ sizeOf (0::CLint)

-- | Return the execution status of the command identified by event.
clGetEventCommandExecutionStatus :: CLEvent -> IO (Maybe CLCommandExecutionStatus)
clGetEventCommandExecutionStatus ev = alloca $ \(dat :: Ptr CLint) -> do
  errcode <- fmap ErrorCode $ raw_clGetEventInfo ev infoid size (castPtr dat) nullPtr
  if errcode == clSuccess
    then fmap (getCommandExecutionStatus) $ peek dat
    else return Nothing
    where 
      infoid = getCLValue CL_EVENT_COMMAND_EXECUTION_STATUS
      size = fromIntegral $ sizeOf (0::CLint)

-- Returns 'True' if the function is executed successfully. It returns 'False' 
-- if event is not a valid event object.
clRetainEvent :: CLEvent -> IO Bool
clRetainEvent ev = wrapCheckSuccess $ raw_clRetainEvent ev

-- | Decrements the event reference count.
-- Decrements the event reference count. The event object is deleted once the 
