File: tstaticimportcpp.nim

package info (click to toggle)
nim 2.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,951,164 kB
  • sloc: sh: 24,599; ansic: 1,771; python: 1,493; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (63 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download
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
61
62
63
discard """
targets: "cpp"
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]\n4"
"""

{.emit: """/*TYPESECTION*/

template <int N, class T>
struct GenericIntType {
  T data[N];
};

template <class T>
struct GenericTType {
  T field;
};

struct SimpleStruct {
  int field;
};


""" .}

type
  GenericIntType[N: static[int]; T] {.importcpp: "GenericIntType<'0, '1>".} = object
    data: array[N, T]

  GenericIntTypeAlt[N: static[int]; T] {.importcpp: "GenericIntType".} = object

  GenericTType[T] {.importcpp: "GenericTType<'0>".} = object
    field: T

  GenInt4 = GenericIntType[4, int]

  SimpleStruct {.importcpp: "SimpleStruct"} = object
    field: int

var
  a = GenInt4()
  b = SimpleStruct()
  c = GenericTType[float]()
  d = SimpleStruct(field: 15)
  e = GenericTType[string](field: "test")
  f = GenericIntTypeAlt[4, int8]()

a.data[2] = 10
b.field = 5
c.field = 1.2

echo a.data
echo b.field
echo c.field
echo d.field
echo e.field

proc plus(a, b: GenInt4): GenInt4 =
  for i in 0 ..< result.data.len:
    result.data[i] = a.data[i] + b.data[i]

echo plus(a, a).data

echo sizeof(f)