summary refs log tree commit diff stats
path: root/tests/cpp/tinitializers.nim
blob: 0199fb96bceeb71b13b255cf552d203515b8e00d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
discard """
  cmd: "nim cpp $file"
"""

{.emit:"""/*TYPESECTION*/
struct CppStruct {
  CppStruct(int x, char* y): x(x), y(y){}
  void doSomething() {}
  int x;
  char* y;
};
""".}
type
  CppStruct {.importcpp, inheritable.} = object
  ChildStruct = object of CppStruct
  HasCppStruct = object
    cppstruct: CppStruct 

proc constructCppStruct(a:cint = 5, b:cstring = "hello"): CppStruct {.importcpp: "CppStruct(@)", constructor.}
proc doSomething(this: CppStruct) {.importcpp.}
proc returnCppStruct(): CppStruct = discard
proc initChildStruct: ChildStruct = ChildStruct() 
proc makeChildStruct(): ChildStruct {.constructor:"""ChildStruct(): CppStruct(5, "10")""".} = discard
proc initHasCppStruct(x: cint): HasCppStruct =
  HasCppStruct(cppstruct: constructCppStruct(x))

proc main =
  var hasCppStruct = initHasCppStruct(2) #generates cppstruct = { 10 } inside the struct
  hasCppStruct.cppstruct.doSomething() 
  discard returnCppStruct() #generates result = { 10 } 
  discard initChildStruct() #generates ChildStruct temp ({}) bypassed with makeChildStruct
  (proc (s:CppStruct) = discard)(CppStruct()) #CppStruct temp ({10})
main()


#Should handle ObjectCalls
{.emit:"""/*TYPESECTION*/
struct Foo {
};
struct Boo {
  Boo(int x, char* y, Foo f): x(x), y(y), foo(f){}
  int x;
  char* y;
  Foo foo;
};
""".}
type
  Foo {.importcpp, inheritable, bycopy.} = object
  Boo {.importcpp, inheritable.} = object
    x: int32
    y: cstring
    foo: Foo

proc makeBoo(a:cint = 10, b:cstring = "hello", foo: Foo = Foo()): Boo {.importcpp, constructor.}

proc main2() = 
  let cppStruct = makeBoo()
  (proc (s:Boo) = discard)(Boo()) 

main2()