Zen API

- Home
- Topics
- Types
- Files

All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kStream.h
Go to the documentation of this file.
1
10 #ifndef K_API_STREAM_H
11 #define K_API_STREAM_H
12
13 #include <kApi/kApiDef.h>
14 #include <kApi/Io/kStream.x.h>
15
22 //typedef kObject kStream; --forward-declared in kApiDef.x.h
23
24 /*
25 * Public
26 */
27
37 kInlineFx(kStatus) kStream_Read(kStream stream, void* buffer, kSize size)
38 {
39 kObj(kStream, stream);
40
41 if ((obj->readEnd - obj->readBegin) < size)
42 {
43 return xkStream_VTable(stream)->VReadSomeImpl(stream, buffer, size, size, kNULL);
44 }
45
46 kMemCopy(buffer, &obj->readBuffer[obj->readBegin], size);
47 obj->readBegin += size;
48
49 return kOK;
50 }
51
63 kInlineFx(kStatus) kStream_ReadSome(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
64 {
65 kObj(kStream, stream);
66 kSize bufferAvailable = obj->readEnd - obj->readBegin;
67
68 if (bufferAvailable < minCount)
69 {
70 return xkStream_VTable(stream)->VReadSomeImpl(stream, buffer, minCount, maxCount, bytesRead);
71 }
72 else
73 {
74 kSize size = kMin_(bufferAvailable, maxCount);
75
76 kMemCopy(buffer, &obj->readBuffer[obj->readBegin], size);
77 obj->readBegin += size;
78
79 if (!kIsNull(bytesRead))
80 {
81 *bytesRead = size;
82 }
83 }
84
85 return kOK;
86 }
87
97 kInlineFx(kStatus) kStream_Write(kStream stream, const void* buffer, kSize size)
98 {
99 kObj(kStream, stream);
100
101 if ((obj->writeEnd - obj->writeBegin) < size)
102 {
103 return xkStream_VTable(stream)->VWriteImpl(stream, buffer, size);
104 }
105
106 kMemCopy(&obj->writeBuffer[obj->writeBegin], buffer, size);
107 obj->writeBegin += size;
108
109 return kOK;
110 }
111
121 kInlineFx(kStatus) kStream_Copy(kStream stream, kStream source, kSize size)
122 {
123 return kStream_CopyEx(stream, source, size, kNULL, kNULL);
124 }
125
141 kFx(kStatus) kStream_CopyEx(kStream stream, kStream source, kSize size, kCallbackFx progress, kPointer context);
142
152 kInlineFx(kStatus) kStream_Seek(kStream stream, k64s offset, kSeekOrigin origin)
153 {
154 return xkStream_VTable(stream)->VSeek(stream, offset, origin);
155 }
156
164 kInlineFx(kStatus) kStream_Flush(kStream stream)
165 {
166 return xkStream_VTable(stream)->VFlush(stream);
167 }
175 kInlineFx(k64u) kStream_BytesRead(kStream stream)
176 {
177 kObj(kStream, stream);
178
179 return obj->bytesRead - (k64u)(obj->readEnd - obj->readBegin);
180 }
181
189 kInlineFx(k64u) kStream_BytesWritten(kStream stream)
190 {
191 kObj(kStream, stream);
192
193 return obj->bytesWritten + (k64u)obj->writeBegin;
194 }
195
203 kInlineFx(kStatus) kStream_ClearStats(kStream stream)
204 {
205 kObj(kStream, stream);
206
207 obj->bytesRead = 0;
208 obj->bytesWritten = 0;
209
210 return kOK;
211 }
212
213
214 /*
215 * Protected
216 */
217
229 kInlineFx(kStatus) kStream_Init(kStream stream, kType type, kAlloc allocator)
230 {
231 kCheck(kObject_Init(stream, type, allocator));
232
233 kInitFields(kStream, stream);
234
235 return kOK;
236 }
237
246 kInlineFx(kStatus) kStream_VRelease(kStream stream)
247 {
248 return kObject_VRelease(stream);
249 }
250
273 kInlineFx(kStatus) kStream_VReadSomeImpl(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
274 {
275 //legacy support: for classes that haven't been updated to implement VReadSomeImpl,
276 //try VReadImpl instead (deprecated, but still in the vtable)
277 kCheck(xkStream_VTable(stream)->VReadImpl(stream, buffer, minCount));
278
279 if (!kIsNull(bytesRead))
280 {
281 *bytesRead = minCount;
282 }
283
284 return kOK;
285 }
286
298 kInlineFx(kStatus) kStream_VWriteImpl(kStream stream, const void* buffer, kSize size)
299 {
300 return kERROR_UNIMPLEMENTED;
301 }
302
314 kInlineFx(kStatus) kStream_VSeek(kStream stream, k64s offset, kSeekOrigin origin)
315 {
316 return kERROR_UNIMPLEMENTED;
317 }
318
328 kInlineFx(kStatus) kStream_VFlush(kStream stream)
329 {
330 return kOK;
331 }
332
333 #endif
kObject::kObject_VRelease
kStatus kObject_VRelease(kObject object)
Protected virtual method that deallocates any resources owned by the object.
Definition: kObject.h:436
kStream::kStream_VFlush
kStatus kStream_VFlush(kStream stream)
Protected virtual method that flushes buffered writes to the underlying medium.
Definition: kStream.h:328
kStream::kStream_BytesRead
k64u kStream_BytesRead(kStream stream)
Reports the number of bytes read from this stream.
Definition: kStream.h:175
kMin_
#define kMin_(A, B)
Returns the minimum of two numbers.
Definition: kApiDef.h:1779
k64u
Represents a 64-bit unsigned integer.
kPointer
Represents a void pointer.
kPointer::kIsNull
#define kIsNull(POINTER)
Tests for equality with null pointer.
Definition: kApiDef.h:339
kStream::kStream_Copy
kStatus kStream_Copy(kStream stream, kStream source, kSize size)
Copies the specified number of bytes from one stream to another.
Definition: kStream.h:121
kStream::kStream_Flush
kStatus kStream_Flush(kStream stream)
Flushes buffered writes to the underlying medium.
Definition: kStream.h:164
kStream::kStream_VRelease
kStatus kStream_VRelease(kStream stream)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:246
kObject::kObject_Init
kStatus kObject_Init(kObject object, kType type, kAlloc alloc)
Protected method called by derived classes to initialize the kObject base class.
Definition: kObject.h:347
kSize
Represents an unsigned integer that can store a pointer address.
kAlloc
Abstract base class for memory allocator types.
kCheck
#define kCheck(EXPRESSION)
Executes a return statement if the given expression is not kOK.
Definition: kApiDef.h:559
kStream::kStream_ReadSome
kStatus kStream_ReadSome(kStream stream, void *buffer, kSize minCount, kSize maxCount, kSize *bytesRead)
Reads up to the specified number of bytes from the stream.
Definition: kStream.h:63
kStream::kStream_VWriteImpl
kStatus kStream_VWriteImpl(kStream stream, const void *buffer, kSize size)
Protected virtual method that writes the specified number of bytes to the stream. ...
Definition: kStream.h:298
kInlineFx
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:26
kStream::kStream_Seek
kStatus kStream_Seek(kStream stream, k64s offset, kSeekOrigin origin)
Moves the read/write pointer to the specified location, if supported by the underlying stream...
Definition: kStream.h:152
kUtils::kMemCopy
kStatus kMemCopy(void *dest, const void *src, kSize size)
Copies memory from a source buffer to a non-overlapping destination.
kSeekOrigin
Represents a stream seek origin.
kStream::kStream_VReadSomeImpl
kStatus kStream_VReadSomeImpl(kStream stream, void *buffer, kSize minCount, kSize maxCount, kSize *bytesRead)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:273
kObj
#define kObj(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from a type-checked object handle...
Definition: kApiDef.h:2858
kStream::kStream_VSeek
kStatus kStream_VSeek(kStream stream, k64s offset, kSeekOrigin origin)
Protected virtual method that moves the read/write pointer to the specified location.
Definition: kStream.h:314
kApiDef.h
Core Zen type declarations.
kStream
Represents an I/O stream.
kCallbackFx
kStatus(kCall * kCallbackFx)(kPointer receiver, kPointer sender, void *args)
Callback signature for a generic event handler.
Definition: kApiDef.h:1706
k64s
Represents a 64-bit signed integer.
kStream::kStream_CopyEx
kStatus kStream_CopyEx(kStream stream, kStream source, kSize size, kCallbackFx progress, kPointer context)
Copies the specified number of bytes from one stream to another, with progress feedback.
kStatus::kERROR_UNIMPLEMENTED
#define kERROR_UNIMPLEMENTED
Feature not implemented.
Definition: kApiDef.h:492
kStream::kStream_BytesWritten
k64u kStream_BytesWritten(kStream stream)
Reports the number of bytes written to this stream.
Definition: kStream.h:189
kType
Represents metadata about a type (class, interface, or value).
kInitFields
#define kInitFields(TYPE, OBJECT)
Zero-initializes the instance fields of the specified object.
Definition: kApiDef.h:2919
kStatus::kOK
#define kOK
Operation successful.
Definition: kApiDef.h:513
kStatus
Represents an error code.
kStream::kStream_Write
kStatus kStream_Write(kStream stream, const void *buffer, kSize size)
Writes the specified number of bytes to the stream.
Definition: kStream.h:97
kNULL
#define kNULL
Null pointer.
Definition: kApiDef.h:267
kStream::kStream_Init
kStatus kStream_Init(kStream stream, kType type, kAlloc allocator)
Protected method called by derived classes to initialize the kStream base class.
Definition: kStream.h:229
kStream::kStream_Read
kStatus kStream_Read(kStream stream, void *buffer, kSize size)
Reads the specified number of bytes from the stream.
Definition: kStream.h:37
kStream::kStream_ClearStats
kStatus kStream_ClearStats(kStream stream)
Clears stream statistics (e.g.
Definition: kStream.h:203