{-# LANGUAGE MultiParamTypeClasses #-} 

module OpenCascade.Inheritance 
( SubTypeOf (..)
, DiscriminatedSubTypeOf (..)
, unsafeDowncast
) where

import Foreign.Ptr

class SubTypeOf a b where
    upcast :: Ptr b -> Ptr a
    upcast = Ptr b -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr

class SubTypeOf a b => DiscriminatedSubTypeOf a b where
    downcast :: Ptr a -> IO (Maybe (Ptr b))

unsafeDowncast :: DiscriminatedSubTypeOf a b => Ptr a -> IO (Ptr b)
unsafeDowncast :: forall a b. DiscriminatedSubTypeOf a b => Ptr a -> IO (Ptr b)
unsafeDowncast Ptr a
p = do
    maybeT <- Ptr a -> IO (Maybe (Ptr b))
forall a b.
DiscriminatedSubTypeOf a b =>
Ptr a -> IO (Maybe (Ptr b))
downcast Ptr a
p
    maybe (error "Incorrect subtype in cast") pure maybeT