@@ -159,40 +159,61 @@ printForeign sigMap = do
159
159
SI. hPutStrLn h $ " module " ++ moduleName ++ " where"
160
160
SI. hPutStrLn h " "
161
161
SI. hPutStrLn h " import Foreign.C.Types"
162
+ SI. hPutStrLn h " import Foreign.Marshal.Error ( throwIf )"
162
163
SI. hPutStrLn h " import Foreign.Ptr"
164
+ SI. hPutStrLn h $ " import " ++ moduleNameFor [" GetProcAddress" ] ++ " ( getProcAddress )"
163
165
SI. hPutStrLn h $ " import " ++ moduleNameFor [" Types" ]
164
166
SI. hPutStrLn h " import Numeric.Fixed"
165
167
SI. hPutStrLn h " import Numeric.Half"
166
168
SI. hPutStrLn h " "
169
+ SI. hPutStrLn h " getCommand :: String -> IO (FunPtr a)"
170
+ SI. hPutStrLn h " getCommand cmd ="
171
+ SI. hPutStrLn h " throwIfNullFunPtr (\" unknown OpenGL command \" ++ cmd) $ getProcAddress cmd"
172
+ SI. hPutStrLn h " where throwIfNullFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a)"
173
+ SI. hPutStrLn h " throwIfNullFunPtr = throwIf (== nullFunPtr) . const"
174
+ SI. hPutStrLn h " "
167
175
mapM_ (SI. hPutStrLn h . uncurry makeImportDynamic) (M. assocs sigMap)
168
176
177
+ chunksOf :: Int -> [a ] -> [[a ]]
178
+ chunksOf n = takeWhile (not . null ) . L. unfoldr (Just . splitAt n)
179
+
180
+ justifyRight :: Int -> a -> [a ] -> [a ]
181
+ justifyRight n c xs = reverse . take (max n (length xs)) . (++ repeat c) . reverse $ xs
182
+
169
183
printFunctions :: API -> Registry -> M. Map String String -> IO ()
170
184
printFunctions api registry sigMap = do
171
185
let comment =
172
186
[" All raw functions from the" ,
173
187
" <http://www.opengl.org/registry/ OpenGL registry>." ]
188
+ cmds = chunksOf 100 . M. toAscList . commands $ registry
189
+ mnames = [ [ " Functions" , " F" ++ justifyRight 2 ' 0' (show i) ] |
190
+ i <- [ 1 .. length cmds ] ]
174
191
startModule [" Functions" ] Nothing comment $ \ moduleName h -> do
175
192
SI. hPutStrLn h $ " module " ++ moduleName ++ " ("
176
- SI. hPutStrLn h . separate unCommandName . M. keys . commands $ registry
193
+ SI. hPutStrLn h . separate (( " module " ++ ) . moduleNameFor) $ mnames
177
194
SI. hPutStrLn h " ) where"
178
195
SI. hPutStrLn h " "
179
- SI. hPutStrLn h " import Control.Monad.IO.Class ( MonadIO(..) )"
180
- SI. hPutStrLn h " import Foreign.Marshal.Error ( throwIf )"
181
- SI. hPutStrLn h " import Foreign.Ptr ( Ptr, FunPtr, nullFunPtr )"
182
- SI. hPutStrLn h " import System.IO.Unsafe ( unsafePerformIO )"
196
+ mapM_ (SI. hPutStrLn h . (" import " ++ ) . moduleNameFor) mnames
197
+ CM. zipWithM_ (printSubFunctions api registry sigMap) mnames cmds
198
+
199
+ printSubFunctions :: API -> Registry -> M. Map String String ->
200
+ [String ] -> [(CommandName , Command )] -> IO ()
201
+ printSubFunctions api registry sigMap mname cmds = do
202
+ let comment =
203
+ [" Raw functions from the" ,
204
+ " <http://www.opengl.org/registry/ OpenGL registry>." ]
205
+ startModule mname (Just " {-# OPTIONS_HADDOCK hide #-}" ) comment $ \ moduleName h -> do
206
+ SI. hPutStrLn h $ " module " ++ moduleName ++ " ("
207
+ SI. hPutStrLn h . separate unCommandName . map fst $ cmds
208
+ SI. hPutStrLn h " ) where"
183
209
SI. hPutStrLn h " "
210
+ SI. hPutStrLn h " import Control.Monad.IO.Class ( MonadIO(..) )"
211
+ SI. hPutStrLn h " import Foreign.Ptr"
184
212
SI. hPutStrLn h $ " import " ++ moduleNameFor [" Foreign" ]
185
- SI. hPutStrLn h $ " import " ++ moduleNameFor [" GetProcAddress" ] ++ " ( getProcAddress )"
186
213
SI. hPutStrLn h $ " import " ++ moduleNameFor [" Types" ]
214
+ SI. hPutStrLn h " import System.IO.Unsafe ( unsafePerformIO )"
187
215
SI. hPutStrLn h " "
188
- SI. hPutStrLn h " getCommand :: String -> IO (FunPtr a)"
189
- SI. hPutStrLn h " getCommand cmd ="
190
- SI. hPutStrLn h " throwIfNullFunPtr (\" unknown OpenGL command \" ++ cmd) $ getProcAddress cmd"
191
- SI. hPutStrLn h " "
192
- SI. hPutStrLn h " throwIfNullFunPtr :: String -> IO (FunPtr a) -> IO (FunPtr a)"
193
- SI. hPutStrLn h " throwIfNullFunPtr = throwIf (== nullFunPtr) . const"
194
- SI. hPutStrLn h " "
195
- mapM_ (SI. hPutStrLn h . showCommand api registry sigMap) (M. elems (commands registry))
216
+ mapM_ (SI. hPutStrLn h . showCommand api registry sigMap . snd ) cmds
196
217
197
218
type ExtensionParts = ([TypeName ], [Enum' ], [Command ])
198
219
type ExtensionModule = (ExtensionName , ExtensionName , ExtensionParts )
0 commit comments