UrlOrStream< STREAM > Class Template Reference IO

#include <url.h>

详细描述

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);

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) { iferr_scope ; InputStreamRef& s = input.GetStream() iferr_return ; // load data from s; input.Close() iferr_return ; // important: write input.Close(), not s.Close() return OK ; }

公共成员函数

  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 Url GetUrl () const
Result < void >  关闭 ()

Private Attributes

Url   _url
ObjectRef  _stream
Result < void >  _error
OPENSTREAMFLAGS   _flags

构造函数 & 析构函数文档编制

◆  UrlOrStream() [1/5]

UrlOrStream ( )
default

◆  UrlOrStream() [2/5]

MAXON_IMPLICIT UrlOrStream ( const Url url ,
OPENSTREAMFLAGS   flags = OPENSTREAMFLAGS::NONE  
)

◆  UrlOrStream() [3/5]

MAXON_IMPLICIT UrlOrStream ( const STREAM &  stream )

◆  UrlOrStream() [4/5]

MAXON_IMPLICIT UrlOrStream ( const Result < Url > &  url ,
OPENSTREAMFLAGS   flags = OPENSTREAMFLAGS::NONE  
)

◆  UrlOrStream() [5/5]

MAXON_IMPLICIT UrlOrStream ( const Result < STREAM > &  stream )

成员函数文档编制

◆  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.

返回
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.

返回
OK on success.

Member Data Documentation

◆  _url

Url _url
private

◆  _stream

ObjectRef _stream private

◆  _error

Result <void> _error
private

◆  _flags

OPENSTREAMFLAGS _flags
private
maxon::OK
return OK
定义: apibase.h:2532
iferr_return
#define iferr_return
定义: resultbase.h:1434
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
maxon::UrlOrInputStream
UrlOrStream< InputStreamRef > UrlOrInputStream
定义: iostreams.h:20