#include <streamconversion_helper.h>
详细描述
template<typename SRCTYPE, typename DSTTYPE>
class maxon::StreamConversionHelper< SRCTYPE, DSTTYPE >
This class implements a helper function for sequential access when using stream conversions Usually with stream conversions you get an arbitrary number of bytes within the ConvertImpl callback. This means you cannot longer read data in a classic linear fashion since you might not get enough bytes to finish your reading.
For example something simple like
ReadBytes(buf, 4)
is not possible whithout checking for the remaning bytes left. In case of insufficient number of available bytes you have to store your current variable heap and continue reading in subsequent calls of ConvertImpl.
While this might be possible for simple algorithms it can get quite complex or even imposible when trying to read more elaborate data files which are for example arranged in hierarchies with chunks or subchunks.
Or
think about using an external complex library for accessing a data format like Alembic or TIFF. In this case you can not change the code structure - only replacing a ReadBytes access is possible.
StreamConversionHelper
frees you from dealing with stream bytes deliverd in arbitrary chunks. First you simply overlad
StreamConversionHelper
template by specifying your source and dest datatype (here both Char) and fill the DoIt routine with your normal code e.g. reading the file (in this example decompressing a LZ4 stream)
class
Lz4StreamConversionHelper :
public
StreamConversionHelper<Char, Char>
{
public
:
virtual
Result<void>
DoIt
()
{
// ... read your data here
Lz4Decompress();
}
};
In this example if you want to read 4 bytes (aka Chars) within the DoIt routine you simply write
Char
buf[4];
读取
(buf, 4)
iferr_return
;
Second you put the helper object somewhere on the stack - ideally as a static variable in your main StreamConversion class and initialize it everytime ConvertImpl is called by simply calling AppendStream - boom you are done.
class
Lz4BaseImpl :
public
组件
<Lz4BaseImpl, StreamConversionInterface>
{
MAXON_COMPONENT
();
public
:
Result<Int> ConvertImpl(
const
Block<const Generic>& xsrc, WritableArrayInterface<Generic>& xdst,
Int
dstLimitHint,
Bool
inputFinished,
Bool
& outputFinished)
{
iferr_scope
;
const
Block<const Char>&
src
=
reinterpret_cast<
const
Block<const Char>&
>
(xsrc);
WritableArrayInterface<Char>& dst =
reinterpret_cast<
WritableArrayInterface<Char>&
>
(xdst);
helper.AppendStream(
src
, dst, inputFinished)
iferr_return
;
if
(inputFinished)
outputFinished =
true
;
return
helper.GetCurrentPosition();
}
protected
:
Lz4StreamConversionHelper helper;
};
Friends
|
template<typename SRCTYPE2 , typename DSTTYPE2 >
|
class
|
CollectorThread
|
构造函数 & 析构函数文档编制
◆
~StreamConversionHelper()
成员函数文档编制
◆
AppendStream()
Adds a new part of the stream to the existing one. Has to be called each time ConvertImpl is executed
-
参数
-
[in]
|
src
|
input stream
|
[in]
|
dst
|
output stream
|
[in]
|
inputFinished
|
signal from ConvertImpl if this is the last part of the stream
|
Example: You put the helper object somewhere on the stack - ideally as a static variable in your main StreamConversion class and initialize it everytime ConvertImpl is called by simply calling AppendStream. Here a LZ4 stream of Chars as input is decoded.
class
Lz4BaseImpl :
public
组件
<Lz4BaseImpl, StreamConversionInterface>
{
MAXON_COMPONENT
();
public
:
Result<Int> ConvertImpl(
const
Block<const Generic>& xsrc, WritableArrayInterface<Generic>& xdst,
Int
dstLimitHint,
Bool
inputFinished,
Bool
& outputFinished)
{
iferr_scope
;
const
Block<const Char>&
src
=
reinterpret_cast<
const
Block<const Char>&
>
(xsrc);
WritableArrayInterface<Char>& dst =
reinterpret_cast<
WritableArrayInterface<Char>&
>
(xdst);
helper.AppendStream(
src
, dst, inputFinished)
iferr_return
;
if
(inputFinished)
outputFinished =
true
;
return
helper.GetCurrentPosition();
}
protected
:
Lz4StreamConversionHelper helper;
};
◆
Write()
Append elements to the internal destination dst buffer specified in AppendStream.
-
参数
-
[in]
|
buf
|
source buffer
|
[in]
|
count
|
number of elements to be copied from buf to dst
|
◆
WriteWithOffset()
Simply copy elements to the internal destination dst buffer specified in AppendStream. This function is especially useful for speeding things up when the final size oft dst ist know. Instead of enlarging dst with each Write call this i just a plain Memcopy Note: you to have to garantue that the dest buffer is sufficiently large enough
-
参数
-
[in]
|
buf
|
source buffer
|
[in]
|
count
|
number of elements to be copied from buf to dst
|
[in]
|
offset
|
number of elements to be copied from buf to dst
|
◆
Read()
Read elements from internal src buffer specified in AppendStream to buf.
-
参数
-
[in]
|
buf
|
destination buffer
|
[in]
|
count
|
number of elements to be copied from src to buf
|
◆
ReadEOS()
Read elements from internal src buffer specified in AppendStream to buf. Contrary to Read this routine does not read a specific number of elements but reads untile either the end of the stream is reached of the maxCount number.
-
参数
-
[in]
|
buf
|
destination buffer
|
[in]
|
maxCount
|
max number of elements to be read
|
-
返回
-
number of elements actually read
◆
Skip()
Skip elements
-
参数
-
[in]
|
count
|
number of elements
|
◆
GetCurrentPosition()
Current reading position. Note that this is a relative position to the part of the stream that was initialized with AppendStream.
-
返回
-
Current reading position
◆
GetDst()
Direct access to the destination array
-
返回
-
pointer to destination array
◆
EndOfStreamReached()
Check for end of stream condition.
-
返回
-
True if end is reached.
◆
DoIt()
virtual
Result
<void> DoIt
|
(
|
|
)
|
|
|
protected
pure virtual
|
you have to overload this function here you fill in your code to process the data of the stream
◆
SignalEmitterThreadToContinue()
◆
SignalCollectorThreadToContinue()
Friends And Related Function Documentation
◆
CollectorThread
Member Data Documentation
◆
_thread
◆
_inputAvailable
ConditionVariableRef _inputAvailable
|
private
|
◆
_outputAvailable
ConditionVariableRef _outputAvailable
|
private
|
◆
_returnErrorVar
Error _returnErrorVar
|
private
|
◆
_DoItDone
◆
_src
const
Block
<const SRCTYPE>* _src
|
|
private
|
◆
_dst
◆
_seg_pos
◆
_seg_count
◆
_inputFinished
maxon::Int Int
定义:
ge_sys_math.h:62
#define MAXON_COMPONENT(KIND,...)
定义:
objectbase.h:2036
#define iferr_return
定义:
resultbase.h:1434
virtual Result< void > DoIt()=0
const T & src
定义:
apibase.h:2525
#define iferr_scope
定义:
resultbase.h:1343
maxon::Bool Bool
定义:
ge_sys_math.h:53
maxon::Char Char
定义:
ge_sys_math.h:54
ComponentWithBase< C, ComponentRoot, INTERFACES... > Component
定义:
objectbase.h:2604
Result< void > Read(SRCTYPE *buf, Int count)
定义:
streamconversion_helper.h:339