xref: /trunk/main/io/source/stm/streamhelper.hxx (revision 36cac86c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // Save NDEBUG state
25 #ifdef NDEBUG
26 #define STREAMHELPER_HXX_HAD_NDEBUG
27 #undef NDEBUG
28 #endif
29 
30 #if OSL_DEBUG_LEVEL == 0
31 #define NDEBUG
32 #endif
33 #include <assert.h>
34 
35 #define Max( a, b )     (((a)>(b)) ? (a) : (b) )
36 #define Min( a, b )		(((a)<(b)) ? (a) : (b) )
37 
38 namespace io_stm {
39 
40 class IFIFO_OutOfBoundsException :
41 				public Exception
42 {};
43 
44 class IFIFO_OutOfMemoryException :
45 				public Exception
46 {};
47 
48 class IFIFO
49 {
50 public:
51 
52 
53 	virtual void 	write( const Sequence<sal_Int8> &) throw( IFIFO_OutOfMemoryException,
54 															  IFIFO_OutOfBoundsException )=0;
55 
56 	virtual void 	read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
57 													throw( IFIFO_OutOfBoundsException )=0;
58 	virtual void 	skip( sal_Int32 nBytesToSkip )
59 										throw( IFIFO_OutOfBoundsException )=0;
60 	virtual sal_Int32 	getSize() const throw(  )  =0;
61 	virtual void 	shrink() throw() = 0;
62 
~IFIFO()63 	virtual ~IFIFO() {};
64 };
65 
66 
67 class IRingBuffer_OutOfBoundsException :
68 				public Exception
69 {};
70 
71 class IRingBuffer_OutOfMemoryException :
72 				public Exception
73 {};
74 
75 class IRingBuffer
76 {
77 public:
78 	/***
79 	* overwrites data at given position. Size is automatically extended, when
80 	* data is written beyond end.
81 	*
82 	***/
83 
84 	virtual void 	writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
85 		throw( IRingBuffer_OutOfMemoryException,
86 			   IRingBuffer_OutOfBoundsException )=0;
87 	virtual void 	readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
88 		throw( IRingBuffer_OutOfBoundsException )=0;
89 	virtual sal_Int32 	getSize() const throw(  )  =0;
90 	virtual void 	forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
91 	virtual void	forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
92 	virtual void 	shrink() throw() = 0;
~IRingBuffer()93 	virtual ~IRingBuffer() {};
94 };
95 
96 
97 class MemRingBuffer :
98 		public IRingBuffer
99 {
100 public:
101 	MemRingBuffer();
102 	virtual ~MemRingBuffer();
103 
104 	virtual void 	writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
105 													throw(	IRingBuffer_OutOfMemoryException,
106 																IRingBuffer_OutOfBoundsException );
107 	virtual void 	readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
108 													throw( IRingBuffer_OutOfBoundsException );
109 	virtual sal_Int32 	getSize() const throw(  );
110 	virtual void 	forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException);
111 	virtual void	forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException);
112 
113 	virtual void shrink() throw();
114 
115 private:
116 
117 	void resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException );
checkInvariants()118 	inline void checkInvariants()
119 	{
120 		assert( m_nBufferLen >= 0 );
121 		assert( m_nOccupiedBuffer >= 0 );
122 		assert( m_nOccupiedBuffer <= m_nBufferLen );
123 		assert( m_nStart >= 0 );
124 		assert( 0 == m_nStart || m_nStart < m_nBufferLen );
125 	}
126 
127 	sal_Int8 	*m_p;
128 	sal_Int32 	m_nBufferLen;
129 	sal_Int32 	m_nStart;
130 	sal_Int32 	m_nOccupiedBuffer;
131 };
132 
133 
134 class MemFIFO :
135 		public  IFIFO,
136 		private MemRingBuffer
137 {
138 public:
139 	virtual void 	write( const Sequence<sal_Int8> &) throw( IFIFO_OutOfMemoryException,
140 															  IFIFO_OutOfBoundsException );
141 	virtual void 	read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
142 													throw( IFIFO_OutOfBoundsException );
143 	virtual void 	skip( sal_Int32 nBytesToSkip ) throw( IFIFO_OutOfBoundsException );
getSize() const144 	virtual sal_Int32 	getSize()  const throw(  )
145 						{ return MemRingBuffer::getSize(); }
shrink()146 	virtual void 	shrink() throw()
147 						{ MemRingBuffer::shrink(); }
148 
149 };
150 
151 // Restore NDEBUG state
152 #ifdef STREAMHELPER_HXX_HAD_NDEBUG
153 #define NDEBUG
154 #else
155 #undef NDEBUG
156 #endif
157 
158 }
159