packages feed

hsp-0.2: HSP/Data/Application.hs

-----------------------------------------------------------------------------
-- |
-- Module      :  HSP.Data.Application
-- Copyright   :  (c) Niklas Broberg 2004,
-- License     :  BSD-style (see the file LICENSE.txt)
--
-- Maintainer  :  Niklas Broberg, d00nibro@dtek.chalmers.se
-- Stability   :  experimental
-- Portability :  requires undecidable and overlapping instances
--
-- Defines the Application object available to HSP pages.
-----------------------------------------------------------------------------
module HSP.Data.Application (
	  Application		-- ^ The 'Application' datatype is abstract
	, toApplication		-- ^ :: Typeable a => a -> Application
	, fromApplication	-- ^ :: Typeable a => Application -> Maybe a
	, defaultApplication	-- ^ :: Application
	) where

import Data.Dynamic

{- |
   The 'Application' datatype is a wrapper around a 'Dynamic' value.
   The reason we need a 'Dynamic' is that a user may want just about
   anything in the 'Application'.
-}
data Application = MkApp { unwrap :: Dynamic }

-- | Lifting a user-defined application-scope datatype into an 'Application' object.
-- Note that the 'Typeable' constraint limits the choices of datatypes to
-- monomorphic ones.
toApplication :: Typeable a
	      => a 		-- ^ The value to put in the 'Application' object
	      -> Application	-- ^ returns: The value wrapped up as an 'Application' object
toApplication = MkApp . toDyn

-- | Extracting the user-defined application-scope datatype from its 'Application'
-- wrapper.
fromApplication :: Typeable a
		=> Application 	-- ^ The 'Application' object to unwrap
		-> Maybe a	-- ^ returns: Just the value if the expected type coincides with
				-- the one put in, 'Nothing' otherwise
fromApplication = fromDynamic . unwrap


-- | An empty Application object to use when no user-defined Application.hs exists.
defaultApplication :: Application
defaultApplication = toApplication ()