Skip to content

Commit

Permalink
Automatic toObjectType for ref or ptr object in useDefault***In (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko authored Oct 23, 2024
1 parent 08055d7 commit 51367e0
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions serialization/formats.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,46 @@ template createFlavor*(ModifiedFormat, FlavorName: untyped) =
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(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) =
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
readRecordValue(r, value)

template writeValue*(w: var Writer(Flavor), value: T) =
template writeValue*(w: var Writer(Flavor), value: TT) =
mixin writeRecordValue
writeRecordValue(w, value)

template useDefaultWriterIn*(T: untyped, Flavor: type) =
mixin Writer

template writeValue*(w: var Writer(Flavor), value: T) =
type TT = toObjectTypeIfNecessary(T)

template writeValue*(w: var Writer(Flavor), value: TT) =
mixin writeRecordValue
writeRecordValue(w, value)

template useDefaultReaderIn*(T: untyped, Flavor: type) =
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
readRecordValue(r, value)

Expand All @@ -83,4 +101,3 @@ macro useDefaultReaderFor*(Flavor: type, types: varargs[untyped])=

for T in types:
result.add newCall(bindSym "useDefaultReaderIn", T, Flavor)

0 comments on commit 51367e0

Please sign in to comment.