{-# LANGUAGE CPP #-}
module Foreign.Hoppy.Generator.Std.Pair (
Options (..),
defaultOptions,
Contents (..),
instantiate,
instantiate',
toExports,
) where
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid (mconcat)
#endif
import Foreign.Hoppy.Generator.Spec
import Foreign.Hoppy.Generator.Spec.Class (
Class,
MethodApplicability (MConst, MNormal),
makeClass,
makeFnMethod,
mkCtor,
mkMethod,
)
import Foreign.Hoppy.Generator.Std.Internal (includeHelper)
import Foreign.Hoppy.Generator.Types
import Foreign.Hoppy.Generator.Version (CppVersion (Cpp2011), activeCppVersion, collect, just, test)
newtype Options = Options
{ optPairClassFeatures :: [ClassFeature]
}
defaultOptions :: Options
defaultOptions = Options []
newtype Contents = Contents
{ c_pair :: Class
}
instantiate :: String -> Type -> Type -> Reqs -> Contents
instantiate pairName a b reqs = instantiate' pairName a b reqs defaultOptions
instantiate' :: String -> Type -> Type -> Reqs -> Options -> Contents
instantiate' pairName a b userReqs opts =
let reqs = mconcat
[ userReqs
, reqInclude $ includeHelper "utility.hpp"
, reqInclude $ includeStd "utility"
]
pair =
addReqs reqs $
classAddFeatures (Assignable : Copyable : optPairClassFeatures opts) $
makeClass (ident1T "std" "pair" [a, b]) (Just $ toExtName pairName) [] $
collect
[ just $ mkCtor "new" np
, just $ mkCtor "newWith" [a, b]
, just $ makeFnMethod (ident2 "hoppy" "utility" "pairFirst") "first" MNormal Nonpure
[refT $ objT pair] $ refT a
, just $ makeFnMethod (ident2 "hoppy" "utility" "pairFirst") "firstConst" MConst Nonpure
[refT $ objT pair] $ refT $ constT a
, just $ makeFnMethod (ident2 "hoppy" "utility" "pairSecond") "second" MNormal Nonpure
[refT $ objT pair] $ refT b
, just $ makeFnMethod (ident2 "hoppy" "utility" "pairSecond") "secondConst" MConst Nonpure
[refT $ objT pair] $ refT $ constT b
, test (activeCppVersion >= Cpp2011) $ mkMethod "swap" [refT $ objT pair] voidT
]
in Contents
{ c_pair = pair
}
toExports :: Contents -> [Export]
toExports m = map (Export . ($ m)) [c_pair]