UrlOrStream< STREAM > Class Template Reference

#include <url.h>

Detailed Description

template<typename STREAM>
class maxon::UrlOrStream< STREAM >

This helper class template can be used for function parameters where the function needs to get a stream, but for convenience also passing a Url as argument to the parameter shall be allowed. In other words, a function

Result<void> LoadSomething(UrlOrInputStream&& input);
UrlOrStream< InputStreamRef > UrlOrInputStream
Definition: iostreams.h:20

can be invoked by passing either a Url or an InputStreamRef to the parameter. It even allows to pass Result<Url> or Result<InputStreamRef>, then the error, if any, will be stored within UrlOrStream and returned by the call to GetStream.

In the implementation of a function with a UrlOrStream parameter, GetStream has to be called to obtain the stream, and finally Close to close the stream (but this will only be done if the stream was obtained from a Url):

Result<void> LoadSomething(UrlOrInputStream&& input)
{
InputStreamRef& s = input.GetStream() iferr_return;
// load data from s;
input.Close() iferr_return; // important: write input.Close(), not s.Close()
return OK;
}
PyObject PyObject const char const char char ** s
Definition: bytesobject.h:60
return OK
Definition: apibase.h:2735
#define iferr_scope
Definition: resultbase.h:1389
#define iferr_return
Definition: resultbase.h:1524

Public Member Functions

 UrlOrStream ()=default
 
MAXON_IMPLICIT UrlOrStream (const Url &url, OPENSTREAMFLAGS flags=OPENSTREAMFLAGS::NONE)
 
MAXON_IMPLICIT UrlOrStream (const STREAM &stream)
 
MAXON_IMPLICIT UrlOrStream (const Result< Url > &url, OPENSTREAMFLAGS flags=OPENSTREAMFLAGS::NONE)
 
MAXON_IMPLICIT UrlOrStream (const Result< STREAM > &stream)
 
Result< STREAM & > GetStream ()
 
const UrlGetUrl () const
 
Result< void > Close ()
 

Private Attributes

Url _url
 
ObjectRef _stream
 
Result< void > _error
 
OPENSTREAMFLAGS _flags
 

Constructor & Destructor Documentation

◆ UrlOrStream() [1/5]

UrlOrStream ( )
default

◆ UrlOrStream() [2/5]

◆ UrlOrStream() [3/5]

MAXON_IMPLICIT UrlOrStream ( const STREAM &  stream)

◆ UrlOrStream() [4/5]

◆ UrlOrStream() [5/5]

MAXON_IMPLICIT UrlOrStream ( const Result< STREAM > &  stream)

Member Function Documentation

◆ GetStream()

Result<STREAM&> GetStream ( )

Returns the stream to use. If this UrlOrStream was constructed with a Url and a stream hasn't yet been opened, a new stream is opened.

Returns
The stream to use.

◆ GetUrl()

const Url& GetUrl ( ) const

◆ Close()

Result<void> Close ( )

Close the underlying stream if is has been opened by this UrlOrStream. You have to call this function at the end of the code which has obtained the stream by GetStream(). The stream is closed only if this UrlOrStream was constructed by a Url, but not if is was constructed by a stream.

Returns
OK on success.

Member Data Documentation

◆ _url

Url _url
private

◆ _stream

ObjectRef _stream
private

◆ _error

Result<void> _error
private

◆ _flags

OPENSTREAMFLAGS _flags
private