diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2014-07-29 19:39:22 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2014-07-29 19:39:22 +0200 |
commit | 651f5122e4c8c6223892db63c7933ffa371fe047 (patch) | |
tree | 6d6e5df3bef9ba6655182f504067add8d8121ea7 /lib | |
parent | 4eccfb979eeabc18ac671ffd7d83348cd3aa2296 (diff) | |
parent | 2577c92ec757f57d9238c362922f01bb807aff54 (diff) | |
download | Nim-651f5122e4c8c6223892db63c7933ffa371fe047.tar.gz |
Merge pull request #1408 from Skrylar/skry-unsigned-any
Add better support for unsigned ints via typeinfo.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/core/typeinfo.nim | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/core/typeinfo.nim b/lib/core/typeinfo.nim index edb4d1188..93b90e120 100644 --- a/lib/core/typeinfo.nim +++ b/lib/core/typeinfo.nim @@ -420,6 +420,59 @@ proc setBiggestInt*(x: TAny, y: biggestInt) = of tyUInt32: cast[ptr uint32](x.value)[] = uint32(y) else: assert false +proc getUInt*(x: TAny): uint = + ## retrieve the uint value out of `x`, `x` needs to represent an uint. + assert skipRange(x.rawtype).kind == tyUInt + result = cast[ptr uint](x.value)[] + +proc getUInt8*(x: TAny): uint8 = + ## retrieve the uint8 value out of `x`, `x` needs to represent an + ## uint8. + assert skipRange(x.rawtype).kind == tyUInt8 + result = cast[ptr uint8](x.value)[] + +proc getUInt16*(x: TAny): uint16 = + ## retrieve the uint16 value out of `x`, `x` needs to represent an + ## uint16. + assert skipRange(x.rawtype).kind == tyUInt16 + result = cast[ptr uint16](x.value)[] + +proc getUInt32*(x: TAny): uint32 = + ## retrieve the uint32 value out of `x`, `x` needs to represent an + ## uint32. + assert skipRange(x.rawtype).kind == tyUInt32 + result = cast[ptr uint32](x.value)[] + +proc getUInt64*(x: TAny): uint64 = + ## retrieve the uint64 value out of `x`, `x` needs to represent an + ## uint64. + assert skipRange(x.rawtype).kind == tyUInt64 + result = cast[ptr uint64](x.value)[] + +proc getBiggestUint*(x: TAny): uint64 = + ## retrieve the unsigned integer value out of `x`. `x` needs to + ## represent an unsigned integer. + var t = skipRange(x.rawtype) + case t.kind + of akUInt: result = uint64(cast[ptr uint](x.value)[]) + of akUInt8: result = uint64(cast[ptr uint8](x.value)[]) + of akUInt16: result = uint64(cast[ptr uint16](x.value)[]) + of akUInt32: result = uint64(cast[ptr uint32](x.value)[]) + of akUInt64: result = uint64(cast[ptr uint64](x.value)[]) + else: assert false + +proc setBiggestUint*(x: TAny; y: uint64) = + ## sets the unsigned integer value of `c`. `c` needs to represent an + ## unsigned integer. + var t = skipRange(x.rawtype) + case t.kind: + of akUInt: result = cast[ptr uint](x.value)[] = uint(y) + of akUInt8: result = cast[ptr uint8](x.value)[] = uint8(y) + of akUInt16: result = cast[ptr uint16](x.value)[] = uint16(y) + of akUInt32: result = cast[ptr uint32](x.value)[] = uint32(y) + of akUInt64: result = cast[ptr uint64](x.value)[] = uint64(y) + else: assert false + proc getChar*(x: TAny): char = ## retrieve the char value out of `x`. `x` needs to represent a char. var t = skipRange(x.rawtype) |