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
151 kFx(kStatus) kStream_CopyAll(kStream stream, kStream source);
152
162 kInlineFx(kStatus) kStream_Seek(kStream stream, k64s offset, kSeekOrigin origin)
163 {
164 return xkStream_VTable(stream)->VSeek(stream, offset, origin);
165 }
166
174 kInlineFx(kStatus) kStream_Flush(kStream stream)
175 {
176 return xkStream_VTable(stream)->VFlush(stream);
177 }
178
186 kInlineFx(kStatus) kStream_Fill(kStream stream)
187 {
188 kObj(kStream, stream);
189
190 kCheckState(!kIsNull(obj->readBuffer));
191
192 if ((obj->readEnd - obj->readBegin) > 0)
193 {
194 return kOK;
195 }
196
197 return xkStream_VTable(stream)->VFill(stream);
198 }
199
207 kInlineFx(k64u) kStream_BytesRead(kStream stream)
208 {
209 kObj(kStream, stream);
210
211 return obj->bytesRead - (k64u)(obj->readEnd - obj->readBegin);
212 }
213
221 kInlineFx(k64u) kStream_BytesWritten(kStream stream)
222 {
223 kObj(kStream, stream);
224
225 return obj->bytesWritten + (k64u)obj->writeBegin;
226 }
227
235 kInlineFx(kStatus) kStream_ClearStats(kStream stream)
236 {
237 kObj(kStream, stream);
238
239 obj->bytesRead = 0;
240 obj->bytesWritten = 0;
241
242 return kOK;
243 }
244
245 /*
246 * Protected
247 */
248
260 kInlineFx(kStatus) kStream_Init(kStream stream, kType type, kAlloc allocator)
261 {
262 kObjR(kStream, stream);
263
264 kCheck(kObject_Init(stream, type, allocator));
265
266 obj->readBuffer = kNULL;
267 obj->readCapacity = 0;
268 obj->readBegin = 0;
269 obj->readEnd = 0;
270 obj->writeBuffer = kNULL;
271 obj->writeCapacity = 0;
272 obj->writeBegin = 0;
273 obj->writeEnd = 0;
274 obj->bytesRead = 0;
275 obj->bytesWritten = 0;
276
277 return kOK;
278 }
279
288 kInlineFx(kStatus) kStream_VRelease(kStream stream)
289 {
290 return kObject_VRelease(stream);
291 }
292
315 kInlineFx(kStatus) kStream_VReadSomeImpl(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
316 {
317 //legacy support: for classes that haven't been updated to implement VReadSomeImpl,
318 //try VReadImpl instead (deprecated, but still in the vtable)
319 kCheck(xkStream_VTable(stream)->VReadImpl(stream, buffer, minCount));
320
321 if (!kIsNull(bytesRead))
322 {
323 *bytesRead = minCount;
324 }
325
326 return kOK;
327 }
328
340 kInlineFx(kStatus) kStream_VWriteImpl(kStream stream, const void* buffer, kSize size)
341 {
342 return kERROR_UNIMPLEMENTED;
343 }
344
356 kInlineFx(kStatus) kStream_VSeek(kStream stream, k64s offset, kSeekOrigin origin)
357 {
358 return kERROR_UNIMPLEMENTED;
359 }
360
370 kInlineFx(kStatus) kStream_VFlush(kStream stream)
371 {
372 return kOK;
373 }
374
384 kInlineFx(kStatus) kStream_VFill(kStream stream)
385 {
386 return kERROR_UNIMPLEMENTED;
387 }
388
389 #endif
kObject::kObject_VRelease
kStatus kObject_VRelease(kObject object)
Protected virtual method that deallocates any resources owned by the object.
Definition: kObject.h:448
kStream::kStream_VFlush
kStatus kStream_VFlush(kStream stream)
Protected virtual method that flushes buffered writes to the underlying medium.
Definition: kStream.h:370
kStream::kStream_BytesRead
k64u kStream_BytesRead(kStream stream)
Reports the number of bytes read from this stream.
Definition: kStream.h:207
kMin_
#define kMin_(A, B)
Returns the minimum of two numbers.
Definition: kApiDef.h:1842
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:341
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:174
kStream::kStream_VRelease
kStatus kStream_VRelease(kStream stream)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:288
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:359
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:561
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:340
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:162
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:315
kStream::kStream_VFill
kStatus kStream_VFill(kStream stream)
Protected virtual method that partially fills the read buffer with bytes from the underlying medium...
Definition: kStream.h:384
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:2923
kStream::kStream_CopyAll
kStatus kStream_CopyAll(kStream stream, kStream source)
Copies all bytes from one stream to another.
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:356
kObjR
#define kObjR(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from an object handle, without type-checking.
Definition: kApiDef.h:2935
kCheckState
#define kCheckState(EXPRESSION)
Executes a return statement if the given expression is not kTRUE.
Definition: kApiDef.h:595
kApiDef.h
Core Zen type declarations.
kStream
Represents an I/O stream.
kStream::kStream_Fill
kStatus kStream_Fill(kStream stream)
Partially fills the read buffer with bytes from the underlying medium.
Definition: kStream.h:186
kCallbackFx
kStatus(kCall * kCallbackFx)(kPointer receiver, kPointer sender, void *args)
Callback signature for a generic event handler.
Definition: kApiDef.h:1737
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:494
kStream::kStream_BytesWritten
k64u kStream_BytesWritten(kStream stream)
Reports the number of bytes written to this stream.
Definition: kStream.h:221
kType
Represents metadata about a type (class, interface, or value).
kStatus::kOK
#define kOK
Operation successful.
Definition: kApiDef.h:515
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:269
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:260
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:235