cplusplus-th: C++ Foreign Import Generation

[ bsd3, foreign, library ] [ Propose Tags ] [ Report a vulnerability ]

cplusplus-th allows you to foreign import C++ functions that are compatible with the ccall calling convention. It also includes some standard library abstractions.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0.0
Dependencies base (>=4.6 && <4.8), bytestring (>=0.10 && <0.11), containers (>=0.5 && <0.6), process (>=1.2 && <1.3), template-haskell [details]
License BSD-3-Clause
Copyright Copyright (c) 2014, National ICT Australia Limited (NICTA)
Author Maxwell Swadling
Maintainer maxwell.swadling@nicta.com.au
Category Foreign
Home page https://github.com/nicta/cplusplus-th
Uploaded by MaxwellSwadling at 2014-10-15T02:04:01Z
Distributions
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 1278 total (3 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for cplusplus-th-1.0.0.0

[back to package description]

cplusplus-th

cplusplus-th allows you to foreign import C++ functions that are compatible with the ccall calling convention. It also includes some standard library abstractions.

Example

With the following C++ function in the object file cbits/string.o:

namespace haskell {
string* fromCString(char const* x, int length) {
  return new string(x, length);
}
}

We can import it into Haskell with:

cplusplus "haskell::fromCString(char const*, int)" "cbits/string.o"
          [t|CString -> Int -> IO Std__basic_string|]

Building

It works by looking up the symbol in the object file. When using cabal, it is recommended you include the C++ file in your c-sources and compile it in a build hook. For example:

import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.Simple.Program
import Distribution.Simple.Program.Types
import Distribution.Simple.LocalBuildInfo
import Distribution.PackageDescription

cc_flags = ["-stdlib=libc++", "-o", "cbits/string.o", "-c", "cbits/string.cc"]

main :: IO ()
main = defaultMainWithHooks simpleUserHooks {
    buildHook = buildCPlusPlus
  }

buildCPlusPlus :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
buildCPlusPlus pkg buildInfo hooks flags = do
  let verb = fromFlag (buildVerbosity flags)
  clang <- findProgramLocation verb "clang++"
  let clang' = case clang of
                Just x -> x
                Nothing -> error "clang++ not on path"
  runProgram verb (simpleConfiguredProgram "clang++" (FoundOnSystem clang')) cc_flags
  buildHook simpleUserHooks pkg buildInfo hooks flags

Standard Library

Foreign.CPlusPlusStdLib exports the following type class:

class CPlusPlusLand a {- haskell side -} b {- c++ side -} where
  to :: a -> IO b
  from :: b -> IO a

To avoid orphan instances, it implements instances for some numeric types, String and ByteString.

Compatability

  • Static functions are simply the arguments.
  • Static member functions take the object as the first argument.
  • Functions via a vtable are not possible.
  • Inline functions are not possible.
  • instantiating templates is not possible.