1 // Copyright (c) 2021 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _OSD_CachedFileSystem_HeaderFile
15 #define _OSD_CachedFileSystem_HeaderFile
17 #include <OSD_FileSystem.hxx>
19 //! File system keeping last stream created by linked file system (OSD_FileSystem::DefaultFileSystem() by default) to be reused for opening a stream with the same URL.
20 //! Note that as file is kept in opened state, application will need destroying this object to ensure all files being closed.
21 //! This interface could be handy in context of reading numerous objects pointing to the same file (at different offset).
22 //! Make sure to create a dedicated OSD_CachedFileSystem for each working thread to avoid data races.
23 class OSD_CachedFileSystem : public OSD_FileSystem
25 DEFINE_STANDARD_RTTIEXT(OSD_CachedFileSystem, OSD_FileSystem)
29 Standard_EXPORT OSD_CachedFileSystem (const Handle(OSD_FileSystem)& theLinkedFileSystem = Handle(OSD_FileSystem)());
31 //! Return linked file system; initialized with OSD_FileSystem::DefaultFileSystem() by default.
32 const Handle(OSD_FileSystem)& LinkedFileSystem() const { return myLinkedFS; }
34 //! Sets linked file system.
35 void SetLinkedFileSystem (const Handle(OSD_FileSystem)& theLinkedFileSystem) { myLinkedFS = theLinkedFileSystem; }
37 //! Returns TRUE if URL defines a supported protocol.
38 Standard_EXPORT virtual Standard_Boolean IsSupportedPath (const TCollection_AsciiString& theUrl) const Standard_OVERRIDE;
40 //! Returns TRUE if current input stream is opened for reading operations.
41 Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const std::shared_ptr<std::istream>& theStream) const Standard_OVERRIDE;
43 //! Returns TRUE if current output stream is opened for writing operations.
44 Standard_EXPORT virtual Standard_Boolean IsOpenOStream (const std::shared_ptr<std::ostream>& theStream) const Standard_OVERRIDE;
46 //! Opens stream for specified file URL for reading operations or returns previously created stream pointing to the same URL.
47 Standard_EXPORT virtual std::shared_ptr<std::istream> OpenIStream
48 (const TCollection_AsciiString& theUrl,
49 const std::ios_base::openmode theParams,
50 const int64_t theOffset,
51 const std::shared_ptr<std::istream>& theOldStream) Standard_OVERRIDE;
53 //! Opens stream for specified file URL for writing operations (std::ostream) by calling parent's method.
54 Standard_EXPORT virtual std::shared_ptr<std::ostream> OpenOStream (const TCollection_AsciiString& theUrl,
55 const std::ios_base::openmode theMode) Standard_OVERRIDE;
57 //! Opens stream buffer for specified file URL.
58 Standard_EXPORT virtual std::shared_ptr<std::streambuf> OpenStreamBuffer
59 (const TCollection_AsciiString& theUrl,
60 const std::ios_base::openmode theMode,
61 const int64_t theOffset = 0,
62 int64_t* theOutBufSize = NULL) Standard_OVERRIDE;
66 // Auxiliary structure to save shared stream with path to it.
67 struct OSD_CachedStream
69 TCollection_AsciiString Url;
70 std::shared_ptr<std::istream> Stream;
71 std::shared_ptr<std::streambuf> StreamBuf;
82 OSD_CachedStream myStream; //!< active cached stream
83 Handle(OSD_FileSystem) myLinkedFS; //!< linked file system to open files
87 #endif // _OSD_CachedFileSystem_HeaderFile