Finally got variant objects to work

* may still require some polish before merging
This commit is contained in:
Felix Krause 2016-05-25 23:32:49 +02:00
parent 592158fad0
commit a0c435d6eb
1 changed files with 21 additions and 15 deletions

View File

@ -72,8 +72,9 @@ proc constructObject*[T: int8|int16|int32|int64](
constructScalarItem(s, item, T): constructScalarItem(s, item, T):
result = T(parseBiggestInt(item.scalarContent)) result = T(parseBiggestInt(item.scalarContent))
template constructObject*(s: var YamlStream, c: ConstructionContext, proc constructObject*(s: var YamlStream, c: ConstructionContext,
result: var int) = result: var int)
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
## constructs an integer of architecture-defined length by loading it into ## constructs an integer of architecture-defined length by loading it into
## int32 and then converting it. ## int32 and then converting it.
var i32Result: int32 var i32Result: int32
@ -86,8 +87,9 @@ proc representObject*[T: int8|int16|int32|int64](value: T, ts: TagStyle,
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent($value, tag, yAnchorNone) yield scalarEvent($value, tag, yAnchorNone)
template representObject*(value: int, tagStyle: TagStyle, proc representObject*(value: int, tagStyle: TagStyle,
c: SerializationContext, tag: TagId): RawYamlStream = c: SerializationContext, tag: TagId): RawYamlStream
{.raises: [], inline.}=
## represent an integer of architecture-defined length by casting it to int32. ## represent an integer of architecture-defined length by casting it to int32.
## on 64-bit systems, this may cause a type conversion error. ## on 64-bit systems, this may cause a type conversion error.
@ -110,8 +112,9 @@ proc constructObject*[T: uint8|uint16|uint32|uint64](
constructScalarItem(s, item, T): constructScalarItem(s, item, T):
result = T(parseBiggestUInt(item.scalarContent)) result = T(parseBiggestUInt(item.scalarContent))
template constructObject*(s: var YamlStream, c: ConstructionContext, proc constructObject*(s: var YamlStream, c: ConstructionContext,
result: var uint) = result: var uint)
{.raises: [YamlConstructionError, YamlStreamError], inline.} =
## represent an unsigned integer of architecture-defined length by loading it ## represent an unsigned integer of architecture-defined length by loading it
## into uint32 and then converting it. ## into uint32 and then converting it.
var u32Result: uint32 var u32Result: uint32
@ -124,11 +127,11 @@ proc representObject*[T: uint8|uint16|uint32|uint64](value: T, ts: TagStyle,
result = iterator(): YamlStreamEvent = result = iterator(): YamlStreamEvent =
yield scalarEvent($value, tag, yAnchorNone) yield scalarEvent($value, tag, yAnchorNone)
template representObject*(value: uint, ts: TagStyle, c: SerializationContext, proc representObject*(value: uint, ts: TagStyle, c: SerializationContext,
tag: TagId): RawYamlStream = tag: TagId): RawYamlStream {.raises: [], inline.} =
## represent an unsigned integer of architecture-defined length by casting it ## represent an unsigned integer of architecture-defined length by casting it
## to int32. on 64-bit systems, this may cause a type conversion error. ## to int32. on 64-bit systems, this may cause a type conversion error.
representObject(uint32(value), tagStyle, c, tag) representObject(uint32(value), ts, c, tag)
proc constructObject*[T: float|float32|float64]( proc constructObject*[T: float|float32|float64](
s: var YamlStream, c: ConstructionContext, result: var T) s: var YamlStream, c: ConstructionContext, result: var T)
@ -384,13 +387,16 @@ proc representObject*[K, V](value: OrderedTable[K, V], ts: TagStyle,
yield endMapEvent() yield endMapEvent()
yield endSeqEvent() yield endSeqEvent()
template yamlTag*(T: typedesc[object|enum]): expr = proc yamlTag*(T: typedesc[object|enum]):
var uri = when compiles(yamlTag(T)): yamlTag(T) else: TagId {.inline, noSideEffect, raises: [].} =
"!nim:custom:" & (typetraits.name(type(T))) when compiles(yamlTag(T)): result = yamlTag(T)
else:
var uri = "!nim:custom:" & (typetraits.name(type(T)))
try: serializationTagLibrary.tags[uri] try: serializationTagLibrary.tags[uri]
except KeyError: serializationTagLibrary.registerUri(uri) except KeyError: serializationTagLibrary.registerUri(uri)
template yamlTag*(T: typedesc[tuple]): expr = proc yamlTag*(T: typedesc[tuple]):
TagId {.inline, noSideEffect, raises: [].} =
var var
i: T i: T
uri = "!nim:tuple(" uri = "!nim:tuple("