{-# LANGUAGE TemplateHaskell #-}
module STD.SharedPtr.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.SharedPtr.Template
t_newSharedPtr0 :: Type -> String -> Q Exp
t_newSharedPtr0 :: Type -> String -> Q Exp
t_newSharedPtr0 Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc
(Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_newSharedPtr0" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_ = let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| IO (SharedPtr $( tp1 )) |]
t_newSharedPtr :: Type -> String -> Q Exp
t_newSharedPtr :: Type -> String -> Q Exp
t_newSharedPtr Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_new" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_
= let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| $( tp1 ) -> IO (SharedPtr $( tp1 )) |]
t_get :: Type -> String -> Q Exp
t_get :: Type -> String -> Q Exp
t_get Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_get" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_
= let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| SharedPtr $( tp1 ) -> IO $( tp1 ) |]
t_reset :: Type -> String -> Q Exp
t_reset :: Type -> String -> Q Exp
t_reset Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_reset" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_
= let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| SharedPtr $( tp1 ) -> IO () |]
t_use_count :: Type -> String -> Q Exp
t_use_count :: Type -> String -> Q Exp
t_use_count Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_use_count" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_
= let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| SharedPtr $( tp1 ) -> IO CInt |]
t_deleteSharedPtr :: Type -> String -> Q Exp
t_deleteSharedPtr :: Type -> String -> Q Exp
t_deleteSharedPtr Type
typ1 String
suffix
= forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"SharedPtr_delete" forall a. Semigroup a => a -> a -> a
<> String
n, forall {m :: * -> *} {p}. Quote m => p -> m Type
tyf)
where tyf :: p -> m Type
tyf p
_
= let tp1 :: m Type
tp1 = forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
typ1 in [t| SharedPtr $( tp1 ) -> IO () |]
genSharedPtrInstanceFor ::
IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genSharedPtrInstanceFor :: IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genSharedPtrInstanceFor IsCPrimitive
isCprim (Q Type
qtyp1, TemplateParamInfo
param1)
= do let params :: [String]
params = forall a b. (a -> b) -> [a] -> [b]
map TemplateParamInfo -> String
tpinfoSuffix [TemplateParamInfo
param1]
let suffix :: String
suffix = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\ TemplateParamInfo
x -> String
"_" forall a. [a] -> [a] -> [a]
++ TemplateParamInfo -> String
tpinfoSuffix TemplateParamInfo
x) [TemplateParamInfo
param1]
String
callmod_ <- 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 <- forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkNew String
"newSharedPtr0" Type -> String -> Q Exp
t_newSharedPtr0 Type
typ1 String
suffix
Dec
f2 <- forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkNew String
"newSharedPtr" Type -> String -> Q Exp
t_newSharedPtr Type
typ1 String
suffix
Dec
f3 <- 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 <- 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
f5 <- forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkMember String
"use_count" Type -> String -> Q Exp
t_use_count Type
typ1 String
suffix
Dec
f6 <- forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkDelete String
"deleteSharedPtr" Type -> String -> Q Exp
t_deleteSharedPtr 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 SharedPtr_newSharedPtr0(callmod, tp1) \\\nextern \"C\" {\\\nvoid* SharedPtr_newSharedPtr0_##tp1 ( );}\\\ninline void* SharedPtr_newSharedPtr0_##tp1 ( ) {\\\nreturn static_cast<void*>(new std::shared_ptr<tp1>());\\\n}\\\nauto a_##callmod##_SharedPtr_newSharedPtr0_##tp1=SharedPtr_newSharedPtr0_##tp1;\n\n\n#define SharedPtr_new(callmod, tp1) \\\nextern \"C\" {\\\nvoid* SharedPtr_new_##tp1 ( tp1##_p p );}\\\ninline void* SharedPtr_new_##tp1 ( tp1##_p p ) {\\\nreturn static_cast<void*>(new std::shared_ptr<tp1>(from_nonconst_to_nonconst<tp1, tp1##_t>(p)));\\\n}\\\nauto a_##callmod##_SharedPtr_new_##tp1=SharedPtr_new_##tp1;\n\n\n#define SharedPtr_get(callmod, tp1) \\\nextern \"C\" {\\\ntp1##_p SharedPtr_get_##tp1 ( void* p );}\\\ninline tp1##_p SharedPtr_get_##tp1 ( void* p ) {\\\nreturn from_nonconst_to_nonconst<tp1##_t, tp1>((static_cast<std::shared_ptr<tp1>*>(p))->get());\\\n}\\\nauto a_##callmod##_SharedPtr_get_##tp1=SharedPtr_get_##tp1;\n\n\n#define SharedPtr_reset(callmod, tp1) \\\nextern \"C\" {\\\nvoid SharedPtr_reset_##tp1 ( void* p );}\\\ninline void SharedPtr_reset_##tp1 ( void* p ) {\\\n(static_cast<std::shared_ptr<tp1>*>(p))->reset();\\\n}\\\nauto a_##callmod##_SharedPtr_reset_##tp1=SharedPtr_reset_##tp1;\n\n\n#define SharedPtr_use_count(callmod, tp1) \\\nextern \"C\" {\\\nint SharedPtr_use_count_##tp1 ( void* p );}\\\ninline int SharedPtr_use_count_##tp1 ( void* p ) {\\\nreturn (static_cast<std::shared_ptr<tp1>*>(p))->use_count();\\\n}\\\nauto a_##callmod##_SharedPtr_use_count_##tp1=SharedPtr_use_count_##tp1;\n\n\n#define SharedPtr_delete(callmod, tp1) \\\nextern \"C\" {\\\nvoid SharedPtr_delete_##tp1 ( void* p );}\\\ninline void SharedPtr_delete_##tp1 ( void* p ) {\\\ndelete static_cast<std::shared_ptr<tp1>*>(p);\\\n}\\\nauto a_##callmod##_SharedPtr_delete_##tp1=SharedPtr_delete_##tp1;\n\n\n#define SharedPtr_newSharedPtr0_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* SharedPtr_newSharedPtr0_##tp1 ( );}\\\ninline void* SharedPtr_newSharedPtr0_##tp1 ( ) {\\\nreturn static_cast<void*>(new std::shared_ptr<tp1>());\\\n}\\\nauto a_##callmod##_SharedPtr_newSharedPtr0_##tp1=SharedPtr_newSharedPtr0_##tp1;\n\n\n#define SharedPtr_new_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* SharedPtr_new_##tp1 ( tp1 p );}\\\ninline void* SharedPtr_new_##tp1 ( tp1 p ) {\\\nreturn static_cast<void*>(new std::shared_ptr<tp1>(p));\\\n}\\\nauto a_##callmod##_SharedPtr_new_##tp1=SharedPtr_new_##tp1;\n\n\n#define SharedPtr_get_s(callmod, tp1) \\\nextern \"C\" {\\\ntp1 SharedPtr_get_##tp1 ( void* p );}\\\ninline tp1 SharedPtr_get_##tp1 ( void* p ) {\\\nreturn (static_cast<std::shared_ptr<tp1>*>(p))->get();\\\n}\\\nauto a_##callmod##_SharedPtr_get_##tp1=SharedPtr_get_##tp1;\n\n\n#define SharedPtr_reset_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid SharedPtr_reset_##tp1 ( void* p );}\\\ninline void SharedPtr_reset_##tp1 ( void* p ) {\\\n(static_cast<std::shared_ptr<tp1>*>(p))->reset();\\\n}\\\nauto a_##callmod##_SharedPtr_reset_##tp1=SharedPtr_reset_##tp1;\n\n\n#define SharedPtr_use_count_s(callmod, tp1) \\\nextern \"C\" {\\\nint SharedPtr_use_count_##tp1 ( void* p );}\\\ninline int SharedPtr_use_count_##tp1 ( void* p ) {\\\nreturn (static_cast<std::shared_ptr<tp1>*>(p))->use_count();\\\n}\\\nauto a_##callmod##_SharedPtr_use_count_##tp1=SharedPtr_use_count_##tp1;\n\n\n#define SharedPtr_delete_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid SharedPtr_delete_##tp1 ( void* p );}\\\ninline void SharedPtr_delete_##tp1 ( void* p ) {\\\ndelete static_cast<std::shared_ptr<tp1>*>(p);\\\n}\\\nauto a_##callmod##_SharedPtr_delete_##tp1=SharedPtr_delete_##tp1;\n\n\n#define SharedPtr_instance(callmod, tp1) \\\nSharedPtr_newSharedPtr0(callmod, tp1)\\\nSharedPtr_new(callmod, tp1)\\\nSharedPtr_get(callmod, tp1)\\\nSharedPtr_reset(callmod, tp1)\\\nSharedPtr_use_count(callmod, tp1)\\\nSharedPtr_delete(callmod, tp1)\n\n\n#define SharedPtr_instance_s(callmod, tp1) \\\nSharedPtr_newSharedPtr0_s(callmod, tp1)\\\nSharedPtr_new_s(callmod, tp1)\\\nSharedPtr_get_s(callmod, tp1)\\\nSharedPtr_reset_s(callmod, tp1)\\\nSharedPtr_use_count_s(callmod, tp1)\\\nSharedPtr_delete_s(callmod, tp1)\n\n"
forall a. [a] -> [a] -> [a]
++
let headers :: [HeaderName]
headers = 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 (forall (f :: * -> *). HeaderName -> CMacro f
Include HeaderName
x)
in forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap HeaderName -> String
f [HeaderName]
headers
forall a. [a] -> [a] -> [a]
++
let nss :: [Namespace]
nss = 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 (forall (f :: * -> *). Namespace -> CStatement f
UsingNamespace Namespace
x)
in forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Namespace -> String
f [Namespace]
nss
forall a. [a] -> [a] -> [a]
++
String
"SharedPtr_instance" forall a. [a] -> [a] -> [a]
++
(case IsCPrimitive
isCprim of
IsCPrimitive
CPrim -> String
"_s"
IsCPrimitive
NonCPrim -> String
"")
forall a. [a] -> [a] -> [a]
++ String
"(" forall a. [a] -> [a] -> [a]
++ forall a. [a] -> [[a]] -> [a]
intercalate String
", " (String
callmod forall a. a -> [a] -> [a]
: [String]
params) forall a. [a] -> [a] -> [a]
++ String
")\n"))
let lst :: [Dec]
lst = [Dec
f1, Dec
f2, Dec
f3, Dec
f4, Dec
f5, Dec
f6]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Cxt -> Type -> [Dec] -> Dec
mkInstance [] (Type -> Type -> Type
AppT (String -> Type
con String
"ISharedPtr") Type
typ1) [Dec]
lst]