summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-04-25 19:55:06 +0200
committerAraq <rumpf_a@web.de>2014-04-25 19:55:06 +0200
commit0049a2a388585ff494fd0ebac780636cbfc808f8 (patch)
tree94c6a406529edfd0a5483161cb3a0142e874f9e3 /lib
parentfab8cee13d305cc157a7332d00a49e1d48577949 (diff)
parent8c6b1b402ea93829596b3add4c71f2d512363484 (diff)
downloadNim-0049a2a388585ff494fd0ebac780636cbfc808f8.tar.gz
Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/collections/tables.nim68
-rw-r--r--lib/pure/os.nim29
-rw-r--r--lib/system.nim9
-rw-r--r--lib/system/excpt.nim2
-rw-r--r--lib/system/inclrtl.nim2
-rw-r--r--lib/wrappers/sqlite3.nim6
6 files changed, 97 insertions, 19 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim
index cd28f9af0..33e558aee 100644
--- a/lib/pure/collections/tables.nim
+++ b/lib/pure/collections/tables.nim
@@ -10,6 +10,47 @@
 ## The ``tables`` module implements an efficient hash table that is
 ## a mapping from keys to values.
 ##
+## If you are using simple standard types like ``int`` or ``string`` for the
+## keys of the table you won't have any problems, but as soon as you try to use
+## a more complex object as a key you will be greeted by a strange compiler
+## error::
+##
+##   Error: type mismatch: got (Person)
+##   but expected one of:
+##   hashes.hash(x: openarray[A]): THash
+##   hashes.hash(x: int): THash
+##   hashes.hash(x: float): THash
+##   …
+##
+## What is happening here is that the types used for table keys require to have
+## a ``hash()`` proc which will convert them to a `THash <hashes.html#THash>`_
+## value, and the compiler is listing all the hash functions it knows. After
+## you add such a proc for your custom type everything will work. See this
+## example:
+##
+## .. code-block:: nimrod
+##   type
+##     Person = object
+##       firstName, lastName: string
+##
+##   proc hash(x: Person): THash =
+##     ## Piggyback on the already available string hash proc.
+##     ##
+##     ## Without this proc nothing works!
+##     result = hash(x.firstName & x.lastName)
+##
+##   var
+##     salaries = initTable[Person, int]()
+##     p1, p2: Person
+##
+##   p1.firstName = "Jon"
+##   p1.lastName = "Ross"
+##   salaries[p1] = 30_000
+##
+##   p2.firstName = "소진"
+##   p2.lastName = "박"
+##   salaries[p2] = 45_000
+##
 ## **Note:** The data types declared here have *value semantics*: This means
 ## that ``=`` performs a copy of the hash table.
 
@@ -526,3 +567,30 @@ proc sort*[A](t: var TCountTable[A]) =
         if j < h: break
     if h == 1: break
 
+when isMainModule:
+  type
+    Person = object
+      firstName, lastName: string
+
+  proc hash(x: Person): THash =
+    ## Piggyback on the already available string hash proc.
+    ##
+    ## Without this proc nothing works!
+    result = hash(x.firstName & x.lastName)
+
+  var
+    salaries = initTable[Person, int]()
+    p1, p2: Person
+  p1.firstName = "Jon"
+  p1.lastName = "Ross"
+  salaries[p1] = 30_000
+  p2.firstName = "소진"
+  p2.lastName = "박"
+  salaries[p2] = 45_000
+  var
+    s2 = initOrderedTable[Person, int]()
+    s3 = initCountTable[Person]()
+  s2[p1] = 30_000
+  s2[p2] = 45_000
+  s3[p1] = 30_000
+  s3[p2] = 45_000
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 00a33db75..e2fc62d77 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -1,7 +1,7 @@
 #
 #
 #            Nimrod's Runtime Library
-#        (c) Copyright 2012 Andreas Rumpf
+#        (c) Copyright 2014 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -1612,6 +1612,20 @@ when defined(linux) or defined(solaris) or defined(bsd) or defined(aix):
       len = readlink(procPath, result, len)
     setLen(result, len)
 
+when not (defined(windows) or defined(macosx)):
+  proc getApplHeuristic(): string =
+    when defined(paramStr):
+      result = string(paramStr(0))
+      # POSIX guaranties that this contains the executable
+      # as it has been executed by the calling process
+      if len(result) > 0 and result[0] != DirSep: # not an absolute path?
+        # iterate over any path in the $PATH environment variable
+        for p in split(string(getEnv("PATH")), {PathSep}):
+          var x = joinPath(p, result)
+          if existsFile(x): return x
+    else:
+      result = ""
+
 when defined(macosx):
   type
     cuint32* {.importc: "unsigned int", nodecl.} = int
@@ -1648,10 +1662,13 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} =
       setlen(result, int(len))
   elif defined(linux) or defined(aix):
     result = getApplAux("/proc/self/exe")
+    if result.len == 0: result = getApplHeuristic()
   elif defined(solaris):
     result = getApplAux("/proc/" & $getpid() & "/path/a.out")
+    if result.len == 0: result = getApplHeuristic()
   elif defined(freebsd):
     result = getApplAux("/proc/" & $getpid() & "/file")
+    if result.len == 0: result = getApplHeuristic()
   elif defined(macosx):
     var size: cuint32
     getExecPath1(nil, size)
@@ -1663,15 +1680,7 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [FReadIO].} =
   else:
     # little heuristic that may work on other POSIX-like systems:
     result = string(getEnv("_"))
-    if len(result) == 0:
-      result = string(paramStr(0))
-      # POSIX guaranties that this contains the executable
-      # as it has been executed by the calling process
-      if len(result) > 0 and result[0] != DirSep: # not an absolute path?
-        # iterate over any path in the $PATH environment variable
-        for p in split(string(getEnv("PATH")), {PathSep}):
-          var x = joinPath(p, result)
-          if existsFile(x): return x
+    if result.len == 0: result = getApplHeuristic()
 
 proc getApplicationFilename*(): string {.rtl, extern: "nos$1", deprecated.} =
   ## Returns the filename of the application's executable.
diff --git a/lib/system.nim b/lib/system.nim
index 4a5d46a7f..ecee7dad7 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -234,6 +234,11 @@ template `>` * (x, y: expr): expr {.immediate.} =
   ## "is greater" operator. This is the same as ``y < x``.
   y < x
 
+const
+  appType* {.magic: "AppType"}: string = ""
+    ## a string that describes the application type. Possible values:
+    ## "console", "gui", "lib".
+
 include "system/inclrtl"
 
 const NoFakeVars* = defined(NimrodVM) ## true if the backend doesn't support \
@@ -940,10 +945,6 @@ const
     ## a string that describes the host CPU. Possible values:
     ## "i386", "alpha", "powerpc", "sparc", "amd64", "mips", "arm".
   
-  appType* {.magic: "AppType"}: string = ""
-    ## a string that describes the application type. Possible values:
-    ## "console", "gui", "lib".
-  
   seqShallowFlag = low(int)
   
 proc compileOption*(option: string): bool {.
diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim
index 2f7c5ed51..2dc134eaf 100644
--- a/lib/system/excpt.nim
+++ b/lib/system/excpt.nim
@@ -77,7 +77,7 @@ proc popCurrentException {.compilerRtl, inl.} =
 
 # some platforms have native support for stack traces:
 const
-  nativeStackTraceSupported = (defined(macosx) or defined(linux)) and 
+  nativeStackTraceSupported* = (defined(macosx) or defined(linux)) and
                               not nimrodStackTrace
   hasSomeStackTrace = nimrodStackTrace or 
     defined(nativeStackTrace) and nativeStackTraceSupported
diff --git a/lib/system/inclrtl.nim b/lib/system/inclrtl.nim
index 12eb90162..5c82db4da 100644
--- a/lib/system/inclrtl.nim
+++ b/lib/system/inclrtl.nim
@@ -1,7 +1,7 @@
 #
 #
 #            Nimrod's Runtime Library
-#        (c) Copyright 2013 Andreas Rumpf
+#        (c) Copyright 2014 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
diff --git a/lib/wrappers/sqlite3.nim b/lib/wrappers/sqlite3.nim
index 586f763ae..7b7f0874e 100644
--- a/lib/wrappers/sqlite3.nim
+++ b/lib/wrappers/sqlite3.nim
@@ -106,15 +106,15 @@ type
   Pstmt* = ptr Tstmt
   Tvalue{.pure, final.} = object 
   Pvalue* = ptr Tvalue
-  PPValue* = ptr Pvalue 
+  PValueArg* = array[0..127, Pvalue]
   
   Tcallback* = proc (para1: pointer, para2: int32, para3, 
                      para4: cstringArray): int32{.cdecl.}
   Tbind_destructor_func* = proc (para1: pointer){.cdecl.}
   Tcreate_function_step_func* = proc (para1: Pcontext, para2: int32, 
-                                      para3: PPValue){.cdecl.}
+                                      para3: PValueArg){.cdecl.}
   Tcreate_function_func_func* = proc (para1: Pcontext, para2: int32, 
-                                      para3: PPValue){.cdecl.}
+                                      para3: PValueArg){.cdecl.}
   Tcreate_function_final_func* = proc (para1: Pcontext){.cdecl.}
   Tresult_func* = proc (para1: pointer){.cdecl.}
   Tcreate_collation_func* = proc (para1: pointer, para2: int32, para3: pointer,