-- This file is part of Qtah.
--
-- Copyright 2015-2021 The Qtah Authors.
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

-- | Bindings for @QList@.
module Graphics.UI.Qtah.Generator.Interface.Core.QList (
  -- * Template
  Options (..),
  defaultOptions,
  Contents (..),
  inheritHasContents,
  instantiate,
  -- * Instantiations
  allModules,
  c_QListInt,
  c_QListQAbstractButton,
  c_QListQByteArray,
  c_QListQItemSelectionRange,
  c_QListQModelIndex,
  c_QListQObject,
  c_QListQSize,
  c_QListQString,
  c_QListQVariant,
  c_QListQWidget,
  c_QListQTreeWidgetItem,
  ) where

import Control.Monad (forM_, when)
import Foreign.Hoppy.Generator.Language.Haskell (
  Generator,
  HsTypeSide (HsHsSide),
  addImports,
  cppTypeToHsTypeAndUse,
  indent,
  ln,
  prettyPrint,
  sayLn,
  saysLn,
  )
import Foreign.Hoppy.Generator.Spec (
  Class,
  ClassHaskellConversion (
    ClassHaskellConversion,
    classHaskellConversionFromCppFn,
    classHaskellConversionToCppFn,
    classHaskellConversionType
    ),
  Constness (Const, Nonconst),
  Operator (OpAdd, OpArray),
  Reqs,
  Type,
  addReqs,
  addAddendumHaskell,
  classSetEntityPrefix,
  classSetHaskellConversion,
  classSetMonomorphicSuperclass,
  hsImport1,
  identT,
  includeStd,
  makeClass,
  mkConstMethod,
  mkConstMethod',
  mkCtor,
  mkMethod,
  mkMethod',
  np,
  reqInclude,
  toExtName,
  )
import Foreign.Hoppy.Generator.Spec.Class (
  toHsCastMethodName,
  toHsClassEntityName',
  toHsDataTypeName,
  )
import Foreign.Hoppy.Generator.Spec.ClassFeature (
  ClassFeature (Assignable, Copyable),
  classAddFeatures,
  )
import Foreign.Hoppy.Generator.Types (boolT, constT, intT, objT, ptrT, refT, toGcT, voidT)
import Foreign.Hoppy.Generator.Version (collect, just, test)
import Graphics.UI.Qtah.Generator.Config (qtVersion)
import Graphics.UI.Qtah.Generator.Interface.Core.QByteArray (c_QByteArray)
import Graphics.UI.Qtah.Generator.Interface.Core.QItemSelectionRange (c_QItemSelectionRange)
import Graphics.UI.Qtah.Generator.Interface.Core.QModelIndex (c_QModelIndex)
import Graphics.UI.Qtah.Generator.Interface.Core.QObject (c_QObject)
import Graphics.UI.Qtah.Generator.Interface.Core.QSize (c_QSize)
import {-# SOURCE #-} Graphics.UI.Qtah.Generator.Interface.Widgets.QTreeWidgetItem (c_QTreeWidgetItem)
import Graphics.UI.Qtah.Generator.Interface.Core.QString (c_QString)
import {-# SOURCE #-} Graphics.UI.Qtah.Generator.Interface.Core.QVariant (c_QVariant)
import Graphics.UI.Qtah.Generator.Interface.Imports
import {-# SOURCE #-} Graphics.UI.Qtah.Generator.Interface.Widgets.QAbstractButton (
  c_QAbstractButton,
  )
import {-# SOURCE #-} Graphics.UI.Qtah.Generator.Interface.Widgets.QWidget (c_QWidget)
import Graphics.UI.Qtah.Generator.Module (AModule (AQtModule), QtModule, makeQtModule)
import Graphics.UI.Qtah.Generator.Types
import Language.Haskell.Syntax (
  HsQName (Special),
  HsSpecialCon (HsListCon),
  HsType (HsTyApp, HsTyCon),
  )

{-# ANN module "HLint: ignore Use camelCase" #-}

-- | Options for instantiating the list classes.
newtype Options = Options
  { Options -> [ClassFeature]
optListClassFeatures :: [ClassFeature]
    -- ^ Additional features to add to the @QList@ class.  Lists are always
    -- 'Assignable' and 'Copyable', but you may want to add 'Equatable' if your
    -- value type supports it.
  }

-- | The default options have no additional 'ClassFeature's.
defaultOptions :: Options
defaultOptions :: Options
defaultOptions = [ClassFeature] -> Options
Options []

-- | A set of instantiated classes.
newtype Contents = Contents
  { Contents -> Class
c_QList :: Class  -- ^ @QList\<T>@
  }

-- | @instantiate className t tReqs@ creates a set of bindings for an
-- instantiation of @QList@ and associated types (e.g. iterators).  In the
-- result, the 'c_QList' class has an external name of @className@.
instantiate :: String -> Type -> Reqs -> Contents
instantiate :: String -> Type -> Reqs -> Contents
instantiate String
listName Type
t Reqs
tReqs = String -> Type -> Reqs -> Options -> Contents
instantiate' String
listName Type
t Reqs
tReqs Options
defaultOptions

-- | 'instantiate' with additional options.
instantiate' :: String -> Type -> Reqs -> Options -> Contents
instantiate' :: String -> Type -> Reqs -> Options -> Contents
instantiate' String
listName Type
t Reqs
tReqs Options
opts =
  let reqs :: Reqs
reqs = [Reqs] -> Reqs
forall a. Monoid a => [a] -> a
mconcat [ Reqs
tReqs
                     , Include -> Reqs
reqInclude (Include -> Reqs) -> Include -> Reqs
forall a b. (a -> b) -> a -> b
$ String -> Include
includeStd String
"QList"
                     ]
      features :: [ClassFeature]
features = ClassFeature
Assignable ClassFeature -> [ClassFeature] -> [ClassFeature]
forall a. a -> [a] -> [a]
: ClassFeature
Copyable ClassFeature -> [ClassFeature] -> [ClassFeature]
forall a. a -> [a] -> [a]
: Options -> [ClassFeature]
optListClassFeatures Options
opts
      hasReserve :: Bool
hasReserve = Version
qtVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int
4, Int
7]

      list :: Class
list =
        Reqs -> Class -> Class
forall a. HasReqs a => Reqs -> a -> a
addReqs Reqs
reqs (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        Generator () -> Class -> Class
forall a. HasAddendum a => Generator () -> a -> a
addAddendumHaskell Generator ()
addendum (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        ClassHaskellConversion -> Class -> Class
classSetHaskellConversion ClassHaskellConversion
conversion (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        [ClassFeature] -> Class -> Class
classAddFeatures [ClassFeature]
features (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        Class -> Class
classSetMonomorphicSuperclass (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        String -> Class -> Class
classSetEntityPrefix String
"" (Class -> Class) -> Class -> Class
forall a b. (a -> b) -> a -> b
$
        Identifier -> Maybe ExtName -> [Class] -> [ClassEntity] -> Class
makeClass (String -> [Type] -> Identifier
identT String
"QList" [Type
t]) (ExtName -> Maybe ExtName
forall a. a -> Maybe a
Just (ExtName -> Maybe ExtName) -> ExtName -> Maybe ExtName
forall a b. (a -> b) -> a -> b
$ HasCallStack => String -> ExtName
String -> ExtName
toExtName String
listName) [] ([ClassEntity] -> Class) -> [ClassEntity] -> Class
forall a b. (a -> b) -> a -> b
$ [Filtered ClassEntity] -> [ClassEntity]
forall a. [Filtered a] -> [a]
collect
        [ ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> ClassEntity
forall p. IsParameter p => String -> [p] -> ClassEntity
mkCtor String
"new" [Parameter]
np
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkMethod' String
"append" String
"append" [Type
t] Type
voidT
        , Bool -> ClassEntity -> Filtered ClassEntity
forall a. Bool -> a -> Filtered a
test (Version
qtVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int
4, Int
5]) (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkMethod' String
"append" String
"appendList" [Class -> Type
objT Class
list] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ Operator -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkMethod' Operator
OpArray String
"at" [Type
intT] (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"at" String
"atConst" [Type
intT] (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Type -> Type
constT Type
t
          -- OMIT back
          -- OMIT begin
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"clear" [Parameter]
np Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"contains" [Type
t] Type
boolT
          -- OMIT count()
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"count" [Type
t] Type
intT
        , Bool -> ClassEntity -> Filtered ClassEntity
forall a. Bool -> a -> Filtered a
test (Version
qtVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int
4, Int
5]) (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"endsWith" [Type
t] Type
boolT
          -- OMIT erase
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkMethod' String
"first" String
"first" [Parameter]
np (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"first" String
"firstConst" [Parameter]
np (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Type -> Type
constT Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ Operator -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' Operator
OpArray String
"get" [Type
intT] Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"indexOf" String
"indexOf" [Type
t] Type
intT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"indexOf" String
"indexOfFrom" [Type
t, Type
intT] Type
intT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"insert" [Type
intT, Type
t] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"isEmpty" [Parameter]
np Type
boolT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkMethod' String
"last" String
"last" [Parameter]
np (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"last" String
"lastConst" [Parameter]
np (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
refT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Type -> Type
constT Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"lastIndexOf" String
"lastIndexOf" [Type
t] Type
intT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"lastIndexOf" String
"lastIndexOfFrom" [Type
t, Type
intT] Type
intT
          -- OMIT length
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"mid" String
"mid" [Type
intT] (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
toGcT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
list
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"mid" String
"midLength" [Type
intT, Type
intT] (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
toGcT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
list
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"move" [Type
intT, Type
intT] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"prepend" [Type
t] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"removeAll" [Type
t] Type
intT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"removeAt" [Type
intT] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"removeFirst" [Parameter]
np Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"removeLast" [Parameter]
np Type
voidT
        , Bool -> ClassEntity -> Filtered ClassEntity
forall a. Bool -> a -> Filtered a
test (Version
qtVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int
4, Int
4]) (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"removeOne" [Type
t] Type
boolT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"replace" [Type
intT, Type
t] Type
voidT
        , Bool -> ClassEntity -> Filtered ClassEntity
forall a. Bool -> a -> Filtered a
test Bool
hasReserve (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"reserve" [Type
intT] Type
voidT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"size" [Parameter]
np Type
intT
        , Bool -> ClassEntity -> Filtered ClassEntity
forall a. Bool -> a -> Filtered a
test (Version
qtVersion Version -> Version -> Bool
forall a. Ord a => a -> a -> Bool
>= [Int
4, Int
5]) (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod String
"startsWith" [Type
t] Type
boolT
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"swap" [Type
intT, Type
intT] Type
voidT
          -- OMIT swap(QList<T>&)
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"takeAt" [Type
intT] Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"takeFirst" [Parameter]
np Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> [Parameter] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkMethod String
"takeLast" [Parameter]
np Type
t
          -- TODO toSet
          -- TODO toStdList
          -- TODO toVector
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"value" String
"value" [Type
intT] Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ String -> String -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> String -> [p] -> Type -> ClassEntity
mkConstMethod' String
"value" String
"valueOr" [Type
intT, Type
t] Type
t
        , ClassEntity -> Filtered ClassEntity
forall a. a -> Maybe a
just (ClassEntity -> Filtered ClassEntity)
-> ClassEntity -> Filtered ClassEntity
forall a b. (a -> b) -> a -> b
$ Operator -> [Type] -> Type -> ClassEntity
forall name p.
(IsFnName String name, IsParameter p) =>
name -> [p] -> Type -> ClassEntity
mkConstMethod Operator
OpAdd [Class -> Type
objT Class
list] (Type -> ClassEntity) -> Type -> ClassEntity
forall a b. (a -> b) -> a -> b
$ Type -> Type
toGcT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
list
        ]

      -- The addendum for the list class contains HasContents and FromContents
      -- instances.
      addendum :: Generator ()
addendum = do
        HsImportSet -> Generator ()
addImports (HsImportSet -> Generator ()) -> HsImportSet -> Generator ()
forall a b. (a -> b) -> a -> b
$ [HsImportSet] -> HsImportSet
forall a. Monoid a => [a] -> a
mconcat [String -> String -> HsImportSet
hsImport1 String
"Prelude" String
"(-)",
                              String -> String -> HsImportSet
hsImport1 String
"Control.Monad" String
"(<=<)",
                              HsImportSet
importForPrelude,
                              HsImportSet
importForRuntime]
        Bool -> Generator () -> Generator ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hasReserve (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$
          HsImportSet -> Generator ()
addImports (HsImportSet -> Generator ()) -> HsImportSet -> Generator ()
forall a b. (a -> b) -> a -> b
$ String -> String -> HsImportSet
hsImport1 String
"Prelude" String
"($)"

        [Constness] -> (Constness -> Generator ()) -> Generator ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Constness
Const, Constness
Nonconst] ((Constness -> Generator ()) -> Generator ())
-> (Constness -> Generator ()) -> Generator ()
forall a b. (a -> b) -> a -> b
$ \Constness
cst -> do
          String
hsDataTypeName <- Constness -> Class -> Generator String
toHsDataTypeName Constness
cst Class
list
          HsType
hsValueType <- HsTypeSide -> Type -> Generator HsType
cppTypeToHsTypeAndUse HsTypeSide
HsHsSide (Type -> Generator HsType) -> Type -> Generator HsType
forall a b. (a -> b) -> a -> b
$ case Constness
cst of
            Constness
Const -> Type -> Type
constT Type
t
            Constness
Nonconst -> Type
t

          -- Generate const and nonconst HasContents instances.
          Generator ()
ln
          [String] -> Generator ()
saysLn [String
"instance QtahFHR.HasContents ", String
hsDataTypeName,
                  String
" (", HsType -> String
forall a. Pretty a => a -> String
prettyPrint HsType
hsValueType, String
") where"]
          Generator () -> Generator ()
forall a. Generator a -> Generator a
indent (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ do
            String -> Generator ()
sayLn String
"toContents this' = do"
            Generator () -> Generator ()
forall a. Generator a -> Generator a
indent (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ do
              let listAt :: String
listAt = case Constness
cst of
                    Constness
Const -> String
"atConst"
                    Constness
Nonconst -> String
"at"
              [String] -> Generator ()
saysLn [String
"size' <- ", Class -> String -> String
forall name. IsFnName String name => Class -> name -> String
toHsClassEntityName' Class
list String
"size", String
" this'"]
              [String] -> Generator ()
saysLn [String
"QtahP.mapM (QtahFHR.decode <=< ",
                      Class -> String -> String
forall name. IsFnName String name => Class -> name -> String
toHsClassEntityName' Class
list String
listAt, String
" this') [0..size'-1]"]

          -- Only generate a nonconst FromContents instance.
          Bool -> Generator () -> Generator ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Constness
cst Constness -> Constness -> Bool
forall a. Eq a => a -> a -> Bool
== Constness
Nonconst) (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ do
            Generator ()
ln
            [String] -> Generator ()
saysLn [String
"instance QtahFHR.FromContents ", String
hsDataTypeName,
                    String
" (", HsType -> String
forall a. Pretty a => a -> String
prettyPrint HsType
hsValueType, String
") where"]
            Generator () -> Generator ()
forall a. Generator a -> Generator a
indent (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ do
              String -> Generator ()
sayLn String
"fromContents values' = do"
              Generator () -> Generator ()
forall a. Generator a -> Generator a
indent (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ do
                [String] -> Generator ()
saysLn [String
"list' <- ", Class -> String -> String
forall name. IsFnName String name => Class -> name -> String
toHsClassEntityName' Class
list String
"new"]
                Bool -> Generator () -> Generator ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hasReserve (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$
                  [String] -> Generator ()
saysLn [Class -> String -> String
forall name. IsFnName String name => Class -> name -> String
toHsClassEntityName' Class
list String
"reserve",
                          String
" list' $ QtahFHR.coerceIntegral $ QtahP.length values'"]
                [String] -> Generator ()
saysLn [String
"QtahP.mapM_ (", Class -> String -> String
forall name. IsFnName String name => Class -> name -> String
toHsClassEntityName' Class
list String
"append", String
" list') values'"]
                String -> Generator ()
sayLn String
"QtahP.return list'"

      -- A QList of some type converts into a Haskell list of the corresponding
      -- type using HasContents/FromContents.
      conversion :: ClassHaskellConversion
conversion =
        ClassHaskellConversion :: Maybe (Generator HsType)
-> Maybe (Generator ())
-> Maybe (Generator ())
-> ClassHaskellConversion
ClassHaskellConversion
        { classHaskellConversionType :: Maybe (Generator HsType)
classHaskellConversionType =
          Generator HsType -> Maybe (Generator HsType)
forall a. a -> Maybe a
Just (Generator HsType -> Maybe (Generator HsType))
-> Generator HsType -> Maybe (Generator HsType)
forall a b. (a -> b) -> a -> b
$ HsType -> HsType -> HsType
HsTyApp (HsQName -> HsType
HsTyCon (HsQName -> HsType) -> HsQName -> HsType
forall a b. (a -> b) -> a -> b
$ HsSpecialCon -> HsQName
Special HsSpecialCon
HsListCon) (HsType -> HsType) -> Generator HsType -> Generator HsType
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HsTypeSide -> Type -> Generator HsType
cppTypeToHsTypeAndUse HsTypeSide
HsHsSide Type
t
        , classHaskellConversionToCppFn :: Maybe (Generator ())
classHaskellConversionToCppFn = Generator () -> Maybe (Generator ())
forall a. a -> Maybe a
Just (Generator () -> Maybe (Generator ()))
-> Generator () -> Maybe (Generator ())
forall a b. (a -> b) -> a -> b
$ do
          HsImportSet -> Generator ()
addImports HsImportSet
importForRuntime
          String -> Generator ()
sayLn String
"QtahFHR.fromContents"
        , classHaskellConversionFromCppFn :: Maybe (Generator ())
classHaskellConversionFromCppFn = Generator () -> Maybe (Generator ())
forall a. a -> Maybe a
Just (Generator () -> Maybe (Generator ()))
-> Generator () -> Maybe (Generator ())
forall a b. (a -> b) -> a -> b
$ do
          HsImportSet -> Generator ()
addImports HsImportSet
importForRuntime
          String -> Generator ()
sayLn String
"QtahFHR.toContents"
        }

  in Contents :: Class -> Contents
Contents
     { c_QList :: Class
c_QList = Class
list
     }

-- | Used in an addendum for a class that inherits from QList, this generates a
-- HasContents instance that uses the HasContents instance on the parent list
-- class.
inheritHasContents :: Class -> Class -> Type -> Generator ()
inheritHasContents :: Class -> Class -> Type -> Generator ()
inheritHasContents Class
cls Class
listClass Type
t =
  [Constness] -> (Constness -> Generator ()) -> Generator ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Constness
Const, Constness
Nonconst] ((Constness -> Generator ()) -> Generator ())
-> (Constness -> Generator ()) -> Generator ()
forall a b. (a -> b) -> a -> b
$ \Constness
cst -> do
    String
hsDataTypeName <- Constness -> Class -> Generator String
toHsDataTypeName Constness
cst Class
cls
    HsType
hsValueType <- HsTypeSide -> Type -> Generator HsType
cppTypeToHsTypeAndUse HsTypeSide
HsHsSide (Type -> Generator HsType) -> Type -> Generator HsType
forall a b. (a -> b) -> a -> b
$ case Constness
cst of
      Constness
Const -> Type -> Type
constT Type
t
      Constness
Nonconst -> Type
t
    String
castToListFn <- Constness -> Class -> Generator String
toHsCastMethodName Constness
cst Class
listClass
    HsImportSet -> Generator ()
addImports HsImportSet
importForRuntime
    [String] -> Generator ()
saysLn [String
"instance QtahFHR.HasContents ", String
hsDataTypeName,
            String
" (", HsType -> String
forall a. Pretty a => a -> String
prettyPrint HsType
hsValueType, String
") where"]
    Generator () -> Generator ()
forall a. Generator a -> Generator a
indent (Generator () -> Generator ()) -> Generator () -> Generator ()
forall a b. (a -> b) -> a -> b
$ [String] -> Generator ()
saysLn [String
"toContents = QtahFHR.toContents . ", String
castToListFn]

-- | Converts an instantiation into a list of exports to be included in a
-- module.
toExports :: Contents -> [QtExport]
toExports :: Contents -> [QtExport]
toExports Contents
m = [Class -> QtExport
forall a. Exportable a => a -> QtExport
qtExport (Class -> QtExport) -> Class -> QtExport
forall a b. (a -> b) -> a -> b
$ Contents -> Class
c_QList Contents
m]

createModule :: String -> Contents -> QtModule
createModule :: String -> Contents -> QtModule
createModule String
name Contents
contents = [String] -> [QtExport] -> QtModule
makeQtModule [String
"Core", String
"QList", String
name] ([QtExport] -> QtModule) -> [QtExport] -> QtModule
forall a b. (a -> b) -> a -> b
$ Contents -> [QtExport]
toExports Contents
contents

allModules :: [AModule]
allModules :: [AModule]
allModules =
  (QtModule -> AModule) -> [QtModule] -> [AModule]
forall a b. (a -> b) -> [a] -> [b]
map QtModule -> AModule
AQtModule
  [ QtModule
qmod_Int
  , QtModule
qmod_QAbstractButton
  , QtModule
qmod_QByteArray
  , QtModule
qmod_QItemSelectionRange
  , QtModule
qmod_QModelIndex
  , QtModule
qmod_QObject
  , QtModule
qmod_QSize
  , QtModule
qmod_QString
  , QtModule
qmod_QVariant
  , QtModule
qmod_QWidget
  , QtModule
qmod_QTreeWidgetItem
  ]

qmod_Int :: QtModule
qmod_Int :: QtModule
qmod_Int = String -> Contents -> QtModule
createModule String
"Int" Contents
contents_Int

contents_Int :: Contents
contents_Int :: Contents
contents_Int =
  String -> Type -> Reqs -> Contents
instantiate String
"QListInt" Type
intT Reqs
forall a. Monoid a => a
mempty

c_QListInt :: Class
c_QListInt :: Class
c_QListInt = Contents -> Class
c_QList Contents
contents_Int

qmod_QAbstractButton :: QtModule
qmod_QAbstractButton :: QtModule
qmod_QAbstractButton = String -> Contents -> QtModule
createModule String
"QAbstractButton" Contents
contents_QAbstractButton

contents_QAbstractButton :: Contents
contents_QAbstractButton :: Contents
contents_QAbstractButton = String -> Type -> Reqs -> Contents
instantiate String
"QListQAbstractButton" (Type -> Type
ptrT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
c_QAbstractButton) Reqs
forall a. Monoid a => a
mempty

c_QListQAbstractButton :: Class
c_QListQAbstractButton :: Class
c_QListQAbstractButton = Contents -> Class
c_QList Contents
contents_QAbstractButton

qmod_QByteArray :: QtModule
qmod_QByteArray :: QtModule
qmod_QByteArray = String -> Contents -> QtModule
createModule String
"QByteArray" Contents
contents_QByteArray

contents_QByteArray :: Contents
contents_QByteArray :: Contents
contents_QByteArray = String -> Type -> Reqs -> Contents
instantiate String
"QListQByteArray" (Class -> Type
objT Class
c_QByteArray) Reqs
forall a. Monoid a => a
mempty

c_QListQByteArray :: Class
c_QListQByteArray :: Class
c_QListQByteArray = Contents -> Class
c_QList Contents
contents_QByteArray

qmod_QItemSelectionRange :: QtModule
qmod_QItemSelectionRange :: QtModule
qmod_QItemSelectionRange = String -> Contents -> QtModule
createModule String
"QItemSelectionRange" Contents
contents_QItemSelectionRange

contents_QItemSelectionRange :: Contents
contents_QItemSelectionRange :: Contents
contents_QItemSelectionRange =
  String -> Type -> Reqs -> Contents
instantiate String
"QListQItemSelectionRange" (Class -> Type
objT Class
c_QItemSelectionRange) Reqs
forall a. Monoid a => a
mempty

c_QListQItemSelectionRange :: Class
c_QListQItemSelectionRange :: Class
c_QListQItemSelectionRange = Contents -> Class
c_QList Contents
contents_QItemSelectionRange

qmod_QModelIndex :: QtModule
qmod_QModelIndex :: QtModule
qmod_QModelIndex = String -> Contents -> QtModule
createModule String
"QModelIndex" Contents
contents_QModelIndex

contents_QModelIndex :: Contents
contents_QModelIndex :: Contents
contents_QModelIndex = String -> Type -> Reqs -> Contents
instantiate String
"QListQModelIndex" (Class -> Type
objT Class
c_QModelIndex) Reqs
forall a. Monoid a => a
mempty

c_QListQModelIndex :: Class
c_QListQModelIndex :: Class
c_QListQModelIndex = Contents -> Class
c_QList Contents
contents_QModelIndex

qmod_QObject :: QtModule
qmod_QObject :: QtModule
qmod_QObject = String -> Contents -> QtModule
createModule String
"QObject" Contents
contents_QObject

contents_QObject :: Contents
contents_QObject :: Contents
contents_QObject = String -> Type -> Reqs -> Contents
instantiate String
"QListQObject" (Type -> Type
ptrT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
c_QObject) Reqs
forall a. Monoid a => a
mempty

c_QListQObject :: Class
c_QListQObject :: Class
c_QListQObject = Contents -> Class
c_QList Contents
contents_QObject

qmod_QSize :: QtModule
qmod_QSize :: QtModule
qmod_QSize = String -> Contents -> QtModule
createModule String
"QSize" Contents
contents_QSize

contents_QSize :: Contents
contents_QSize :: Contents
contents_QSize = String -> Type -> Reqs -> Contents
instantiate String
"QListQSize" (Class -> Type
objT Class
c_QSize) Reqs
forall a. Monoid a => a
mempty

c_QListQSize :: Class
c_QListQSize :: Class
c_QListQSize = Contents -> Class
c_QList Contents
contents_QSize

qmod_QString :: QtModule
qmod_QString :: QtModule
qmod_QString = String -> Contents -> QtModule
createModule String
"QString" Contents
contents_QString

contents_QString :: Contents
contents_QString :: Contents
contents_QString = String -> Type -> Reqs -> Contents
instantiate String
"QListQString" (Class -> Type
objT Class
c_QString) Reqs
forall a. Monoid a => a
mempty

c_QListQString :: Class
c_QListQString :: Class
c_QListQString = Contents -> Class
c_QList Contents
contents_QString

qmod_QVariant :: QtModule
qmod_QVariant :: QtModule
qmod_QVariant = String -> Contents -> QtModule
createModule String
"QVariant" Contents
contents_QVariant

contents_QVariant :: Contents
contents_QVariant :: Contents
contents_QVariant = String -> Type -> Reqs -> Contents
instantiate String
"QListQVariant" (Class -> Type
objT Class
c_QVariant) Reqs
forall a. Monoid a => a
mempty

c_QListQVariant :: Class
c_QListQVariant :: Class
c_QListQVariant = Contents -> Class
c_QList Contents
contents_QVariant

qmod_QWidget :: QtModule
qmod_QWidget :: QtModule
qmod_QWidget = String -> Contents -> QtModule
createModule String
"QWidget" Contents
contents_QWidget

contents_QWidget :: Contents
contents_QWidget :: Contents
contents_QWidget = String -> Type -> Reqs -> Contents
instantiate String
"QListQWidget" (Type -> Type
ptrT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
c_QWidget) Reqs
forall a. Monoid a => a
mempty

c_QListQWidget :: Class
c_QListQWidget :: Class
c_QListQWidget = Contents -> Class
c_QList Contents
contents_QWidget

qmod_QTreeWidgetItem :: QtModule
qmod_QTreeWidgetItem :: QtModule
qmod_QTreeWidgetItem = String -> Contents -> QtModule
createModule String
"QTreeWidgetItem" Contents
contents_QTreeWidgetItem

contents_QTreeWidgetItem :: Contents
contents_QTreeWidgetItem :: Contents
contents_QTreeWidgetItem = String -> Type -> Reqs -> Contents
instantiate String
"QListQTreeWidgetItem" (Type -> Type
ptrT (Type -> Type) -> Type -> Type
forall a b. (a -> b) -> a -> b
$ Class -> Type
objT Class
c_QTreeWidgetItem) Reqs
forall a. Monoid a => a
mempty

c_QListQTreeWidgetItem :: Class
c_QListQTreeWidgetItem :: Class
c_QListQTreeWidgetItem = Contents -> Class
c_QList Contents
contents_QTreeWidgetItem