Permalink
Browse files

Added a DynRef type.

  • Loading branch information...
1 parent 86d68b8 commit e9ccfb9dc1ed7f2e965c24f7ff3dd0ea75395a7a @luqui committed Nov 17, 2008
Showing with 28 additions and 0 deletions.
  1. +28 −0 Udon/DynRef.hs
View
@@ -0,0 +1,28 @@
+module Udon.DynRef
+ ( DynType, makeDynType
+ , DynRef, dynRefToExtRef, unsafeExtRefToDynRef
+ )
+where
+
+import Data.Typeable
+import Udon.Hash
+import Udon.DataDesc
+
+type TypeID = String
+newtype DynType a = DynType TypeID
+
+-- Brittle!
+makeDynType :: (Typeable a) => a -> DynType a
+makeDynType = DynType . show . typeOf
+
+data DynRef = DynRef TypeID Hash
+
+dynRefToExtRef :: DynType a -> DynRef -> Maybe (ExtRef a)
+dynRefToExtRef (DynType tid) (DynRef tid' h)
+ | tid == tid' = Just $ unsafeMakeExtRef h Nothing
+ | otherwise = Nothing
+
+-- unsafe because it forgets any data the ExtRef was storing
+unsafeExtRefToDynRef :: DynType a -> ExtRef a -> DynRef
+unsafeExtRefToDynRef (DynType tid) ref = DynRef tid (extRefHash ref)
+

0 comments on commit e9ccfb9

Please sign in to comment.