{-# LANGUAGE TemplateHaskell #-}
module OGDF.List.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 OGDF.List.Template
import OGDF.ListIterator.Template

t_begin :: Type -> String -> Q Exp
t_begin :: Type -> String -> Q Exp
t_begin 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
"List_begin" 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| List $( tp1 ) -> IO (ListIterator $( tp1 )) |]

t_end :: Type -> String -> Q Exp
t_end :: Type -> String -> Q Exp
t_end 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
"List_end" 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| List $( tp1 ) -> IO (ListIterator $( tp1 )) |]

t_pushBack :: Type -> String -> Q Exp
t_pushBack :: Type -> String -> Q Exp
t_pushBack 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
"List_pushBack" 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| List $( tp1 ) -> $( tp1 ) -> IO (ListIterator $( tp1 )) |]

genListInstanceFor ::
                   IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genListInstanceFor :: IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genListInstanceFor 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
mkMember String
"begin" Type -> String -> Q Exp
t_begin 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
mkMember String
"end" Type -> String -> Q Exp
t_end 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
"pushBack" Type -> String -> Q Exp
t_pushBack Type
typ1 String
suffix
       Q () -> Q ()
addModFinalizer
         (ForeignSrcLang -> String -> Q ()
addForeignSource ForeignSrcLang
LangCxx
            (String
"\n#include \"MacroPatternMatch.h\"\n\n\n#include \"ogdf/basic/List.h\"\n\n\n#define List_begin(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_begin_##tp1 ( void* p );}\\\ninline void* List_begin_##tp1 ( void* p ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->begin());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_begin_##tp1=List_begin_##tp1;\n\n\n#define List_end(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_end_##tp1 ( void* p );}\\\ninline void* List_end_##tp1 ( void* p ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->end());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_end_##tp1=List_end_##tp1;\n\n\n#define List_pushBack(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_pushBack_##tp1 ( void* p, tp1##_p x );}\\\ninline void* List_pushBack_##tp1 ( void* p, tp1##_p x ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->pushBack(*(from_nonconst_to_nonconst<tp1, tp1##_t>(x))));return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_pushBack_##tp1=List_pushBack_##tp1;\n\n\n#define List_begin_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_begin_##tp1 ( void* p );}\\\ninline void* List_begin_##tp1 ( void* p ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->begin());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_begin_##tp1=List_begin_##tp1;\n\n\n#define List_end_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_end_##tp1 ( void* p );}\\\ninline void* List_end_##tp1 ( void* p ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->end());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_end_##tp1=List_end_##tp1;\n\n\n#define List_pushBack_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* List_pushBack_##tp1 ( void* p, tp1 x );}\\\ninline void* List_pushBack_##tp1 ( void* p, tp1 x ) {\\\nListIterator<tp1>* r=new ListIterator<tp1>((static_cast<ogdf::List<tp1>*>(p))->pushBack(x));return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_List_pushBack_##tp1=List_pushBack_##tp1;\n\n\n#define List_instance(callmod, tp1) \\\nList_begin(callmod, tp1)\\\nList_end(callmod, tp1)\\\nList_pushBack(callmod, tp1)\n\n\n#define List_instance_s(callmod, tp1) \\\nList_begin_s(callmod, tp1)\\\nList_end_s(callmod, tp1)\\\nList_pushBack_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
"List_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] -> 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
"IList") Type
typ1) [Dec]
lst]