xref: /trunk/main/io/source/stm/streamhelper.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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> &)=0;
54 
55     virtual void    read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )=0;
56     virtual void    skip( sal_Int32 nBytesToSkip )=0;
57     virtual sal_Int32   getSize() const throw(  )  =0;
58     virtual void    shrink() throw() = 0;
59 
~IFIFO()60     virtual ~IFIFO() {};
61 };
62 
63 
64 class IRingBuffer_OutOfBoundsException :
65                 public Exception
66 {};
67 
68 class IRingBuffer_OutOfMemoryException :
69                 public Exception
70 {};
71 
72 class IRingBuffer
73 {
74 public:
75     /***
76     * overwrites data at given position. Size is automatically extended, when
77     * data is written beyond end.
78     *
79     ***/
80 
81     virtual void    writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)=0;
82     virtual void    readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const=0;
83     virtual sal_Int32   getSize() const throw(  )  =0;
84     virtual void    forgetFromStart( sal_Int32 nBytesToForget )=0;
85     virtual void    forgetFromEnd( sal_Int32 nBytesToForget )=0;
86     virtual void    shrink() throw() = 0;
~IRingBuffer()87     virtual ~IRingBuffer() {};
88 };
89 
90 
91 class MemRingBuffer :
92         public IRingBuffer
93 {
94 public:
95     MemRingBuffer();
96     virtual ~MemRingBuffer();
97 
98     virtual void    writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &);
99     virtual void    readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const;
100     virtual sal_Int32   getSize() const throw(  );
101     virtual void    forgetFromStart( sal_Int32 nBytesToForget );
102     virtual void    forgetFromEnd( sal_Int32 nBytesToForget );
103 
104     virtual void shrink() throw();
105 
106 private:
107 
108     void resizeBuffer( sal_Int32 nMinSize );
checkInvariants()109     inline void checkInvariants()
110     {
111         assert( m_nBufferLen >= 0 );
112         assert( m_nOccupiedBuffer >= 0 );
113         assert( m_nOccupiedBuffer <= m_nBufferLen );
114         assert( m_nStart >= 0 );
115         assert( 0 == m_nStart || m_nStart < m_nBufferLen );
116     }
117 
118     sal_Int8    *m_p;
119     sal_Int32   m_nBufferLen;
120     sal_Int32   m_nStart;
121     sal_Int32   m_nOccupiedBuffer;
122 };
123 
124 
125 class MemFIFO :
126         public  IFIFO,
127         private MemRingBuffer
128 {
129 public:
130     virtual void    write( const Sequence<sal_Int8> &);
131     virtual void    read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead );
132     virtual void    skip( sal_Int32 nBytesToSkip );
getSize() const133     virtual sal_Int32   getSize()  const throw(  )
134                         { return MemRingBuffer::getSize(); }
shrink()135     virtual void    shrink() throw()
136                         { MemRingBuffer::shrink(); }
137 
138 };
139 
140 // Restore NDEBUG state
141 #ifdef STREAMHELPER_HXX_HAD_NDEBUG
142 #define NDEBUG
143 #else
144 #undef NDEBUG
145 #endif
146 
147 }
148