~ehmry/nim-tkrzw

bedb257a8a45b12fc42f21d27bc535554564f14d — Emery Hemingway 3 years ago db727b5
Add newDbm[T](…) to API

This allows dbm instances to be created by passing around the
database type rather than a proc for creating some type.
2 files changed, 16 insertions(+), 13 deletions(-)

M src/tkrzw.nim
M tests/test_tkrzw.nim
M src/tkrzw.nim => src/tkrzw.nim +4 -1
@@ 125,12 125,15 @@ proc newDbm*[T: DBM](path: string; rw: RW; options: set[OpenOption] = {}): T =
  result = construct[T]()
  result.Open(path, rw == writeable, opts)

proc newDbm*[T: MemoryDBM](): T =
  construct[T]()

template declareNewFile(T: untyped): untyped =
  proc `new T`*(path: string; rw: RW; options: set[OpenOption] = {}): T =
    newDBM[T](path, rw, options)

template declareNewMem(T: untyped): untyped =
  proc `new T`*(): T = construct[T]()
  proc `new T`*(): T = newDBM[T]()

declareNewFile(HashDBM)
declareNewFile(TreeDBM)

M tests/test_tkrzw.nim => tests/test_tkrzw.nim +12 -12
@@ 2,14 2,14 @@ import std/[os, unittest]

import tkrzw

template testEx1(newProc: untyped; isFile: static[bool]): untyped =
template testEx1(T: untyped; isFile: static[bool]): untyped =
  test "ex1":
    const path = "casket.test"
    when isFile:
      var dbm = newProc(path, writeable, {ooTruncate})
        # newHashDBM(…), etc
    else:
      var dbm = newProc()
    var dbm =
      when isFile:
        newDbm[T](path, writeable, {ooTruncate})
      else:
        newDbm[T]()

    dbm["foo"] = "hop"
    dbm["bar"] = "step"


@@ 34,19 34,19 @@ template testEx1(newProc: untyped; isFile: static[bool]): untyped =
    removeFile(path)

suite "HashDBM":
  testEx1(newHashDBM, true)
  testEx1(HashDBM, true)

suite "TreeDBM":
  testEx1(newTreeDBM, true)
  testEx1(TreeDBM, true)

suite "SkipDBM":
  testEx1(newSkipDBM, true)
  testEx1(SkipDBM, true)

suite "TinyDBM":
  testEx1(newTinyDBM, false)
  testEx1(TinyDBM, false)

suite "BabyDBM":
  testEx1(newBabyDBM, false)
  testEx1(BabyDBM, false)

suite "CacheDBM":
  testEx1(newCacheDBM, false)
  testEx1(CacheDBM, false)