{-# LANGUAGE TemplateHaskell #-}
module STD.UniquePtr.TH where
import Data.Char
import Data.List
import Data.Monoid
import Foreign.C.Types
import Foreign.Ptr
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import FFICXX.Runtime.CodeGen.Cxx
import FFICXX.Runtime.TH
import STD.UniquePtr.Template

t_newUniquePtr0 :: Type -> String -> Q Exp
t_newUniquePtr0 :: Type -> String -> Q Exp
t_newUniquePtr0 Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc
      (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_newUniquePtr0" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_ = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| IO (UniquePtr $( tp1 )) |]

t_newUniquePtr :: Type -> String -> Q Exp
t_newUniquePtr :: Type -> String -> Q Exp
t_newUniquePtr Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_new" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_
          = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| $( tp1 ) -> IO (UniquePtr $( tp1 )) |]

t_get :: Type -> String -> Q Exp
t_get :: Type -> String -> Q Exp
t_get Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_get" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_
          = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| UniquePtr $( tp1 ) -> IO $( tp1 ) |]

t_release :: Type -> String -> Q Exp
t_release :: Type -> String -> Q Exp
t_release Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_release" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_
          = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| UniquePtr $( tp1 ) -> IO $( tp1 ) |]

t_reset :: Type -> String -> Q Exp
t_reset :: Type -> String -> Q Exp
t_reset Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_reset" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_
          = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| UniquePtr $( tp1 ) -> IO () |]

t_deleteUniquePtr :: Type -> String -> Q Exp
t_deleteUniquePtr :: Type -> String -> Q Exp
t_deleteUniquePtr Type
typ1 String
suffix
  = (Type, String, String -> String, Type -> Q Type) -> Q Exp
forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"UniquePtr_delete" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
n, Type -> Q Type
forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
  where tyf :: p -> m Type
tyf p
_
          = let tp1 :: m Type
tp1 = Type -> m Type
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| UniquePtr $( tp1 ) -> IO () |]

genUniquePtrInstanceFor ::
                        IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genUniquePtrInstanceFor :: IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genUniquePtrInstanceFor IsCPrimitive
isCprim (Q Type
qtyp1, TemplateParamInfo
param1)
  = do let params :: [String]
params = (TemplateParamInfo -> String) -> [TemplateParamInfo] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map TemplateParamInfo -> String
tpinfoSuffix [TemplateParamInfo
param1]
       let suffix :: String
suffix = (TemplateParamInfo -> String) -> [TemplateParamInfo] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\ TemplateParamInfo
x -> String
"_" String -> String -> String
forall a. [a] -> [a] -> [a]
++ TemplateParamInfo -> String
tpinfoSuffix TemplateParamInfo
x) [TemplateParamInfo
param1]
       String
callmod_ <- (Loc -> String) -> Q Loc -> Q String
forall a b. (a -> b) -> Q a -> Q b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Loc -> String
loc_module Q Loc
location
       let callmod :: String
callmod = String -> String
dot2_ String
callmod_
       Type
typ1 <- Q Type
qtyp1
       Dec
f1 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkNew String
"newUniquePtr0" Type -> String -> Q Exp
t_newUniquePtr0 Type
typ1 String
suffix
       Dec
f2 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkNew String
"newUniquePtr" Type -> String -> Q Exp
t_newUniquePtr Type
typ1 String
suffix
       Dec
f3 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkMember String
"get" Type -> String -> Q Exp
t_get Type
typ1 String
suffix
       Dec
f4 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkMember String
"release" Type -> String -> Q Exp
t_release Type
typ1 String
suffix
       Dec
f5 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkMember String
"reset" Type -> String -> Q Exp
t_reset Type
typ1 String
suffix
       Dec
f6 <- String -> (Type -> String -> Q Exp) -> Type -> String -> Q Dec
forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkDelete String
"deleteUniquePtr" Type -> String -> Q Exp
t_deleteUniquePtr Type
typ1 String
suffix
       Q () -> Q ()
addModFinalizer
         (ForeignSrcLang -> String -> Q ()
addForeignSource ForeignSrcLang
LangCxx
            (String
"\n#include \"MacroPatternMatch.h\"\n\n\n#include \"memory\"\n\n\n#define UniquePtr_newUniquePtr0(callmod, tp1) \\\nextern \"C\" {\\\nvoid* UniquePtr_newUniquePtr0_##tp1 (  );}\\\ninline void* UniquePtr_newUniquePtr0_##tp1 (  ) {\\\nreturn static_cast<void*>(new std::unique_ptr<tp1>());\\\n}\\\nauto a_##callmod##_UniquePtr_newUniquePtr0_##tp1=UniquePtr_newUniquePtr0_##tp1;\n\n\n#define UniquePtr_new(callmod, tp1) \\\nextern \"C\" {\\\nvoid* UniquePtr_new_##tp1 ( tp1##_p p );}\\\ninline void* UniquePtr_new_##tp1 ( tp1##_p p ) {\\\nreturn static_cast<void*>(new std::unique_ptr<tp1>(from_nonconst_to_nonconst<tp1, tp1##_t>(p)));\\\n}\\\nauto a_##callmod##_UniquePtr_new_##tp1=UniquePtr_new_##tp1;\n\n\n#define UniquePtr_get(callmod, tp1) \\\nextern \"C\" {\\\ntp1##_p UniquePtr_get_##tp1 ( void* p );}\\\ninline tp1##_p UniquePtr_get_##tp1 ( void* p ) {\\\nreturn from_nonconst_to_nonconst<tp1##_t, tp1>((static_cast<std::unique_ptr<tp1>*>(p))->get());\\\n}\\\nauto a_##callmod##_UniquePtr_get_##tp1=UniquePtr_get_##tp1;\n\n\n#define UniquePtr_release(callmod, tp1) \\\nextern \"C\" {\\\ntp1##_p UniquePtr_release_##tp1 ( void* p );}\\\ninline tp1##_p UniquePtr_release_##tp1 ( void* p ) {\\\nreturn from_nonconst_to_nonconst<tp1##_t, tp1>((static_cast<std::unique_ptr<tp1>*>(p))->release());\\\n}\\\nauto a_##callmod##_UniquePtr_release_##tp1=UniquePtr_release_##tp1;\n\n\n#define UniquePtr_reset(callmod, tp1) \\\nextern \"C\" {\\\nvoid UniquePtr_reset_##tp1 ( void* p );}\\\ninline void UniquePtr_reset_##tp1 ( void* p ) {\\\n(static_cast<std::unique_ptr<tp1>*>(p))->reset();\\\n}\\\nauto a_##callmod##_UniquePtr_reset_##tp1=UniquePtr_reset_##tp1;\n\n\n#define UniquePtr_delete(callmod, tp1) \\\nextern \"C\" {\\\nvoid UniquePtr_delete_##tp1 ( void* p );}\\\ninline void UniquePtr_delete_##tp1 ( void* p ) {\\\ndelete static_cast<std::unique_ptr<tp1>*>(p);\\\n}\\\nauto a_##callmod##_UniquePtr_delete_##tp1=UniquePtr_delete_##tp1;\n\n\n#define UniquePtr_newUniquePtr0_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* UniquePtr_newUniquePtr0_##tp1 (  );}\\\ninline void* UniquePtr_newUniquePtr0_##tp1 (  ) {\\\nreturn static_cast<void*>(new std::unique_ptr<tp1>());\\\n}\\\nauto a_##callmod##_UniquePtr_newUniquePtr0_##tp1=UniquePtr_newUniquePtr0_##tp1;\n\n\n#define UniquePtr_new_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* UniquePtr_new_##tp1 ( tp1 p );}\\\ninline void* UniquePtr_new_##tp1 ( tp1 p ) {\\\nreturn static_cast<void*>(new std::unique_ptr<tp1>(p));\\\n}\\\nauto a_##callmod##_UniquePtr_new_##tp1=UniquePtr_new_##tp1;\n\n\n#define UniquePtr_get_s(callmod, tp1) \\\nextern \"C\" {\\\ntp1 UniquePtr_get_##tp1 ( void* p );}\\\ninline tp1 UniquePtr_get_##tp1 ( void* p ) {\\\nreturn (static_cast<std::unique_ptr<tp1>*>(p))->get();\\\n}\\\nauto a_##callmod##_UniquePtr_get_##tp1=UniquePtr_get_##tp1;\n\n\n#define UniquePtr_release_s(callmod, tp1) \\\nextern \"C\" {\\\ntp1 UniquePtr_release_##tp1 ( void* p );}\\\ninline tp1 UniquePtr_release_##tp1 ( void* p ) {\\\nreturn (static_cast<std::unique_ptr<tp1>*>(p))->release();\\\n}\\\nauto a_##callmod##_UniquePtr_release_##tp1=UniquePtr_release_##tp1;\n\n\n#define UniquePtr_reset_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid UniquePtr_reset_##tp1 ( void* p );}\\\ninline void UniquePtr_reset_##tp1 ( void* p ) {\\\n(static_cast<std::unique_ptr<tp1>*>(p))->reset();\\\n}\\\nauto a_##callmod##_UniquePtr_reset_##tp1=UniquePtr_reset_##tp1;\n\n\n#define UniquePtr_delete_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid UniquePtr_delete_##tp1 ( void* p );}\\\ninline void UniquePtr_delete_##tp1 ( void* p ) {\\\ndelete static_cast<std::unique_ptr<tp1>*>(p);\\\n}\\\nauto a_##callmod##_UniquePtr_delete_##tp1=UniquePtr_delete_##tp1;\n\n\n#define UniquePtr_instance(callmod, tp1) \\\nUniquePtr_newUniquePtr0(callmod, tp1)\\\nUniquePtr_new(callmod, tp1)\\\nUniquePtr_get(callmod, tp1)\\\nUniquePtr_release(callmod, tp1)\\\nUniquePtr_reset(callmod, tp1)\\\nUniquePtr_delete(callmod, tp1)\n\n\n#define UniquePtr_instance_s(callmod, tp1) \\\nUniquePtr_newUniquePtr0_s(callmod, tp1)\\\nUniquePtr_new_s(callmod, tp1)\\\nUniquePtr_get_s(callmod, tp1)\\\nUniquePtr_release_s(callmod, tp1)\\\nUniquePtr_reset_s(callmod, tp1)\\\nUniquePtr_delete_s(callmod, tp1)\n\n"
               String -> String -> String
forall a. [a] -> [a] -> [a]
++
               let headers :: [HeaderName]
headers = (TemplateParamInfo -> [HeaderName])
-> [TemplateParamInfo] -> [HeaderName]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TemplateParamInfo -> [HeaderName]
tpinfoCxxHeaders [TemplateParamInfo
param1]
                   f :: HeaderName -> String
f HeaderName
x = CMacro Identity -> String
renderCMacro (HeaderName -> CMacro Identity
forall (f :: * -> *). HeaderName -> CMacro f
Include HeaderName
x)
                 in (HeaderName -> String) -> [HeaderName] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap HeaderName -> String
f [HeaderName]
headers
                 String -> String -> String
forall a. [a] -> [a] -> [a]
++
                 let nss :: [Namespace]
nss = (TemplateParamInfo -> [Namespace])
-> [TemplateParamInfo] -> [Namespace]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TemplateParamInfo -> [Namespace]
tpinfoCxxNamespaces [TemplateParamInfo
param1]
                     f :: Namespace -> String
f Namespace
x = CStatement Identity -> String
renderCStmt (Namespace -> CStatement Identity
forall (f :: * -> *). Namespace -> CStatement f
UsingNamespace Namespace
x)
                   in (Namespace -> String) -> [Namespace] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Namespace -> String
f [Namespace]
nss
                   String -> String -> String
forall a. [a] -> [a] -> [a]
++
                   String
"UniquePtr_instance" String -> String -> String
forall a. [a] -> [a] -> [a]
++
                     (case IsCPrimitive
isCprim of
                          IsCPrimitive
CPrim -> String
"_s"
                          IsCPrimitive
NonCPrim -> String
"")
                       String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " (String
callmod String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
params) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")\n"))
       let lst :: [Dec]
lst = [Dec
f1, Dec
f2, Dec
f3, Dec
f4, Dec
f5, Dec
f6]
       [Dec] -> Q [Dec]
forall a. a -> Q a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Cxt -> Type -> [Dec] -> Dec
mkInstance [] (Type -> Type -> Type
AppT (String -> Type
con String
"IUniquePtr") Type
typ1) [Dec]
lst]