xref: /trunk/main/io/source/stm/streamhelper.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // Save NDEBUG state
29 #ifdef NDEBUG
30 #define STREAMHELPER_HXX_HAD_NDEBUG
31 #undef NDEBUG
32 #endif
33 
34 #if OSL_DEBUG_LEVEL == 0
35 #define NDEBUG
36 #endif
37 #include <assert.h>
38 
39 #define Max( a, b )     (((a)>(b)) ? (a) : (b) )
40 #define Min( a, b )		(((a)<(b)) ? (a) : (b) )
41 
42 namespace io_stm {
43 
44 class IFIFO_OutOfBoundsException :
45 				public Exception
46 {};
47 
48 class IFIFO_OutOfMemoryException :
49 				public Exception
50 {};
51 
52 class IFIFO
53 {
54 public:
55 
56 
57 	virtual void 	write( const Sequence<sal_Int8> &) throw( IFIFO_OutOfMemoryException,
58 															  IFIFO_OutOfBoundsException )=0;
59 
60 	virtual void 	read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
61 													throw( IFIFO_OutOfBoundsException )=0;
62 	virtual void 	skip( sal_Int32 nBytesToSkip )
63 										throw( IFIFO_OutOfBoundsException )=0;
64 	virtual sal_Int32 	getSize() const throw(  )  =0;
65 	virtual void 	shrink() throw() = 0;
66 
67 	virtual ~IFIFO() {};
68 };
69 
70 
71 class IRingBuffer_OutOfBoundsException :
72 				public Exception
73 {};
74 
75 class IRingBuffer_OutOfMemoryException :
76 				public Exception
77 {};
78 
79 class IRingBuffer
80 {
81 public:
82 	/***
83 	* overwrites data at given position. Size is automatically extended, when
84 	* data is written beyond end.
85 	*
86 	***/
87 
88 	virtual void 	writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
89 		throw( IRingBuffer_OutOfMemoryException,
90 			   IRingBuffer_OutOfBoundsException )=0;
91 	virtual void 	readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
92 		throw( IRingBuffer_OutOfBoundsException )=0;
93 	virtual sal_Int32 	getSize() const throw(  )  =0;
94 	virtual void 	forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
95 	virtual void	forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
96 	virtual void 	shrink() throw() = 0;
97 	virtual ~IRingBuffer() {};
98 };
99 
100 
101 class MemRingBuffer :
102 		public IRingBuffer
103 {
104 public:
105 	MemRingBuffer();
106 	virtual ~MemRingBuffer();
107 
108 	virtual void 	writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
109 													throw(	IRingBuffer_OutOfMemoryException,
110 																IRingBuffer_OutOfBoundsException );
111 	virtual void 	readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
112 													throw( IRingBuffer_OutOfBoundsException );
113 	virtual sal_Int32 	getSize() const throw(  );
114 	virtual void 	forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException);
115 	virtual void	forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException);
116 
117 	virtual void shrink() throw();
118 
119 private:
120 
121 	void resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException );
122 	inline void checkInvariants()
123 	{
124 		assert( m_nBufferLen >= 0 );
125 		assert( m_nOccupiedBuffer >= 0 );
126 		assert( m_nOccupiedBuffer <= m_nBufferLen );
127 		assert( m_nStart >= 0 );
128 		assert( 0 == m_nStart || m_nStart < m_nBufferLen );
129 	}
130 
131 	sal_Int8 	*m_p;
132 	sal_Int32 	m_nBufferLen;
133 	sal_Int32 	m_nStart;
134 	sal_Int32 	m_nOccupiedBuffer;
135 };
136 
137 
138 class MemFIFO :
139 		public  IFIFO,
140 		private MemRingBuffer
141 {
142 public:
143 	virtual void 	write( const Sequence<sal_Int8> &) throw( IFIFO_OutOfMemoryException,
144 															  IFIFO_OutOfBoundsException );
145 	virtual void 	read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
146 													throw( IFIFO_OutOfBoundsException );
147 	virtual void 	skip( sal_Int32 nBytesToSkip ) throw( IFIFO_OutOfBoundsException );
148 	virtual sal_Int32 	getSize()  const throw(  )
149 						{ return MemRingBuffer::getSize(); }
150 	virtual void 	shrink() throw()
151 						{ MemRingBuffer::shrink(); }
152 
153 };
154 
155 // Restore NDEBUG state
156 #ifdef STREAMHELPER_HXX_HAD_NDEBUG
157 #define NDEBUG
158 #else
159 #undef NDEBUG
160 #endif
161 
162 }
163