Automatic toObjectType for ref or ptr object in useDefault***In (#77)
This commit is contained in:
parent
08055d72f3
commit
51367e0c79
|
@ -41,28 +41,46 @@ template createFlavor*(ModifiedFormat, FlavorName: untyped) =
|
||||||
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(ModifiedFormat)
|
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(ModifiedFormat)
|
||||||
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat)
|
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat)
|
||||||
|
|
||||||
|
template toObjectType(T: type): untyped =
|
||||||
|
typeof(T()[])
|
||||||
|
|
||||||
|
template toObjectTypeIfNecessary(T: type): untyped =
|
||||||
|
when T is ref|ptr:
|
||||||
|
toObjectType(T)
|
||||||
|
else:
|
||||||
|
T
|
||||||
|
|
||||||
|
# useDefault***In or useDefault***For only works for
|
||||||
|
# object|ref object|ptr object
|
||||||
|
|
||||||
template useDefaultSerializationIn*(T: untyped, Flavor: type) =
|
template useDefaultSerializationIn*(T: untyped, Flavor: type) =
|
||||||
mixin Reader, Writer
|
mixin Reader, Writer
|
||||||
|
|
||||||
template readValue*(r: var Reader(Flavor), value: var T) =
|
type TT = toObjectTypeIfNecessary(T)
|
||||||
|
|
||||||
|
template readValue*(r: var Reader(Flavor), value: var TT) =
|
||||||
mixin readRecordValue
|
mixin readRecordValue
|
||||||
readRecordValue(r, value)
|
readRecordValue(r, value)
|
||||||
|
|
||||||
template writeValue*(w: var Writer(Flavor), value: T) =
|
template writeValue*(w: var Writer(Flavor), value: TT) =
|
||||||
mixin writeRecordValue
|
mixin writeRecordValue
|
||||||
writeRecordValue(w, value)
|
writeRecordValue(w, value)
|
||||||
|
|
||||||
template useDefaultWriterIn*(T: untyped, Flavor: type) =
|
template useDefaultWriterIn*(T: untyped, Flavor: type) =
|
||||||
mixin Writer
|
mixin Writer
|
||||||
|
|
||||||
template writeValue*(w: var Writer(Flavor), value: T) =
|
type TT = toObjectTypeIfNecessary(T)
|
||||||
|
|
||||||
|
template writeValue*(w: var Writer(Flavor), value: TT) =
|
||||||
mixin writeRecordValue
|
mixin writeRecordValue
|
||||||
writeRecordValue(w, value)
|
writeRecordValue(w, value)
|
||||||
|
|
||||||
template useDefaultReaderIn*(T: untyped, Flavor: type) =
|
template useDefaultReaderIn*(T: untyped, Flavor: type) =
|
||||||
mixin Reader
|
mixin Reader
|
||||||
|
|
||||||
template readValue*(r: var Reader(Flavor), value: var T) =
|
type TT = toObjectTypeIfNecessary(T)
|
||||||
|
|
||||||
|
template readValue*(r: var Reader(Flavor), value: var TT) =
|
||||||
mixin readRecordValue
|
mixin readRecordValue
|
||||||
readRecordValue(r, value)
|
readRecordValue(r, value)
|
||||||
|
|
||||||
|
@ -83,4 +101,3 @@ macro useDefaultReaderFor*(Flavor: type, types: varargs[untyped])=
|
||||||
|
|
||||||
for T in types:
|
for T in types:
|
||||||
result.add newCall(bindSym "useDefaultReaderIn", T, Flavor)
|
result.add newCall(bindSym "useDefaultReaderIn", T, Flavor)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue