{-# LANGUAGE TemplateHaskell #-}
module STD.VectorIterator.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.VectorIterator.Template

t_deRef :: Type -> String -> Q Exp
t_deRef :: Type -> String -> Q Exp
t_deRef Type
typ1 String
suffix
  = forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc (Type
typ1, String
suffix, \ String
n -> String
"VectorIterator_deRef" 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| VectorIterator $( tp1 ) -> IO $( tp1 ) |]

t_increment :: Type -> String -> Q Exp
t_increment :: Type -> String -> Q Exp
t_increment Type
typ1 String
suffix
  = forall types.
(types, String, String -> String, types -> Q Type) -> Q Exp
mkTFunc
      (Type
typ1, String
suffix, \ String
n -> String
"VectorIterator_increment" 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| VectorIterator $( tp1 ) -> IO (VectorIterator $( tp1 )) |]

genVectorIteratorInstanceFor ::
                             IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genVectorIteratorInstanceFor :: IsCPrimitive -> (Q Type, TemplateParamInfo) -> Q [Dec]
genVectorIteratorInstanceFor 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
mkMember String
"deRef" Type -> String -> Q Exp
t_deRef Type
typ1 String
suffix
       Dec
f2 <- forall types.
String -> (types -> String -> Q Exp) -> types -> String -> Q Dec
mkMember String
"increment" Type -> String -> Q Exp
t_increment Type
typ1 String
suffix
       Q () -> Q ()
addModFinalizer
         (ForeignSrcLang -> String -> Q ()
addForeignSource ForeignSrcLang
LangCxx
            (String
"\n#include \"MacroPatternMatch.h\"\n\n\n#include \"vector\"\n\n\n#define VectorIterator_deRef(callmod, tp1) \\\nextern \"C\" {\\\ntp1##_p VectorIterator_deRef_##tp1 ( void* p );}\\\ninline tp1##_p VectorIterator_deRef_##tp1 ( void* p ) {\\\nreturn from_nonconst_to_nonconst<tp1##_t, tp1>((tp1*)&((static_cast<std::vector<tp1>::iterator*>(p))->operator*()));\\\n}\\\nauto a_##callmod##_VectorIterator_deRef_##tp1=VectorIterator_deRef_##tp1;\n\n\n#define VectorIterator_increment(callmod, tp1) \\\nextern \"C\" {\\\nvoid* VectorIterator_increment_##tp1 ( void* p );}\\\ninline void* VectorIterator_increment_##tp1 ( void* p ) {\\\nstd::vector<tp1>::iterator* r=new std::vector<tp1>::iterator((static_cast<std::vector<tp1>::iterator*>(p))->operator++());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_VectorIterator_increment_##tp1=VectorIterator_increment_##tp1;\n\n\n#define VectorIterator_deRef_s(callmod, tp1) \\\nextern \"C\" {\\\ntp1 VectorIterator_deRef_##tp1 ( void* p );}\\\ninline tp1 VectorIterator_deRef_##tp1 ( void* p ) {\\\nreturn (static_cast<std::vector<tp1>::iterator*>(p))->operator*();\\\n}\\\nauto a_##callmod##_VectorIterator_deRef_##tp1=VectorIterator_deRef_##tp1;\n\n\n#define VectorIterator_increment_s(callmod, tp1) \\\nextern \"C\" {\\\nvoid* VectorIterator_increment_##tp1 ( void* p );}\\\ninline void* VectorIterator_increment_##tp1 ( void* p ) {\\\nstd::vector<tp1>::iterator* r=new std::vector<tp1>::iterator((static_cast<std::vector<tp1>::iterator*>(p))->operator++());return static_cast<void*>(r);\\\n}\\\nauto a_##callmod##_VectorIterator_increment_##tp1=VectorIterator_increment_##tp1;\n\n\n#define VectorIterator_instance(callmod, tp1) \\\nVectorIterator_deRef(callmod, tp1)\\\nVectorIterator_increment(callmod, tp1)\n\n\n#define VectorIterator_instance_s(callmod, tp1) \\\nVectorIterator_deRef_s(callmod, tp1)\\\nVectorIterator_increment_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
"VectorIterator_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]
       forall (f :: * -> *) a. Applicative f => a -> f a
pure [Cxt -> Type -> [Dec] -> Dec
mkInstance [] (Type -> Type -> Type
AppT (String -> Type
con String
"IVectorIterator") Type
typ1) [Dec]
lst]