diff options
author | Joshua Cearley <joshua.cearley@gmail.com> | 2014-07-23 21:30:03 -0500 |
---|---|---|
committer | Joshua Cearley <joshua.cearley@gmail.com> | 2014-07-23 21:30:03 -0500 |
commit | 2577c92ec757f57d9238c362922f01bb807aff54 (patch) | |
tree | d397cd074832ed683b6fc02a306dbed95a278cb0 /lib/core | |
parent | dcf1425eb996db5d39a23c0360573f1addd4a850 (diff) | |
download | Nim-2577c92ec757f57d9238c362922f01bb807aff54.tar.gz |
Add better support for unsigned ints via typeinfo.
Diffstat (limited to 'lib/core')
-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) |