[OutputStream] Simpler API for stream implementers
This commit is contained in:
parent
62136f8e79
commit
49eb9a10d3
1 changed files with 23 additions and 16 deletions
|
|
@ -71,21 +71,29 @@ proc addPage(s: OutputStreamVar) =
|
||||||
startOffset: 0)
|
startOffset: 0)
|
||||||
s.flipPage
|
s.flipPage
|
||||||
|
|
||||||
proc initWithSinglePage*(s: OutputStreamVar,
|
proc implementOutputStream*(pageSize: int,
|
||||||
pageSize: int,
|
maxWriteSize = high(int),
|
||||||
maxWriteSize: int,
|
minWriteSize = 1,
|
||||||
minWriteSize = 1) =
|
vtable: ptr OutputStreamVTable = nil,
|
||||||
s.pageSize = pageSize
|
outputDevice: RootRef = nil): OutputStream =
|
||||||
s.maxWriteSize = maxWriteSize
|
## This proc is intented for use by module that implement
|
||||||
s.minWriteSize = minWriteSize
|
## their own flavours of OutputStream by providing a custom
|
||||||
s.pages = initDeque[OutputPage]()
|
## VTable. Such modules should export easier to use high-level
|
||||||
s.addPage
|
## constructors intended for end-users.
|
||||||
s.cursor.stream = s
|
result = OutputStream(
|
||||||
|
pageSize: pageSize,
|
||||||
|
maxWriteSize: maxWriteSize,
|
||||||
|
minWriteSize: minWriteSize,
|
||||||
|
pages: initDeque[OutputPage](),
|
||||||
|
vtable: vtable,
|
||||||
|
outputDevice: outputDevice)
|
||||||
|
|
||||||
|
result.addPage
|
||||||
|
result.cursor.stream = result
|
||||||
|
|
||||||
proc init*(T: type OutputStream,
|
proc init*(T: type OutputStream,
|
||||||
pageSize = defaultPageSize): OutputStream =
|
pageSize = defaultPageSize): OutputStream =
|
||||||
result = OutputStream()
|
implementOutputStream pageSize
|
||||||
result.initWithSinglePage pageSize, high(int)
|
|
||||||
|
|
||||||
let FileStreamVTable = OutputStreamVTable(
|
let FileStreamVTable = OutputStreamVTable(
|
||||||
writePage: proc (s: OutputStreamVar, data: openarray[byte]) {.nimcall, gcsafe.} =
|
writePage: proc (s: OutputStreamVar, data: openarray[byte]) {.nimcall, gcsafe.} =
|
||||||
|
|
@ -102,10 +110,9 @@ let FileStreamVTable = OutputStreamVTable(
|
||||||
proc init*(T: type OutputStream,
|
proc init*(T: type OutputStream,
|
||||||
filename: string,
|
filename: string,
|
||||||
pageSize = defaultPageSize): OutputStream =
|
pageSize = defaultPageSize): OutputStream =
|
||||||
result = OutputStream(
|
implementOutputStream pageSize,
|
||||||
outputDevice: FileOutput(file: open(filename, fmWrite)),
|
outputDevice = FileOutput(file: open(filename, fmWrite)),
|
||||||
vtable: unsafeAddr FileStreamVTable)
|
vtable = unsafeAddr FileStreamVTable
|
||||||
result.initWithSinglePage pageSize, high(int)
|
|
||||||
|
|
||||||
proc init*(T: type OutputStream,
|
proc init*(T: type OutputStream,
|
||||||
buffer: pointer, len: int): OutputStream =
|
buffer: pointer, len: int): OutputStream =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue