summary refs log tree commit diff stats
path: root/tests/destructor/ttuple.nim
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2020-01-04 01:33:13 +0000
committerClyybber <darkmine956@gmail.com>2020-01-04 02:33:13 +0100
commit47e7b8771c76deb1eb0827b7613a77b4bbacf019 (patch)
tree98510872597efd3fceba1fac4b42076cd80bffee /tests/destructor/ttuple.nim
parent89570aa44b3a90ed00cca4c31b62503d013719ce (diff)
downloadNim-47e7b8771c76deb1eb0827b7613a77b4bbacf019.tar.gz
Fixes #13026 (#13028)
Diffstat (limited to 'tests/destructor/ttuple.nim')
-rw-r--r--tests/destructor/ttuple.nim84
1 files changed, 83 insertions, 1 deletions
diff --git a/tests/destructor/ttuple.nim b/tests/destructor/ttuple.nim
index ec12dfc3a..5a2126105 100644
--- a/tests/destructor/ttuple.nim
+++ b/tests/destructor/ttuple.nim
@@ -1,6 +1,9 @@
 
 discard """
-   output: '''5.0 10.0'''
+   output: '''5.0 10.0
+=destroy
+=destroy   
+'''
 """
 
 type
@@ -46,3 +49,82 @@ let h = MyOpt[float](has: true, val: 5.0)
 myproc(h)
 
 
+#-------------------------------------------------------------
+type
+  MyObject* = object
+    len*: int
+    amount: UncheckedArray[float]
+
+  MyObjPtr* = ptr MyObject
+
+  MyObjContainer* {.byref.} = object
+    size1: int
+    size2: int
+    data: ptr UncheckedArray[MyObjPtr]
+
+ 
+proc size1*(m: MyObjContainer): int {.inline.} = m.size1
+proc size2*(m: MyObjContainer): int {.inline.} = m.size2
+
+proc allocateMyObjPtr(size2: int): MyObjPtr =
+  cast[MyObjPtr](allocShared(sizeof(MyObject) + sizeof(float) * size2.int))
+
+proc `=destroy`*(m: var MyObjContainer) {.inline.} =
+  if m.data != nil:
+    for i in 0..<m.size1:
+      if m.data[i] != nil:
+        deallocShared(m.data[i])
+        m.data[i] = nil
+    deallocShared(m.data)
+    echo "=destroy"
+    m.data = nil
+
+proc `=sink`*(m: var MyObjContainer, m2: MyObjContainer) {.inline.} =
+  if m.data != m2.data:
+    `=destroy`(m)
+  m.size1 = m2.size1
+  m.size2 = m2.size2  
+  m.data = m2.data
+
+
+proc `=`*(m: var MyObjContainer, m2: MyObjContainer) {.error.}
+  ## non copyable
+
+func newMyObjContainer*(size2: Natural): MyObjContainer =
+  result.size2 = size2
+
+proc push(m: var MyObjContainer, cf: MyObjPtr) =
+  ## Add MyObjPtr to MyObjContainer, shallow copy
+  m.size1.inc
+  m.data = cast[ptr UncheckedArray[MyObjPtr]](reallocShared(m.data, m.size1 * sizeof(MyObjPtr)))
+  m.data[m.size1 - 1] = cf
+
+ 
+proc add*(m: var MyObjContainer, amount: float) =
+  assert m.size2 > 0, "MyObjContainer is not initialized, use newMyObjContainer() to initialize object before use"
+  let cf = allocateMyObjPtr(m.size2)
+  for i in 0..<m.size2:
+    cf.amount[i.int] = amount
+
+  m.push(cf)
+
+proc add*(dest: var MyObjContainer, src: sink MyObjContainer) =
+  # merge containers
+
+  for i in 0..<src.size1:
+    dest.push src.data[i]
+    src.data[i] = nil
+
+ 
+proc test = 
+  var cf1 = newMyObjContainer(100)
+  cf1.add(1)
+  cf1.add(2)
+
+  var cf3 = newMyObjContainer(100)
+  cf3.add(2)
+  cf3.add(3)
+
+  cf1.add(cf3)
+
+test()