GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svl / source / fsstor / ostreamcontainer.cxx
blob0089784c18da4bc5a02f792e94e660cac0b98ceb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "ostreamcontainer.hxx"
24 using namespace ::com::sun::star;
26 OFSStreamContainer::OFSStreamContainer( const uno::Reference < io::XStream >& xStream )
27 : m_bDisposed( false )
28 , m_bInputClosed( false )
29 , m_bOutputClosed( false )
30 , m_pListenersContainer( NULL )
31 , m_pTypeCollection( NULL )
33 try
35 m_xStream = xStream;
36 if ( !m_xStream.is() )
37 throw uno::RuntimeException();
39 m_xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
40 m_xInputStream = xStream->getInputStream();
41 m_xOutputStream = xStream->getOutputStream();
42 m_xTruncate = uno::Reference< io::XTruncate >( m_xOutputStream, uno::UNO_QUERY );
43 m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >( m_xOutputStream, uno::UNO_QUERY );
45 catch( uno::Exception& )
47 m_xStream = uno::Reference< io::XStream >();
48 m_xSeekable = uno::Reference< io::XSeekable >();
49 m_xInputStream = uno::Reference< io::XInputStream >();
50 m_xOutputStream = uno::Reference< io::XOutputStream >();
51 m_xTruncate = uno::Reference< io::XTruncate >();
52 m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >();
56 OFSStreamContainer::~OFSStreamContainer()
58 if ( m_pListenersContainer )
60 delete m_pListenersContainer;
61 m_pListenersContainer = NULL;
65 // XInterface
66 uno::Any SAL_CALL OFSStreamContainer::queryInterface( const uno::Type& rType )
67 throw( uno::RuntimeException )
69 uno::Any aReturn;
71 aReturn <<= ::cppu::queryInterface
72 ( rType
73 , static_cast<lang::XTypeProvider*> ( this )
74 , static_cast<io::XStream*> ( this )
75 , static_cast<embed::XExtendedStorageStream*> ( this )
76 , static_cast<lang::XComponent*> ( this ) );
78 if ( aReturn.hasValue() == sal_True )
79 return aReturn ;
81 if ( m_xSeekable.is() )
83 aReturn <<= ::cppu::queryInterface
84 ( rType
85 , static_cast<io::XSeekable*> ( this ) );
87 if ( aReturn.hasValue() == sal_True )
88 return aReturn ;
91 if ( m_xInputStream.is() )
93 aReturn <<= ::cppu::queryInterface
94 ( rType
95 , static_cast<io::XInputStream*> ( this ) );
97 if ( aReturn.hasValue() == sal_True )
98 return aReturn ;
100 if ( m_xOutputStream.is() )
102 aReturn <<= ::cppu::queryInterface
103 ( rType
104 , static_cast<io::XOutputStream*> ( this ) );
106 if ( aReturn.hasValue() == sal_True )
107 return aReturn ;
109 if ( m_xTruncate.is() )
111 aReturn <<= ::cppu::queryInterface
112 ( rType
113 , static_cast<io::XTruncate*> ( this ) );
115 if ( aReturn.hasValue() == sal_True )
116 return aReturn ;
118 if ( m_xAsyncOutputMonitor.is() )
120 aReturn <<= ::cppu::queryInterface
121 ( rType
122 , static_cast<io::XAsyncOutputMonitor*> ( this ) );
124 if ( aReturn.hasValue() == sal_True )
125 return aReturn ;
128 return OWeakObject::queryInterface( rType );
131 void SAL_CALL OFSStreamContainer::acquire()
132 throw()
134 OWeakObject::acquire();
137 void SAL_CALL OFSStreamContainer::release()
138 throw()
140 OWeakObject::release();
143 // XTypeProvider
144 uno::Sequence< uno::Type > SAL_CALL OFSStreamContainer::getTypes()
145 throw( uno::RuntimeException )
147 if ( m_pTypeCollection == NULL )
149 ::osl::MutexGuard aGuard( m_aMutex );
151 if ( m_pTypeCollection == NULL )
153 ::cppu::OTypeCollection aTypeCollection
154 ( ::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
155 , ::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL ) );
157 if ( m_xSeekable.is() )
158 aTypeCollection = ::cppu::OTypeCollection
159 ( ::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL ),
160 aTypeCollection.getTypes() );
161 if ( m_xInputStream.is() )
162 aTypeCollection = ::cppu::OTypeCollection
163 ( ::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL ),
164 aTypeCollection.getTypes() );
166 if ( m_xOutputStream.is() )
167 aTypeCollection = ::cppu::OTypeCollection
168 ( ::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL ),
169 aTypeCollection.getTypes() );
170 if ( m_xTruncate.is() )
171 aTypeCollection = ::cppu::OTypeCollection
172 ( ::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL ),
173 aTypeCollection.getTypes() );
174 if ( m_xAsyncOutputMonitor.is() )
175 aTypeCollection = ::cppu::OTypeCollection
176 ( ::getCppuType( ( const uno::Reference< io::XAsyncOutputMonitor >* )NULL ),
177 aTypeCollection.getTypes() );
179 m_pTypeCollection = new ::cppu::OTypeCollection( aTypeCollection );
182 return m_pTypeCollection->getTypes() ;
185 uno::Sequence< sal_Int8 > SAL_CALL OFSStreamContainer::getImplementationId()
186 throw( uno::RuntimeException )
188 static ::cppu::OImplementationId* pID = NULL ;
190 if ( pID == NULL )
192 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;
194 if ( pID == NULL )
196 static ::cppu::OImplementationId aID( sal_False ) ;
197 pID = &aID ;
201 return pID->getImplementationId() ;
204 // XStream
205 uno::Reference< io::XInputStream > SAL_CALL OFSStreamContainer::getInputStream()
206 throw ( uno::RuntimeException )
208 ::osl::MutexGuard aGuard( m_aMutex );
210 if ( m_bDisposed )
211 throw lang::DisposedException();
213 if ( !m_xStream.is() )
214 throw uno::RuntimeException();
216 if ( m_xInputStream.is() )
217 return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ) );
219 return uno::Reference< io::XInputStream >();
222 uno::Reference< io::XOutputStream > SAL_CALL OFSStreamContainer::getOutputStream()
223 throw ( uno::RuntimeException )
225 ::osl::MutexGuard aGuard( m_aMutex );
227 if ( m_bDisposed )
228 throw lang::DisposedException();
230 if ( !m_xStream.is() )
231 throw uno::RuntimeException();
233 if ( m_xOutputStream.is() )
234 return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ) );
236 return uno::Reference< io::XOutputStream >();
239 // XComponent
240 void SAL_CALL OFSStreamContainer::dispose()
241 throw ( uno::RuntimeException )
243 ::osl::MutexGuard aGuard( m_aMutex );
245 if ( m_bDisposed )
246 throw lang::DisposedException();
248 if ( !m_xStream.is() )
249 throw uno::RuntimeException();
251 if ( m_xInputStream.is() && !m_bInputClosed )
253 m_xInputStream->closeInput();
254 m_bInputClosed = true;
257 if ( m_xOutputStream.is() && !m_bOutputClosed )
259 m_xOutputStream->closeOutput();
260 m_bOutputClosed = true;
263 if ( m_pListenersContainer )
265 lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
266 m_pListenersContainer->disposeAndClear( aSource );
269 m_bDisposed = true;
272 void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
273 throw ( uno::RuntimeException )
275 ::osl::MutexGuard aGuard( m_aMutex );
277 if ( m_bDisposed )
278 throw lang::DisposedException();
280 if ( !m_pListenersContainer )
281 m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
283 m_pListenersContainer->addInterface( xListener );
286 void SAL_CALL OFSStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
287 throw ( uno::RuntimeException )
289 ::osl::MutexGuard aGuard( m_aMutex );
291 if ( m_bDisposed )
292 throw lang::DisposedException();
294 if ( m_pListenersContainer )
295 m_pListenersContainer->removeInterface( xListener );
299 // XSeekable
300 void SAL_CALL OFSStreamContainer::seek( sal_Int64 location )
301 throw ( lang::IllegalArgumentException,
302 io::IOException,
303 uno::RuntimeException )
305 ::osl::MutexGuard aGuard( m_aMutex );
307 if ( m_bDisposed )
308 throw lang::DisposedException();
310 if ( !m_xStream.is() || !m_xSeekable.is() )
311 throw uno::RuntimeException();
313 m_xSeekable->seek( location );
316 sal_Int64 SAL_CALL OFSStreamContainer::getPosition()
317 throw ( io::IOException,
318 uno::RuntimeException )
320 ::osl::MutexGuard aGuard( m_aMutex );
322 if ( m_bDisposed )
323 throw lang::DisposedException();
325 if ( !m_xStream.is() || !m_xSeekable.is() )
326 throw uno::RuntimeException();
328 return m_xSeekable->getPosition();
331 sal_Int64 SAL_CALL OFSStreamContainer::getLength()
332 throw ( io::IOException,
333 uno::RuntimeException )
335 ::osl::MutexGuard aGuard( m_aMutex );
337 if ( m_bDisposed )
338 throw lang::DisposedException();
340 if ( !m_xStream.is() || !m_xSeekable.is() )
341 throw uno::RuntimeException();
343 return m_xSeekable->getLength();
347 // XInputStream
348 sal_Int32 SAL_CALL OFSStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
349 throw( io::NotConnectedException,
350 io::BufferSizeExceededException,
351 io::IOException,
352 uno::RuntimeException )
354 ::osl::MutexGuard aGuard( m_aMutex );
356 if ( m_bDisposed )
357 throw lang::DisposedException();
359 if ( !m_xStream.is() || !m_xInputStream.is() )
360 throw uno::RuntimeException();
362 return m_xInputStream->readBytes( aData, nBytesToRead );
365 sal_Int32 SAL_CALL OFSStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
366 throw( io::NotConnectedException,
367 io::BufferSizeExceededException,
368 io::IOException,
369 uno::RuntimeException )
371 ::osl::MutexGuard aGuard( m_aMutex );
373 if ( m_bDisposed )
374 throw lang::DisposedException();
376 if ( !m_xStream.is() || !m_xInputStream.is() )
377 throw uno::RuntimeException();
379 return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
382 void SAL_CALL OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
383 throw( io::NotConnectedException,
384 io::BufferSizeExceededException,
385 io::IOException,
386 uno::RuntimeException )
388 ::osl::MutexGuard aGuard( m_aMutex );
390 if ( m_bDisposed )
391 throw lang::DisposedException();
393 if ( !m_xStream.is() || !m_xInputStream.is() )
394 throw uno::RuntimeException();
396 m_xInputStream->skipBytes( nBytesToSkip );
399 sal_Int32 SAL_CALL OFSStreamContainer::available()
400 throw( io::NotConnectedException,
401 io::IOException,
402 uno::RuntimeException )
404 ::osl::MutexGuard aGuard( m_aMutex );
406 if ( m_bDisposed )
407 throw lang::DisposedException();
409 if ( !m_xStream.is() || !m_xInputStream.is() )
410 throw uno::RuntimeException();
412 return m_xInputStream->available();
415 void SAL_CALL OFSStreamContainer::closeInput()
416 throw( io::NotConnectedException,
417 io::IOException,
418 uno::RuntimeException )
420 ::osl::MutexGuard aGuard( m_aMutex );
422 if ( m_bDisposed )
423 throw lang::DisposedException();
425 if ( !m_xStream.is() || !m_xInputStream.is() )
426 throw uno::RuntimeException();
428 if ( m_xInputStream.is() )
430 m_xInputStream->closeInput();
431 m_bInputClosed = true;
434 if ( m_bOutputClosed )
435 dispose();
438 // XOutputStream
439 void SAL_CALL OFSStreamContainer::writeBytes( const uno::Sequence< sal_Int8 >& aData )
440 throw ( io::NotConnectedException,
441 io::BufferSizeExceededException,
442 io::IOException,
443 uno::RuntimeException )
445 ::osl::MutexGuard aGuard( m_aMutex );
447 if ( m_bDisposed )
448 throw lang::DisposedException();
450 if ( !m_xStream.is() || !m_xOutputStream.is() )
451 throw uno::RuntimeException();
453 return m_xOutputStream->writeBytes( aData );
456 void SAL_CALL OFSStreamContainer::flush()
457 throw ( io::NotConnectedException,
458 io::BufferSizeExceededException,
459 io::IOException,
460 uno::RuntimeException )
462 ::osl::MutexGuard aGuard( m_aMutex );
464 if ( m_bDisposed )
465 throw lang::DisposedException();
467 if ( !m_xStream.is() || !m_xOutputStream.is() )
468 throw uno::RuntimeException();
470 return m_xOutputStream->flush();
473 void SAL_CALL OFSStreamContainer::closeOutput()
474 throw ( io::NotConnectedException,
475 io::BufferSizeExceededException,
476 io::IOException,
477 uno::RuntimeException )
479 ::osl::MutexGuard aGuard( m_aMutex );
481 if ( m_bDisposed )
482 throw lang::DisposedException();
484 if ( !m_xStream.is() || !m_xOutputStream.is() )
485 throw uno::RuntimeException();
487 if ( m_xOutputStream.is() )
489 m_xOutputStream->closeOutput();
490 m_bOutputClosed = true;
493 if ( m_bInputClosed )
494 dispose();
498 // XTruncate
499 void SAL_CALL OFSStreamContainer::truncate()
500 throw ( io::IOException,
501 uno::RuntimeException )
503 ::osl::MutexGuard aGuard( m_aMutex );
505 if ( m_bDisposed )
506 throw lang::DisposedException();
508 if ( !m_xStream.is() || !m_xTruncate.is() )
509 throw uno::RuntimeException();
511 m_xTruncate->truncate();
515 // XAsyncOutputMonitor
516 void SAL_CALL OFSStreamContainer::waitForCompletion()
517 throw ( io::IOException,
518 uno::RuntimeException )
520 ::osl::MutexGuard aGuard( m_aMutex );
522 if ( m_bDisposed )
523 throw lang::DisposedException();
525 if ( !m_xStream.is() || !m_xAsyncOutputMonitor.is() )
526 throw uno::RuntimeException();
528 m_xAsyncOutputMonitor->waitForCompletion();
533 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */