Home | History | Annotate | Download | only in destream
      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, deInt32 bufSize, deInt32* numRead);
     53 typedef deStreamResult	(*deIOStreamWriteFunc)		(deStreamData* stream, const void* buf, deInt32 bufSize, deInt32* numWritten);
     54 typedef const char*		(*deIOStreamGetErrorFunc)	(deStreamData* stream);
     55 typedef deStreamResult	(*deIOStreamFlushFunc)		(deStreamData* stream);
     56 typedef deStreamResult	(*deIOStreamDeinitFunc)		(deStreamData* stream);
     57 typedef deStreamStatus	(*deIOStreamStatusFunc)		(deStreamData* stream);
     58 
     59 /* Virtual table type for specifying stream specifig behaviour */
     60 typedef struct deIOStreamVFTable_s
     61 {
     62 	deIOStreamReadFunc		readFunc;
     63 	deIOStreamWriteFunc		writeFunc;
     64 	deIOStreamGetErrorFunc	getErrorFunc;
     65 	deIOStreamFlushFunc		flushFunc;
     66 	deIOStreamDeinitFunc	deinitFunc;
     67 	deIOStreamStatusFunc	statusFunc;
     68 } deIOStreamVFTable;
     69 
     70 /* Generig IOStream struct */
     71 typedef struct deIOStream_s
     72 {
     73 	deStreamData*				streamData;
     74 	const deIOStreamVFTable*	vfTable;
     75 } deIOStream;
     76 
     77 DE_INLINE deStreamResult	deIOStream_read			(deIOStream* stream, void* buf, deInt32 bufSize, deInt32* numRead);
     78 DE_INLINE deStreamResult	deIOStream_write		(deIOStream* stream, const void* buf, deInt32 bufSize, deInt32* numWritten);
     79 DE_INLINE const char*		deIOStream_getError		(deIOStream* stream);
     80 DE_INLINE deStreamStatus	deIOStream_getStatus	(deIOStream* stream);
     81 DE_INLINE deStreamResult	deIOStream_flush		(deIOStream* stream);
     82 DE_INLINE deStreamResult	deIOStream_deinit		(deIOStream* stream);
     83 
     84 
     85 
     86 DE_INLINE deStreamResult deIOStream_write (deIOStream* stream, const void* buf, deInt32 bufSize, deInt32* numWritten)
     87 {
     88 	DE_ASSERT(stream);
     89 	DE_ASSERT(stream->vfTable);
     90 	DE_ASSERT(stream->vfTable->writeFunc);
     91 
     92 	return stream->vfTable->writeFunc(stream->streamData, buf, bufSize, numWritten);
     93 }
     94 
     95 DE_INLINE deStreamResult deIOStream_read (deIOStream* stream, void* buf, deInt32 bufSize, deInt32* numRead)
     96 {
     97 	DE_ASSERT(stream);
     98 	DE_ASSERT(stream->vfTable);
     99 	DE_ASSERT(stream->vfTable->readFunc);
    100 
    101 	return stream->vfTable->readFunc(stream->streamData, buf, bufSize, numRead);
    102 }
    103 
    104 DE_INLINE const char* deIOStream_getError (deIOStream* stream)
    105 {
    106 	DE_ASSERT(stream);
    107 	DE_ASSERT(stream->vfTable);
    108 	DE_ASSERT(stream->vfTable->getErrorFunc);
    109 
    110 	return stream->vfTable->getErrorFunc(stream->streamData);
    111 }
    112 
    113 DE_INLINE deStreamResult deIOStream_flush (deIOStream* stream)
    114 {
    115 	DE_ASSERT(stream);
    116 	DE_ASSERT(stream->vfTable);
    117 	DE_ASSERT(stream->vfTable->flushFunc);
    118 
    119 	return stream->vfTable->flushFunc(stream->streamData);
    120 }
    121 
    122 DE_INLINE deStreamResult deIOStream_deinit (deIOStream* stream)
    123 {
    124 	deStreamResult result = DE_STREAMRESULT_ERROR;
    125 	DE_ASSERT(stream);
    126 	DE_ASSERT(stream->vfTable);
    127 	DE_ASSERT(stream->vfTable->deinitFunc);
    128 
    129 	result = stream->vfTable->deinitFunc(stream->streamData);
    130 
    131 	stream->vfTable		= DE_NULL;
    132 	stream->streamData	= DE_NULL;
    133 
    134 	return result;
    135 }
    136 
    137 DE_INLINE deStreamStatus deIOStream_getStatus (deIOStream* stream)
    138 {
    139 	DE_ASSERT(stream);
    140 	DE_ASSERT(stream->vfTable);
    141 	DE_ASSERT(stream->vfTable->statusFunc);
    142 
    143 	return stream->vfTable->statusFunc(stream->streamData);
    144 }
    145 
    146 DE_END_EXTERN_C
    147 
    148 #endif /* _DEIOSTREAM_H */
    149