#include <url.h>
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 |
|
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 | ( | ) |
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.
const Url & GetUrl | ( | ) | const |
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.
|
private |
ObjectRef _stream | private |
|
private |
|
private |