{-# LANGUAGE ForeignFunctionInterface #-}
module Util.Object (
ObjectID(..),
Object(..),
newObject,
staticObject,
newInt
) where
newtype ObjectID = ObjectID Int deriving (Eq,Ord)
class Object o where
objectID :: o -> ObjectID
instance Show ObjectID where
showsPrec d (ObjectID n) r = showsPrec d n r
instance Read ObjectID where
readsPrec p b =
case reads b of
[] -> []
((v,xs):_) ->[(ObjectID v,xs)]
foreign import ccall unsafe "new_object.h next_object_id" newInt :: IO Int
newObject :: IO ObjectID
newObject =
do
nextInt <- newInt
return(ObjectID nextInt)
staticObject :: Int -> ObjectID
staticObject i
| i>0 = ObjectID (-i)
| True = error "staticObject not given positive integer"