1 #ifndef _DEIOSTREAM_H
2 #define _DEIOSTREAM_H
3 /*-------------------------------------------------------------------------
4 * drawElements Stream Library
5 * ---------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Input-output stream abstraction.
24 *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.h"
27
28 DE_BEGIN_EXTERN_C
29
30 /* Result of operation on stream */
31 typedef enum deStreamResult_e
32 {
33 DE_STREAMRESULT_SUCCESS = 0,
34 DE_STREAMRESULT_END_OF_STREAM,
35 DE_STREAMRESULT_ERROR,
36
37 DE_STREAMRESULT_LAST
38 } deStreamResult;
39
40 typedef enum deStreamStatus_e
41 {
42 DE_STREAMSTATUS_GOOD = 0,
43 DE_STREAMSTATUS_ERROR,
44
45 DE_STREAMSTATUS_LAST
46 } deStreamStatus;
47
48 /* Type for pointer to internal stream psecifig data */
49 typedef void deStreamData;
50
51 /* Function types for v_table */
52 typedef deStreamResult (*deIOStreamReadFunc)(deStreamData *stream, void *buf, int32_t bufSize, int32_t *numRead);
53 typedef deStreamResult (*deIOStreamWriteFunc)(deStreamData *stream, const void *buf, int32_t bufSize,
54 int32_t *numWritten);
55 typedef const char *(*deIOStreamGetErrorFunc)(deStreamData *stream);
56 typedef deStreamResult (*deIOStreamFlushFunc)(deStreamData *stream);
57 typedef deStreamResult (*deIOStreamDeinitFunc)(deStreamData *stream);
58 typedef deStreamStatus (*deIOStreamStatusFunc)(deStreamData *stream);
59
60 /* Virtual table type for specifying stream specifig behaviour */
61 typedef struct deIOStreamVFTable_s
62 {
63 deIOStreamReadFunc readFunc;
64 deIOStreamWriteFunc writeFunc;
65 deIOStreamGetErrorFunc getErrorFunc;
66 deIOStreamFlushFunc flushFunc;
67 deIOStreamDeinitFunc deinitFunc;
68 deIOStreamStatusFunc statusFunc;
69 } deIOStreamVFTable;
70
71 /* Generig IOStream struct */
72 typedef struct deIOStream_s
73 {
74 deStreamData *streamData;
75 const deIOStreamVFTable *vfTable;
76 } deIOStream;
77
78 DE_INLINE deStreamResult deIOStream_read(deIOStream *stream, void *buf, int32_t bufSize, int32_t *numRead);
79 DE_INLINE deStreamResult deIOStream_write(deIOStream *stream, const void *buf, int32_t bufSize, int32_t *numWritten);
80 DE_INLINE const char *deIOStream_getError(deIOStream *stream);
81 DE_INLINE deStreamStatus deIOStream_getStatus(deIOStream *stream);
82 DE_INLINE deStreamResult deIOStream_flush(deIOStream *stream);
83 DE_INLINE deStreamResult deIOStream_deinit(deIOStream *stream);
84
deIOStream_write(deIOStream * stream,const void * buf,int32_t bufSize,int32_t * numWritten)85 DE_INLINE deStreamResult deIOStream_write(deIOStream *stream, const void *buf, int32_t bufSize, int32_t *numWritten)
86 {
87 DE_ASSERT(stream);
88 DE_ASSERT(stream->vfTable);
89 DE_ASSERT(stream->vfTable->writeFunc);
90
91 return stream->vfTable->writeFunc(stream->streamData, buf, bufSize, numWritten);
92 }
93
deIOStream_read(deIOStream * stream,void * buf,int32_t bufSize,int32_t * numRead)94 DE_INLINE deStreamResult deIOStream_read(deIOStream *stream, void *buf, int32_t bufSize, int32_t *numRead)
95 {
96 DE_ASSERT(stream);
97 DE_ASSERT(stream->vfTable);
98 DE_ASSERT(stream->vfTable->readFunc);
99
100 return stream->vfTable->readFunc(stream->streamData, buf, bufSize, numRead);
101 }
102
deIOStream_getError(deIOStream * stream)103 DE_INLINE const char *deIOStream_getError(deIOStream *stream)
104 {
105 DE_ASSERT(stream);
106 DE_ASSERT(stream->vfTable);
107 DE_ASSERT(stream->vfTable->getErrorFunc);
108
109 return stream->vfTable->getErrorFunc(stream->streamData);
110 }
111
deIOStream_flush(deIOStream * stream)112 DE_INLINE deStreamResult deIOStream_flush(deIOStream *stream)
113 {
114 DE_ASSERT(stream);
115 DE_ASSERT(stream->vfTable);
116 DE_ASSERT(stream->vfTable->flushFunc);
117
118 return stream->vfTable->flushFunc(stream->streamData);
119 }
120
deIOStream_deinit(deIOStream * stream)121 DE_INLINE deStreamResult deIOStream_deinit(deIOStream *stream)
122 {
123 deStreamResult result = DE_STREAMRESULT_ERROR;
124 DE_ASSERT(stream);
125 DE_ASSERT(stream->vfTable);
126 DE_ASSERT(stream->vfTable->deinitFunc);
127
128 result = stream->vfTable->deinitFunc(stream->streamData);
129
130 stream->vfTable = DE_NULL;
131 stream->streamData = DE_NULL;
132
133 return result;
134 }
135
deIOStream_getStatus(deIOStream * stream)136 DE_INLINE deStreamStatus deIOStream_getStatus(deIOStream *stream)
137 {
138 DE_ASSERT(stream);
139 DE_ASSERT(stream->vfTable);
140 DE_ASSERT(stream->vfTable->statusFunc);
141
142 return stream->vfTable->statusFunc(stream->streamData);
143 }
144
145 DE_END_EXTERN_C
146
147 #endif /* _DEIOSTREAM_H */
148