Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_IO_SOURCE_STM_STREAMHELPER_HXX
21 : #define INCLUDED_IO_SOURCE_STM_STREAMHELPER_HXX
22 :
23 : // Save NDEBUG state
24 : #ifdef NDEBUG
25 : #define STREAMHELPER_HXX_HAD_NDEBUG
26 : #undef NDEBUG
27 : #endif
28 :
29 : #if OSL_DEBUG_LEVEL == 0
30 : #define NDEBUG
31 : #endif
32 : #include <assert.h>
33 :
34 : #define Max( a, b ) (((a)>(b)) ? (a) : (b) )
35 : #define Min( a, b ) (((a)<(b)) ? (a) : (b) )
36 :
37 : namespace io_stm {
38 :
39 0 : class I_FIFO_OutOfBoundsException :
40 : public Exception
41 : {};
42 :
43 0 : class I_FIFO_OutOfMemoryException :
44 : public Exception
45 : {};
46 :
47 45 : class I_FIFO
48 : {
49 : public:
50 :
51 :
52 : virtual void write( const Sequence<sal_Int8> &) throw( I_FIFO_OutOfMemoryException,
53 : I_FIFO_OutOfBoundsException )=0;
54 :
55 : virtual void read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
56 : throw( I_FIFO_OutOfBoundsException )=0;
57 : virtual void skip( sal_Int32 nBytesToSkip )
58 : throw( I_FIFO_OutOfBoundsException )=0;
59 : virtual sal_Int32 getSize() const throw( ) =0;
60 : virtual void shrink() throw() = 0;
61 :
62 3 : virtual ~I_FIFO() {};
63 : };
64 :
65 :
66 0 : class IRingBuffer_OutOfBoundsException :
67 : public Exception
68 : {};
69 :
70 0 : class IRingBuffer_OutOfMemoryException :
71 : public Exception
72 : {};
73 :
74 127 : class IRingBuffer
75 : {
76 : public:
77 : /***
78 : * overwrites data at given position. Size is automatically extended, when
79 : * data is written beyond end.
80 : *
81 : ***/
82 :
83 : virtual void writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
84 : throw( IRingBuffer_OutOfMemoryException,
85 : IRingBuffer_OutOfBoundsException )=0;
86 : virtual void readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
87 : throw( IRingBuffer_OutOfBoundsException )=0;
88 : virtual sal_Int32 getSize() const throw( ) =0;
89 : virtual void forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
90 : virtual void forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException)=0;
91 : virtual void shrink() throw() = 0;
92 5 : virtual ~IRingBuffer() {};
93 : };
94 :
95 :
96 : class MemRingBuffer :
97 : public IRingBuffer
98 : {
99 : public:
100 : MemRingBuffer();
101 : virtual ~MemRingBuffer();
102 :
103 : virtual void writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &)
104 : throw( IRingBuffer_OutOfMemoryException,
105 : IRingBuffer_OutOfBoundsException ) SAL_OVERRIDE;
106 : virtual void readAt( sal_Int32 nPos, Sequence<sal_Int8> & , sal_Int32 nBytesToRead ) const
107 : throw( IRingBuffer_OutOfBoundsException ) SAL_OVERRIDE;
108 : virtual sal_Int32 getSize() const throw( ) SAL_OVERRIDE;
109 : virtual void forgetFromStart( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException) SAL_OVERRIDE;
110 : virtual void forgetFromEnd( sal_Int32 nBytesToForget ) throw(IRingBuffer_OutOfBoundsException) SAL_OVERRIDE;
111 :
112 : virtual void shrink() throw() SAL_OVERRIDE;
113 :
114 : private:
115 :
116 : void resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException );
117 10028 : inline void checkInvariants()
118 : {
119 : assert( m_nBufferLen >= 0 );
120 : assert( m_nOccupiedBuffer >= 0 );
121 : assert( m_nOccupiedBuffer <= m_nBufferLen );
122 : assert( m_nStart >= 0 );
123 : assert( 0 == m_nStart || m_nStart < m_nBufferLen );
124 10028 : }
125 :
126 : sal_Int8 *m_p;
127 : sal_Int32 m_nBufferLen;
128 : sal_Int32 m_nStart;
129 : sal_Int32 m_nOccupiedBuffer;
130 : };
131 :
132 :
133 51 : class MemFIFO :
134 : public I_FIFO,
135 : private MemRingBuffer
136 : {
137 : public:
138 : virtual void write( const Sequence<sal_Int8> &) throw( I_FIFO_OutOfMemoryException,
139 : I_FIFO_OutOfBoundsException ) SAL_OVERRIDE;
140 : virtual void read( Sequence<sal_Int8> & , sal_Int32 nBytesToRead )
141 : throw( I_FIFO_OutOfBoundsException ) SAL_OVERRIDE;
142 : virtual void skip( sal_Int32 nBytesToSkip ) throw( I_FIFO_OutOfBoundsException ) SAL_OVERRIDE;
143 2986 : virtual sal_Int32 getSize() const throw( ) SAL_OVERRIDE
144 2986 : { return MemRingBuffer::getSize(); }
145 0 : virtual void shrink() throw() SAL_OVERRIDE
146 0 : { MemRingBuffer::shrink(); }
147 :
148 : };
149 :
150 : // Restore NDEBUG state
151 : #ifdef STREAMHELPER_HXX_HAD_NDEBUG
152 : #define NDEBUG
153 : #else
154 : #undef NDEBUG
155 : #endif
156 :
157 : }
158 :
159 : #endif // INCLUDED_IO_SOURCE_STM_STREAMHELPER_HXX
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|