Hide menu
Loading...
Searching...
No Matches
cadex::Base_StreamProvider< T > Class Template Referenceabstract

Base template class for user-defined stream providers that can be used during file parsing or formatting. More...

#include <cadex/Base_FileStreamProvider.hxx>

Inherited by cadex::internal::Base_IFileStreamProvider.

Public Member Functions

virtual ~Base_StreamProvider ()
 Destructor.
 
virtual bool IsOpen () const =0
 Returns true if the underlying stream has been successfully open and is available for access.
 
virtual T & Stream ()=0
 Returns underlying stream.
 
virtual void Open (const Base_UTF16String &theFileName)=0
 Opens the stream per its file name.
 
virtual void Close ()=0
 Closes the stream.
 

Detailed Description

template<typename T>
class cadex::Base_StreamProvider< T >

Base template class for user-defined stream providers that can be used during file parsing or formatting.

An instance of provider is created by Base_StreamProviderFactory which is supplied into Base_Reader::ReadFile().

Template instantiation Base_IStreamProvider instantiates a provider for input streams (std::istream).

Provider's lifespan can be large enough so the stream must remain valid throughout lifespan of the provider that provides it. The stream may be closed and reopened again during life-span. Thus, it should not be closed earlier than in the destructor.

The redefined virtual methods may be called by CAD Exchanger SDK once or multiple times for every file name, custom implementation should not make any assumptions on the number or timing of calls.

Subclasses should provide specific implementation to encapsulate stream objects (in the form of std::istream) for a particular data storage (e.g. cloud). This approach can be specifically useful when working with files with external references (such as JT) residing in specific data storage.

Below is a sample implementation:

class CloudStreamProvider : public Base_IStreamProvider
{
public:
CloudIStreamProvider() {}
CloudIStreamProvider (const Base_UTF16String& theFileName) : myAccessor (theFileName) {}
virtual bool IsOpen() const override
{ return myAccessor.IsOpen(); }
virtual std::istream& Stream() override
{ return myAccessor.Stream(); }
virtual void Open (const Base_UTF16String& theFileName) override
{ myAccessor.Open (theFileName); }
virtual void Close() override
{ myAccessor.Closed(); }
private:
//accessor for specific data storage
MyStorageAccessor myAccessor;
};
class CloudIStreamProviderFactory : public Base_IStreamProviderFactory
{
public:
virtual std::shared_ptr<StreamProvider> Create (const Base_UTF16String& theFileName) override
{
return std::shared_ptr<Base_StreamProvider> (new CloudStreamProvider (theFileName));
};
virtual std::shared_ptr<StreamProvider> Create() override
{
return std::shared_ptr<Base_StreamProvider> (new CloudStreamProvider());
};
};
JT_Reader aReader;
aReader.ReadFile (std::make_shared<CloudIStreamProviderFactory>(), "/custom_path/assembly.jt");
Base template class for user-defined stream providers that can be used during file parsing or formatt...
Definition: Base_StreamProvider.hxx:32
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Base_StreamProviderFactory< std::istream > Base_IStreamProviderFactory
Instantiates Base_StreamProviderFactory for std::istream.
Definition: Base_StreamProvider.hxx:54
See also
Base_StreamProviderFactory, Base_IStreamProvider.

Constructor & Destructor Documentation

◆ ~Base_StreamProvider()

template<typename T >
cadex::Base_StreamProvider< T >::~Base_StreamProvider ( )
inlinevirtual

Destructor.

Subclasses should typically close the stream here (e.g. by destroying an object wrapping the stream).

Member Function Documentation

◆ Close()

template<typename T >
void cadex::Base_StreamProvider< T >::Close ( )
pure virtual

Closes the stream.

This method will only be called if the stream needs to be closed (e.g. to reduce the number of simultaneously open files in Operating System) but the stream itself could be used later on. This may happen for instance when parsing JT files when each JT file contains multiple data segments which will be parsed at different moments on 'as needed' basis.

See also
Open().

◆ IsOpen()

template<typename T >
bool cadex::Base_StreamProvider< T >::IsOpen ( ) const
pure virtual

Returns true if the underlying stream has been successfully open and is available for access.

This method is guaranteed to be called before Stream().

◆ Open()

template<typename T >
void cadex::Base_StreamProvider< T >::Open ( const Base_UTF16String theFileName)
pure virtual

Opens the stream per its file name.

This method will only be called if either the provider has been created by the factory without specified file name or if the stream has been closed with the Close() method.

◆ Stream()

template<typename T >
T & cadex::Base_StreamProvider< T >::Stream ( )
pure virtual

Returns underlying stream.

This method will only be called if IsOpen() returned true.