Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:18:45

0001 // Copyright (c) 2021 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _OSD_CachedFileSystem_HeaderFile
0015 #define _OSD_CachedFileSystem_HeaderFile
0016 
0017 #include <OSD_FileSystem.hxx>
0018 
0019 //! File system keeping last stream created by linked file system
0020 //! (OSD_FileSystem::DefaultFileSystem() by default) to be reused for opening a stream with the same
0021 //! URL. Note that as file is kept in opened state, application will need destroying this object to
0022 //! ensure all files being closed. This interface could be handy in context of reading numerous
0023 //! objects pointing to the same file (at different offset). Make sure to create a dedicated
0024 //! OSD_CachedFileSystem for each working thread to avoid data races.
0025 class OSD_CachedFileSystem : public OSD_FileSystem
0026 {
0027   DEFINE_STANDARD_RTTIEXT(OSD_CachedFileSystem, OSD_FileSystem)
0028 public:
0029   //! Constructor.
0030   Standard_EXPORT OSD_CachedFileSystem(
0031     const occ::handle<OSD_FileSystem>& theLinkedFileSystem = occ::handle<OSD_FileSystem>());
0032 
0033   //! Return linked file system; initialized with OSD_FileSystem::DefaultFileSystem() by default.
0034   const occ::handle<OSD_FileSystem>& LinkedFileSystem() const { return myLinkedFS; }
0035 
0036   //! Sets linked file system.
0037   void SetLinkedFileSystem(const occ::handle<OSD_FileSystem>& theLinkedFileSystem)
0038   {
0039     myLinkedFS = theLinkedFileSystem;
0040   }
0041 
0042   //! Returns TRUE if URL defines a supported protocol.
0043   Standard_EXPORT bool IsSupportedPath(const TCollection_AsciiString& theUrl) const override;
0044 
0045   //! Returns TRUE if current input stream is opened for reading operations.
0046   Standard_EXPORT bool IsOpenIStream(const std::shared_ptr<std::istream>& theStream) const override;
0047 
0048   //! Returns TRUE if current output stream is opened for writing operations.
0049   Standard_EXPORT bool IsOpenOStream(const std::shared_ptr<std::ostream>& theStream) const override;
0050 
0051   //! Opens stream for specified file URL for reading operations or returns previously created
0052   //! stream pointing to the same URL.
0053   Standard_EXPORT std::shared_ptr<std::istream> OpenIStream(
0054     const TCollection_AsciiString&       theUrl,
0055     const std::ios_base::openmode        theParams,
0056     const int64_t                        theOffset,
0057     const std::shared_ptr<std::istream>& theOldStream) override;
0058 
0059   //! Opens stream for specified file URL for writing operations (std::ostream) by calling parent's
0060   //! method.
0061   Standard_EXPORT std::shared_ptr<std::ostream> OpenOStream(
0062     const TCollection_AsciiString& theUrl,
0063     const std::ios_base::openmode  theMode) override;
0064 
0065   //! Opens stream buffer for specified file URL.
0066   Standard_EXPORT std::shared_ptr<std::streambuf> OpenStreamBuffer(
0067     const TCollection_AsciiString& theUrl,
0068     const std::ios_base::openmode  theMode,
0069     const int64_t                  theOffset     = 0,
0070     int64_t*                       theOutBufSize = nullptr) override;
0071 
0072 protected:
0073   // Auxiliary structure to save shared stream with path to it.
0074   struct OSD_CachedStream
0075   {
0076     TCollection_AsciiString         Url;
0077     std::shared_ptr<std::istream>   Stream;
0078     std::shared_ptr<std::streambuf> StreamBuf;
0079 
0080     void Reset()
0081     {
0082       Stream.reset();
0083       StreamBuf.reset();
0084     }
0085   };
0086 
0087 protected:
0088   OSD_CachedStream            myStream;   //!< active cached stream
0089   occ::handle<OSD_FileSystem> myLinkedFS; //!< linked file system to open files
0090 };
0091 
0092 #endif // _OSD_CachedFileSystem_HeaderFile