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