/src/libreoffice/include/unotools/streamwrap.hxx
Line | Count | Source |
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_UNOTOOLS_STREAMWRAP_HXX |
21 | | #define INCLUDED_UNOTOOLS_STREAMWRAP_HXX |
22 | | |
23 | | #include <unotools/unotoolsdllapi.h> |
24 | | #include <com/sun/star/io/XOutputStream.hpp> |
25 | | #include <com/sun/star/io/XInputStream.hpp> |
26 | | #include <com/sun/star/io/XSeekable.hpp> |
27 | | #include <com/sun/star/io/XTruncate.hpp> |
28 | | #include <com/sun/star/io/XStream.hpp> |
29 | | #include <comphelper/bytereader.hxx> |
30 | | #include <cppuhelper/implbase.hxx> |
31 | | #include <cppuhelper/implbase1.hxx> |
32 | | #include <memory> |
33 | | #include <mutex> |
34 | | |
35 | | class SvStream; |
36 | | |
37 | | namespace utl |
38 | | { |
39 | | |
40 | | // workaround for incremental linking bugs in MSVC2015 |
41 | | class SAL_DLLPUBLIC_TEMPLATE OInputStreamWrapper_Base : public cppu::WeakImplHelper< css::io::XInputStream > {}; |
42 | | |
43 | | /// helper class for wrapping an SvStream into a com.sun.star.io::XInputStream |
44 | | class UNOTOOLS_DLLPUBLIC OInputStreamWrapper : public OInputStreamWrapper_Base, public comphelper::ByteReader |
45 | | { |
46 | | protected: |
47 | | std::mutex m_aMutex; |
48 | | SvStream* m_pSvStream; |
49 | | bool m_bSvStreamOwner : 1; |
50 | | OInputStreamWrapper() |
51 | 182k | { m_pSvStream = nullptr; m_bSvStreamOwner = false; } |
52 | | void SetStream(SvStream* _pStream, bool bOwner ) |
53 | 182k | { m_pSvStream = _pStream; m_bSvStreamOwner = bOwner; } |
54 | | |
55 | | public: |
56 | | OInputStreamWrapper(SvStream& _rStream); |
57 | | OInputStreamWrapper(SvStream* pStream, bool bOwner=false); |
58 | | OInputStreamWrapper(std::unique_ptr<SvStream> pStream); |
59 | | virtual ~OInputStreamWrapper() override; |
60 | | |
61 | | // css::io::XInputStream |
62 | | virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) override final; |
63 | | virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) override final; |
64 | | virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override final; |
65 | | virtual sal_Int32 SAL_CALL available() override final; |
66 | | virtual void SAL_CALL closeInput() override final; |
67 | | |
68 | | // utl::ByteReader |
69 | | virtual sal_Int32 readSomeBytes( sal_Int8* aData, sal_Int32 nMaxBytesToRead ) final override; |
70 | | |
71 | | protected: |
72 | | /// throws a NotConnectedException if the object is not connected anymore |
73 | | void checkConnected() const; |
74 | | /// throws an exception according to the error flag of m_pSvStream |
75 | | void checkError() const; |
76 | | }; |
77 | | |
78 | | //= OSeekableInputStreamWrapper |
79 | | |
80 | | /** helper class for wrapping an SvStream into a com.sun.star.io::XInputStream |
81 | | which is seekable (i.e. supports the com.sun.star.io::XSeekable interface). |
82 | | */ |
83 | | class UNOTOOLS_DLLPUBLIC OSeekableInputStreamWrapper |
84 | | : public cppu::ImplInheritanceHelper< OInputStreamWrapper, css::io::XSeekable > |
85 | | { |
86 | | protected: |
87 | 13.4k | OSeekableInputStreamWrapper() {} |
88 | | ~OSeekableInputStreamWrapper() override; |
89 | | |
90 | | public: |
91 | | OSeekableInputStreamWrapper(SvStream& _rStream); |
92 | | OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner = false); |
93 | | |
94 | | // XSeekable |
95 | | virtual void SAL_CALL seek( sal_Int64 _nLocation ) override final; |
96 | | virtual sal_Int64 SAL_CALL getPosition( ) override final; |
97 | | virtual sal_Int64 SAL_CALL getLength( ) override final; |
98 | | }; |
99 | | |
100 | | //= OOutputStreamWrapper |
101 | | |
102 | | // avoid LNK2005 duplicate WeakImplHelper definitions with MSVC |
103 | | class SAL_DLLPUBLIC_TEMPLATE OOutputStreamWrapper_Base : public cppu::WeakImplHelper<css::io::XOutputStream> |
104 | | { |
105 | | }; |
106 | | |
107 | | class UNOTOOLS_DLLPUBLIC OOutputStreamWrapper : public OOutputStreamWrapper_Base |
108 | | { |
109 | | public: |
110 | | OOutputStreamWrapper(SvStream& _rStream); |
111 | | |
112 | | // css::io::XOutputStream |
113 | | virtual void SAL_CALL writeBytes(const css::uno::Sequence< sal_Int8 >& aData) override final; |
114 | | virtual void SAL_CALL flush() override final; |
115 | | virtual void SAL_CALL closeOutput() override final; |
116 | | |
117 | | protected: |
118 | | virtual ~OOutputStreamWrapper() override; |
119 | | |
120 | | /// throws an exception according to the error flag of m_pSvStream |
121 | | void checkError() const; |
122 | | |
123 | | // TODO: thread safety! |
124 | | SvStream& rStream; |
125 | | }; |
126 | | |
127 | | //= OSeekableOutputStreamWrapper |
128 | | |
129 | | typedef ::cppu::ImplHelper1 < css::io::XSeekable |
130 | | > OSeekableOutputStreamWrapper_Base; |
131 | | /** helper class for wrapping an SvStream into a com.sun.star.io::XOutputStream |
132 | | which is seekable (i.e. supports the com.sun.star.io::XSeekable interface). |
133 | | */ |
134 | | class UNOTOOLS_DLLPUBLIC OSeekableOutputStreamWrapper final |
135 | | :public OOutputStreamWrapper |
136 | | ,public OSeekableOutputStreamWrapper_Base |
137 | | { |
138 | | public: |
139 | | OSeekableOutputStreamWrapper(SvStream& _rStream); |
140 | | |
141 | | private: |
142 | | virtual ~OSeekableOutputStreamWrapper() override; |
143 | | |
144 | | // disambiguate XInterface |
145 | | virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type& _rType ) override; |
146 | | virtual void SAL_CALL acquire( ) noexcept override |
147 | 0 | { OOutputStreamWrapper::acquire(); } |
148 | | virtual void SAL_CALL release( ) noexcept override |
149 | 0 | { OOutputStreamWrapper::release(); } |
150 | | |
151 | | // XSeekable |
152 | | virtual void SAL_CALL seek( sal_Int64 _nLocation ) override; |
153 | | virtual sal_Int64 SAL_CALL getPosition( ) override; |
154 | | virtual sal_Int64 SAL_CALL getLength( ) override; |
155 | | }; |
156 | | |
157 | | class UNOTOOLS_DLLPUBLIC OStreamWrapper final |
158 | | : public cppu::ImplInheritanceHelper<OSeekableInputStreamWrapper, |
159 | | css::io::XStream, |
160 | | css::io::XOutputStream, |
161 | | css::io::XTruncate> |
162 | | { |
163 | | ~OStreamWrapper() override; |
164 | | |
165 | | public: |
166 | | OStreamWrapper(SvStream& _rStream); |
167 | | OStreamWrapper(std::unique_ptr<SvStream> _rStream); |
168 | | OStreamWrapper(SvStream* _pStream, bool _bOwner = false); |
169 | | |
170 | | // css::io::XStream |
171 | | virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream( ) override; |
172 | | virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream( ) override; |
173 | | |
174 | | // css::io::XOutputStream |
175 | | virtual void SAL_CALL writeBytes(const css::uno::Sequence< sal_Int8 >& aData) override; |
176 | | virtual void SAL_CALL flush() override; |
177 | | virtual void SAL_CALL closeOutput() override; |
178 | | virtual void SAL_CALL truncate() override; |
179 | | }; |
180 | | |
181 | | } |
182 | | // namespace utl |
183 | | |
184 | | #endif // INCLUDED_UNOTOOLS_STREAMWRAP_HXX |
185 | | |
186 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |