{-# LINE 1 "./QuantLib/Context.chs" #-}
module QuantLib.Context
(
Error(..)
, SettingsSnapshot(..)
, restoreSettings
, setEvaluationDate
, setEnforceTodaysHistoricFixings
, setIncludeTodaysCashFlows
, setIncludeReferenceDateEvents
, disableUpdates
, enableUpdates
, keepingSettings
, keepingSettingsGc
, collectGarbage
, setExtendedPrecision
, captureSettings
, evaluationDate
, enforceTodaysHistoricFixings
, includeTodaysCashFlows
, includeReferenceDateEvents
, updatesEnabled
, updatesDeferred
, version
, boostVersion
, epsilon
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import Foreign.C.Types(CDouble)
import Foreign.C.String(CString, peekCString)
import System.IO.Unsafe(unsafePerformIO)
import System.Mem(performGC)
import Control.Exception(bracket, SomeException, try, throwIO)
import Control.Monad (forM)
import QuantLib.Time.Date
import QuantLib.Internal
setExtendedPrecision :: IO ()
setExtendedPrecision :: IO ()
setExtendedPrecision =
IO ()
setExtendedPrecision'_ IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# LINE 49 "./QuantLib/Context.chs" #-}
evaluationDate :: IO ((Day))
evaluationDate :: IO Day
evaluationDate =
IO CInt
evaluationDate'_ IO CInt -> (CInt -> IO Day) -> IO Day
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \CInt
res ->
let {res' :: Day
res' = CInt -> Day
toDay CInt
res} in
Day -> IO Day
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Day
res')
{-# LINE 59 "./QuantLib/Context.chs" #-}
setEvaluationDate :: (Maybe Day) -> IO ()
setEvaluationDate :: Maybe Day -> IO ()
setEvaluationDate Maybe Day
a1 =
Maybe Day -> (CInt -> IO ()) -> IO ()
forall a. Maybe Day -> (CInt -> IO a) -> IO a
withMaybeDay Maybe Day
a1 ((CInt -> IO ()) -> IO ()) -> (CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CInt
a1' ->
(Ptr (Ptr ()) -> IO ()) -> IO ()
forall a b. (Ptr (Ptr a) -> IO b) -> IO b
preErrorCheck ((Ptr (Ptr ()) -> IO ()) -> IO ())
-> (Ptr (Ptr ()) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr ())
a2' ->
CInt -> Ptr (Ptr ()) -> IO ()
setEvaluationDate'_ CInt
a1' Ptr (Ptr ())
a2' IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Ptr (Ptr ()) -> IO ()
forall a. Ptr (Ptr a) -> IO ()
errorCheck Ptr (Ptr ())
a2'IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# LINE 64 "./QuantLib/Context.chs" #-}
enforceTodaysHistoricFixings :: IO ((Bool))
enforceTodaysHistoricFixings =
enforceTodaysHistoricFixings'_ >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
{-# LINE 68 "./QuantLib/Context.chs" #-}
setEnforceTodaysHistoricFixings :: (Bool) -> IO ()
setEnforceTodaysHistoricFixings a1 =
let {a1' = C2HSImp.fromBool a1} in
setEnforceTodaysHistoricFixings'_ a1' >>
return ()
{-# LINE 71 "./QuantLib/Context.chs" #-}
includeTodaysCashFlows :: IO ((Maybe Bool))
includeTodaysCashFlows =
includeTodaysCashFlows'_ >>= \res ->
let {res' = toMaybeBool res} in
return (res')
{-# LINE 75 "./QuantLib/Context.chs" #-}
setIncludeTodaysCashFlows :: (Maybe Bool) -> IO ()
setIncludeTodaysCashFlows a1 =
let {a1' = fromMaybeBool a1} in
setIncludeTodaysCashFlows'_ a1' >>
return ()
{-# LINE 78 "./QuantLib/Context.chs" #-}
includeReferenceDateEvents :: IO ((Bool))
includeReferenceDateEvents =
includeReferenceDateEvents'_ >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
{-# LINE 81 "./QuantLib/Context.chs" #-}
setIncludeReferenceDateEvents :: (Bool) -> IO ()
setIncludeReferenceDateEvents a1 =
let {a1' = C2HSImp.fromBool a1} in
setIncludeReferenceDateEvents'_ a1' >>
return ()
{-# LINE 84 "./QuantLib/Context.chs" #-}
disableUpdates :: (Bool)
-> IO ()
disableUpdates :: Bool -> IO ()
disableUpdates Bool
a1 =
let {a1' :: CInt
a1' = Bool -> CInt
forall a. Num a => Bool -> a
C2HSImp.fromBool Bool
a1} in
CInt -> IO ()
disableUpdates'_ CInt
a1' IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# LINE 89 "./QuantLib/Context.chs" #-}
enableUpdates :: IO ()
enableUpdates =
preErrorCheck $ \a1' ->
enableUpdates'_ a1' >>
errorCheck a1'>>
return ()
{-# LINE 92 "./QuantLib/Context.chs" #-}
updatesEnabled :: IO ((Bool))
updatesEnabled =
updatesEnabled'_ >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
{-# LINE 95 "./QuantLib/Context.chs" #-}
updatesDeferred :: IO ((Bool))
updatesDeferred =
updatesDeferred'_ >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
{-# LINE 98 "./QuantLib/Context.chs" #-}
data SettingsSnapshot = SettingsSnapshot
{ snapshotEvaluationDate :: Maybe Day
, snapshotEnforceTodaysHistoricFixings :: Bool
, SettingsSnapshot -> Maybe Bool
snapshotIncludeTodaysCashFlows :: Maybe Bool
, SettingsSnapshot -> Bool
snapshotIncludeReferenceDateEvents :: Bool
, SettingsSnapshot -> Bool
snapshotUpdatesEnabled :: Bool
, SettingsSnapshot -> Bool
snapshotUpdatesDeferred :: Bool
} deriving (SettingsSnapshot -> SettingsSnapshot -> Bool
(SettingsSnapshot -> SettingsSnapshot -> Bool)
-> (SettingsSnapshot -> SettingsSnapshot -> Bool)
-> Eq SettingsSnapshot
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SettingsSnapshot -> SettingsSnapshot -> Bool
== :: SettingsSnapshot -> SettingsSnapshot -> Bool
$c/= :: SettingsSnapshot -> SettingsSnapshot -> Bool
/= :: SettingsSnapshot -> SettingsSnapshot -> Bool
Eq, Int -> SettingsSnapshot -> ShowS
[SettingsSnapshot] -> ShowS
SettingsSnapshot -> String
(Int -> SettingsSnapshot -> ShowS)
-> (SettingsSnapshot -> String)
-> ([SettingsSnapshot] -> ShowS)
-> Show SettingsSnapshot
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SettingsSnapshot -> ShowS
showsPrec :: Int -> SettingsSnapshot -> ShowS
$cshow :: SettingsSnapshot -> String
show :: SettingsSnapshot -> String
$cshowList :: [SettingsSnapshot] -> ShowS
showList :: [SettingsSnapshot] -> ShowS
Show)
captureSettings :: IO SettingsSnapshot
captureSettings :: IO SettingsSnapshot
captureSettings = Maybe Day
-> Bool -> Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot
SettingsSnapshot (Maybe Day
-> Bool -> Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot)
-> IO (Maybe Day)
-> IO
(Bool -> Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Maybe Day)
rawEvaluationDate IO (Bool -> Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot)
-> IO Bool
-> IO (Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO Bool
enforceTodaysHistoricFixings
IO (Maybe Bool -> Bool -> Bool -> Bool -> SettingsSnapshot)
-> IO (Maybe Bool) -> IO (Bool -> Bool -> Bool -> SettingsSnapshot)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO (Maybe Bool)
includeTodaysCashFlows IO (Bool -> Bool -> Bool -> SettingsSnapshot)
-> IO Bool -> IO (Bool -> Bool -> SettingsSnapshot)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO Bool
includeReferenceDateEvents IO (Bool -> Bool -> SettingsSnapshot)
-> IO Bool -> IO (Bool -> SettingsSnapshot)
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO Bool
updatesEnabled IO (Bool -> SettingsSnapshot) -> IO Bool -> IO SettingsSnapshot
forall a b. IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> IO Bool
updatesDeferred
rawEvaluationDate :: IO ((Maybe Day))
rawEvaluationDate :: IO (Maybe Day)
rawEvaluationDate =
IO CInt
rawEvaluationDate'_ IO CInt -> (CInt -> IO (Maybe Day)) -> IO (Maybe Day)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \CInt
res ->
let {res' :: Maybe Day
res' = CInt -> Maybe Day
toMaybeDay CInt
res} in
Maybe Day -> IO (Maybe Day)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Day
res')
{-# LINE 116 "./QuantLib/Context.chs" #-}
restoreSettings :: SettingsSnapshot -> IO ()
restoreSettings s = do
results <- forM
[ setEnforceTodaysHistoricFixings (snapshotEnforceTodaysHistoricFixings s)
, setIncludeTodaysCashFlows (snapshotIncludeTodaysCashFlows s)
, setIncludeReferenceDateEvents (snapshotIncludeReferenceDateEvents s)
, setEvaluationDate (snapshotEvaluationDate s)
, if snapshotUpdatesEnabled s then enableUpdates else disableUpdates (snapshotUpdatesDeferred s)
] (try :: IO () -> IO (Either SomeException ()))
case [e | Left e <- results] of
e : _ -> throwIO e
[] -> pure ()
keepingSettings :: IO b -> IO b
keepingSettings :: forall b. IO b -> IO b
keepingSettings = IO (Ptr ()) -> (Ptr () -> IO ()) -> (Ptr () -> IO b) -> IO b
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO (Ptr ())
qlSavedSettings Ptr () -> IO ()
qlFreeSavedSettings ((Ptr () -> IO b) -> IO b)
-> (IO b -> Ptr () -> IO b) -> IO b -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO b -> Ptr () -> IO b
forall a b. a -> b -> a
const
keepingSettingsGc :: IO b -> IO b
keepingSettingsGc :: forall b. IO b -> IO b
keepingSettingsGc = IO (Ptr ()) -> (Ptr () -> IO ()) -> (Ptr () -> IO b) -> IO b
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO (Ptr ())
qlSavedSettings (\Ptr ()
s -> IO ()
collectGarbage IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr () -> IO ()
qlFreeSavedSettings Ptr ()
s) ((Ptr () -> IO b) -> IO b)
-> (IO b -> Ptr () -> IO b) -> IO b -> IO b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO b -> Ptr () -> IO b
forall a b. a -> b -> a
const
collectGarbage :: IO ()
collectGarbage :: IO ()
collectGarbage = IO ()
performGC IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> IO ()
performGC
foreign import ccall safe "ql.h qlVersion" qlVersion :: IO CString
foreign import ccall safe "ql.h qlBoostVersion" qlBoostVersion :: IO CString
foreign import ccall safe "ql.h qlEpsilon" qlEpsilon :: CDouble
{-# NOINLINE version #-}
version :: String
version :: String
version = IO String -> String
forall a. IO a -> a
unsafePerformIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$ IO CString
qlVersion IO CString -> (CString -> IO String) -> IO String
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
{-# NOINLINE boostVersion #-}
boostVersion :: String
boostVersion :: String
boostVersion = IO String -> String
forall a. IO a -> a
unsafePerformIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$ IO CString
qlBoostVersion IO CString -> (CString -> IO String) -> IO String
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
epsilon :: Double
epsilon :: Double
epsilon = CDouble -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac CDouble
qlEpsilon
foreign import ccall safe "QuantLib/Context.chs.h qlSetExtendedPrecision"
setExtendedPrecision'_ :: (IO ())
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsEvaluationDate"
evaluationDate'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsSetEvaluationDate"
setEvaluationDate'_ :: (C2HSImp.CInt -> ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO ())))
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsEnforceTodaysHistoricFixings"
enforceTodaysHistoricFixings'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsSetEnforceTodaysHistoricFixings"
setEnforceTodaysHistoricFixings'_ :: (C2HSImp.CInt -> (IO ()))
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsIncludeTodaysCashFlows"
includeTodaysCashFlows'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsSetIncludeTodaysCashFlows"
setIncludeTodaysCashFlows'_ :: (C2HSImp.CInt -> (IO ()))
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsIncludeReferenceDateEvents"
includeReferenceDateEvents'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsSetIncludeReferenceDateEvents"
setIncludeReferenceDateEvents'_ :: (C2HSImp.CInt -> (IO ()))
foreign import ccall safe "QuantLib/Context.chs.h qlObservableSettingsDisableUpdates"
disableUpdates'_ :: (C2HSImp.CInt -> (IO ()))
foreign import ccall safe "QuantLib/Context.chs.h qlObservableSettingsEnableUpdates"
enableUpdates'_ :: ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> (IO ()))
foreign import ccall safe "QuantLib/Context.chs.h qlObservableSettingsUpdatesEnabled"
updatesEnabled'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlObservableSettingsUpdatesDeferred"
updatesDeferred'_ :: (IO C2HSImp.CInt)
foreign import ccall safe "QuantLib/Context.chs.h qlSettingsRawEvaluationDate"
rawEvaluationDate'_ :: (IO C2HSImp.CInt)