n0bra1n3r / cinterop Goto Github PK
View Code? Open in Web Editor NEWA C/C++ interop library for the Nim programming language
A C/C++ interop library for the Nim programming language
I am playing with OCCUtils. It depends on OpenCascade library.
I wrote the following example:
{.passL: "-loccutils".}
{.passC:"-I/usr/local/include/occutils -I/usr/include/opencascade" .}
import cinterop
# From: /usr/include/opencascade
type
TopoDS_Shape* {.importcpp: "TopoDS_Shape", header: "TopoDS_Shape.hxx", bycopy.} = object of RootObj
TopoDS_Solid* {.importcpp: "TopoDS_Solid", header: "TopoDS_Solid.hxx", bycopy.} = object of TopoDS_Shape
# From: /usr/local/include/occutils
csource "Primitive.hxx":
cnamespace OCCUtils:
cnamespace Primitive:
type primitive* {.cgen:"OCCUtils::Primitive::$1(@)".} = object of CClass # OCCUtils::Primitive::$1(@)
proc makeCube*(size:float):TopoDS_Solid =
cexpr[TopoDS_Solid]^primitive.MakeCube(size)
proc main() =
var myCube:TopoDS_Solid = makeCube(5)
main()
Which gives me the following error:
error: ‘class TopoDS_Solid’ has no member named ‘m_type’
89 | result.m_type = (&NTI__iFNg5Zv78nzJlY9cUWwvWwg_);
The c++ created contains:
...
/* section: NIM_merge_PROCS */
N_LIB_PRIVATE N_NIMCALL(TopoDS_Solid, makeCube__0MVTOneNhGZEGBJK1P9cGWQ)(NF size) {
TopoDS_Solid result;
result.m_type = (&NTI__iFNg5Zv78nzJlY9cUWwvWwg_); //<------ ERROR
result = /*OCCUtils::Primitive::primitive*/OCCUtils::Primitive::MakeCube(size);
return result;
}
...
As a side note, I have tried this {.cgen:"OCCUtils::Primitive::$1(@)".}
and {.cgen:"$1(@)".}. In the second case, shouldn't ÒCCUtils
and Primitive
be added given the cnamescape
? Or maybe cnamescape
shouldn't be used if I am using cgen
?
Adding some notes about CEnum in the README.
I was playing with OpenSceneGraph. I wrote the following code:
{.passL: "-losg -losgViewer",
passC:"-I/usr/include/osg" }
import cinterop
csource "Vec3f":
cnamespace osg:
type
Vec3f* = object of CClass
proc newVec3[X,Y,Z:SomeNumber](x:X, y:Y, z:Z):Vec3f =
Vec3f.init(x.cfloat, y.cfloat, z.cfloat)
proc x*(this:Vec3f):cfloat =
cexpr[cfloat]^this.x()
var p = newVec3(1, 2.0, 3.0)
echo p.x()
This code compiles, but I get a warning:
Warning: did not generate C/C++ expression; you may not need `cexpr` here
The code compiles, but produces a runtime error:
Error: call depth limit reached in a debug build (2000 function calls). You can change it with -d:nimCallDepthLimit=<int> but really try to avoid deep recursions instead.
Error: execution of an external program failed: '/home/jose/src/osg.nim/prueba2 '
The source code for the header is: Vec3f
cinclude "header1.hpp"
cinclude "header2.hpp"
cnamespace Namespace
type Type1*[T] {.ccls.} = object
proc staticMethod1*(Self)
field1*: int
proc method1*(self: var)
type NestedType1* = object
proc method2*(self)
type ENUM_1* {.cenu.} = enum
a
b
c
proc function1*() {.cfun.}
expands to:
var cinterop_includes_private {.compileTime.} = "#include \"header1.hpp\"\n#include \"header2.hpp\"\n"
var cinterop_namespace_private {.compileTime.} = "Namespace"
type
Type1*[T] {.importcpp:cinterop_namespace_private&"::$1" header:cinterop_includes_private.} = object of CClass
field1*: int
Type1Ext`somepostfix = cinterop_class_ext_private:
proc staticMethod1*(Self) {.importcpp:"'1::$1(@)" header:cinterop_includes_private.}
proc method1*(self: var Self) {.importcpp:"#.$1(@)" header:cinterop_includes_private.}
type
NestedType1`somepostfix {.importcpp:cinterop_namespace_private&"::Type1::$1" header:cinterop_includes_private.} = object
NestedType1Ext`somepostfix = cinterop_class_ext_private:
proc method2*(self) {.importcpp:"#.$1(@)" header:cinterop_includes_private.}
template NestedType1*[T](Parent: Type1[T]): type = NestedType1`somepostfix
type
ENUM_1* = object of CEnum
val: ENUM_1Val`somepostfix
ENUM_1`somepostfix {.importcpp:cinterop_namespace_private&"::$1" header:cinterop_includes_private.} = enum
a
b
c
proc function1*() {.importcpp:cinterop_namespace_private&"::$1(@)" header:cinterop_includes_private.}
types.nim
is useful for projects that don't need the macro magic in the primary modules.
Hi @n0bra1n3r
This project is fantastic 👍👍👍 What are the possibilities to interface to Qt5/6 with it?
I don't have experience with cinterop
, other that compiling the example, so would it be feasable to create an intermediate Qt C++ header/source pair of files and use them in Nim
?
Thanks
I have the following inheritance tree:
where I have the following code:
csource "BRepBuilderAPI_Transform.hxx":
type
BRepBuilderAPI_Transform* = object of CClass
converter toTopoDS_Shape*(self: BRepBuilderAPI_Transform):TopoDS_Shape {.importcpp:"(#)".}
How would you call a method from BRepBuilderAPI_MakeShape
given than Nim doesn't have multiple inheritance?
How should I deal with operators?
For instance, the following operator:
class GC_MakeArcOfCircle : public GC_Root {
...
operator const Handle(Geom_TrimmedCurve)& () const { return Value(); }
...
}
gets wrapped by c2nim as something like:
converter `constopencascade`*(this: GC_MakeArcOfCircle): handle[Geom_TrimmedCurve] {.
noSideEffect, importcpp: "GC_MakeArcOfCircle::operator constopencascade",
header: "GC_MakeArcOfCircle.hxx".}
How would I use operators from cinterop.
How should I work with templates?
I am trying to do this, but using your library.
The type Handle_Geom_TrimmedCurve
is defined here:
Handle_Geom_TrimmedCurve* {.header: "Geom_TrimmedCurve.hxx", importcpp: "Handle_Geom_TrimmedCurve".} = Handle[Geom_TrimmedCurve]
and Handle
is defined here.
So how would I do that with cinterop?
What I am trying to do is here but I get an error about init
.
I am having an issue with the following:
csource "BRepBuilderAPI_Transform.hxx":
type
BRepBuilderAPI_Transform* = object of CClass
converter toTopoDS_Shape*(self: BRepBuilderAPI_Transform):TopoDS_Shape {.importcpp:"(#)".}
The following fails:
var aBRepTrsf:BRepBuilderAPI_Transform = BRepBuilderAPI_Transform.init(aWire, aTrsf, false)
with the C++ generated code:
N_LIB_PRIVATE BRepBuilderAPI_Transform aBRepTrsf__9c0qHVFq9cjuePb4cRv7GJPg;
...
aBRepTrsf__9c0qHVFq9cjuePb4cRv7GJPg = BRepBuilderAPI_Transform(aWire__78rPOEJid3gfRlRSPHyJ2A, aTrsf__mMdicb5vcYPa15dsHtfr9bg, NIM_FALSE);
where the first line is reported as an error:
error: no matching function for call to ‘BRepBuilderAPI_Transform::BRepBuilderAPI_Transform()’
129 | N_LIB_PRIVATE BRepBuilderAPI_Transform aBRepTrsf__9c0qHVFq9cjuePb4cRv7GJPg;
On the other hand, the following works:
var aBRepTrsf:TopoDS_Shape = BRepBuilderAPI_Transform.init(aWire, aTrsf, false)
with the generated C++ code:
N_LIB_PRIVATE TopoDS_Shape aBRepTrsf__9c0qHVFq9cjuePb4cRv7GJPg;
...
aBRepTrsf__9c0qHVFq9cjuePb4cRv7GJPg = (BRepBuilderAPI_Transform(aWire__78rPOEJid3gfRlRSPHyJ2A, aTrsf__mMdicb5vcYPa15dsHtfr9bg, NIM_FALSE));
IMHO, both should work.
What am I missing?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.