-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbit__stream__writer_8hpp_source.html
186 lines (184 loc) · 17.7 KB
/
bit__stream__writer_8hpp_source.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>libfly: fly/types/bit_stream/bit_stream_writer.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">libfly
 <span id="projectnumber">6.2.2</span>
</div>
<div id="projectbrief">C++20 utility library for Linux, macOS, and Windows</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('bit__stream__writer_8hpp_source.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">bit_stream_writer.hpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00002"></a><span class="lineno"> 2</span>  </div>
<div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="preprocessor">#include "fly/types/bit_stream/detail/bit_stream.hpp"</span></div>
<div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="preprocessor">#include "fly/types/bit_stream/detail/concepts.hpp"</span></div>
<div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="preprocessor">#include "fly/types/bit_stream/types.hpp"</span></div>
<div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="preprocessor">#include "fly/types/numeric/endian.hpp"</span></div>
<div class="line"><a name="l00007"></a><span class="lineno"> 7</span>  </div>
<div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="preprocessor">#include <bit></span></div>
<div class="line"><a name="l00009"></a><span class="lineno"> 9</span> <span class="preprocessor">#include <ostream></span></div>
<div class="line"><a name="l00010"></a><span class="lineno"> 10</span>  </div>
<div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="keyword">namespace </span>fly {</div>
<div class="line"><a name="l00012"></a><span class="lineno"> 12</span>  </div>
<div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="classfly_1_1_bit_stream_writer.html"> 23</a></span> <span class="keyword">class </span><a class="code" href="classfly_1_1_bit_stream_writer.html">BitStreamWriter</a> : <span class="keyword">public</span> <a class="code" href="classfly_1_1detail_1_1_bit_stream.html">detail::BitStream</a></div>
<div class="line"><a name="l00024"></a><span class="lineno"> 24</span> {</div>
<div class="line"><a name="l00025"></a><span class="lineno"> 25</span> <span class="keyword">public</span>:</div>
<div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keyword">explicit</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#aac978a668121bd958d340f6f3d567ccf">BitStreamWriter</a>(std::ostream &stream) noexcept;</div>
<div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  </div>
<div class="line"><a name="l00040"></a><span class="lineno"> 40</span>  <span class="keywordtype">void</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#a8f28589dc377c9d8110cd86d82e249a6">write_word</a>(word_type word);</div>
<div class="line"><a name="l00041"></a><span class="lineno"> 41</span>  </div>
<div class="line"><a name="l00049"></a><span class="lineno"> 49</span>  <span class="keywordtype">void</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#a4c1d23024a1c07b13e32b78c52491367">write_byte</a>(byte_type <span class="keywordtype">byte</span>);</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>  </div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  <span class="keyword">template</span> <detail::BitStreamInteger DataType></div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>  <span class="keywordtype">void</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#a8adc929ff06cf2c95d7d0a2e74b83b40">write_bits</a>(DataType bits, byte_type size);</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  </div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>  <span class="keywordtype">bool</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#a3a4eae04f57794995a0c445b72073fbe">finish</a>();</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  </div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span> <span class="keyword">private</span>:</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  <span class="keywordtype">void</span> flush_header(byte_type remainder);</div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  </div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  <span class="keywordtype">void</span> flush_buffer();</div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  </div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>  <span class="keyword">template</span> <detail::BitStreamInteger DataType></div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>  <span class="keywordtype">void</span> flush(<span class="keyword">const</span> DataType &buffer, byte_type bytes);</div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>  </div>
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>  std::ostream &m_stream;</div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span> };</div>
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>  </div>
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span> <span class="comment">//==================================================================================================</span></div>
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span> <span class="keyword">template</span> <detail::BitStreamInteger DataType></div>
<div class="line"><a name="l00101"></a><span class="lineno"><a class="line" href="classfly_1_1_bit_stream_writer.html#a8adc929ff06cf2c95d7d0a2e74b83b40"> 101</a></span> <span class="keywordtype">void</span> <a class="code" href="classfly_1_1_bit_stream_writer.html#a8adc929ff06cf2c95d7d0a2e74b83b40">BitStreamWriter::write_bits</a>(DataType bits, byte_type size)</div>
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span> {</div>
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  <span class="comment">// If there are more bits to write than are available in the byte buffer, break the bits into</span></div>
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  <span class="comment">// two chunks.</span></div>
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  <span class="keywordflow">if</span> (size > m_position)</div>
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  {</div>
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  <span class="keyword">const</span> byte_type rshift = size - m_position;</div>
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  </div>
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  <span class="comment">// Fill the remainder of the byte buffer with as many bits as are available, and flush it</span></div>
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  <span class="comment">// onto the stream.</span></div>
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  m_buffer |= <span class="keyword">static_cast<</span>buffer_type<span class="keyword">></span>(bits) >> rshift;</div>
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  flush_buffer();</div>
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  </div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>  <span class="comment">// Then update the input bits to retain only those bits that have not been written yet.</span></div>
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>  bits &= bit_mask<DataType>(rshift);</div>
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>  size = rshift;</div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>  }</div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>  </div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>  <span class="keyword">const</span> byte_type lshift = m_position - size;</div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>  </div>
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>  m_buffer |= <span class="keyword">static_cast<</span>buffer_type<span class="keyword">></span>(bits) << lshift;</div>
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>  m_position = lshift;</div>
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span> }</div>
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>  </div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span> <span class="comment">//==================================================================================================</span></div>
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span> <span class="keyword">template</span> <detail::BitStreamInteger DataType></div>
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span> <span class="keywordtype">void</span> BitStreamWriter::flush(<span class="keyword">const</span> DataType &buffer, byte_type bytes)</div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span> {</div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>  <span class="keywordflow">if</span> (m_stream)</div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>  {</div>
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>  <span class="keyword">const</span> DataType data = endian_swap_if_non_native<std::endian::big>(buffer);</div>
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  </div>
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>  m_stream_buffer->sputn(</div>
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>  <span class="keyword">reinterpret_cast<</span><span class="keyword">const </span>std::ios::char_type *<span class="keyword">></span>(&data),</div>
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>  <span class="keyword">static_cast<</span>std::streamsize<span class="keyword">></span>(bytes));</div>
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  }</div>
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span> }</div>
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  </div>
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span> } <span class="comment">// namespace fly</span></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html">fly::BitStreamWriter</a></div><div class="ttdef"><b>Definition:</b> bit_stream_writer.hpp:24</div></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html_a3a4eae04f57794995a0c445b72073fbe"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html#a3a4eae04f57794995a0c445b72073fbe">fly::BitStreamWriter::finish</a></div><div class="ttdeci">bool finish()</div><div class="ttdef"><b>Definition:</b> bit_stream_writer.cpp:29</div></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html_a4c1d23024a1c07b13e32b78c52491367"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html#a4c1d23024a1c07b13e32b78c52491367">fly::BitStreamWriter::write_byte</a></div><div class="ttdeci">void write_byte(byte_type byte)</div><div class="ttdef"><b>Definition:</b> bit_stream_writer.cpp:23</div></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html_a8adc929ff06cf2c95d7d0a2e74b83b40"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html#a8adc929ff06cf2c95d7d0a2e74b83b40">fly::BitStreamWriter::write_bits</a></div><div class="ttdeci">void write_bits(DataType bits, byte_type size)</div><div class="ttdef"><b>Definition:</b> bit_stream_writer.hpp:101</div></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html_a8f28589dc377c9d8110cd86d82e249a6"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html#a8f28589dc377c9d8110cd86d82e249a6">fly::BitStreamWriter::write_word</a></div><div class="ttdeci">void write_word(word_type word)</div><div class="ttdef"><b>Definition:</b> bit_stream_writer.cpp:17</div></div>
<div class="ttc" id="aclassfly_1_1_bit_stream_writer_html_aac978a668121bd958d340f6f3d567ccf"><div class="ttname"><a href="classfly_1_1_bit_stream_writer.html#aac978a668121bd958d340f6f3d567ccf">fly::BitStreamWriter::BitStreamWriter</a></div><div class="ttdeci">BitStreamWriter(std::ostream &stream) noexcept</div><div class="ttdef"><b>Definition:</b> bit_stream_writer.cpp:9</div></div>
<div class="ttc" id="aclassfly_1_1detail_1_1_bit_stream_html"><div class="ttname"><a href="classfly_1_1detail_1_1_bit_stream.html">fly::detail::BitStream</a></div><div class="ttdef"><b>Definition:</b> bit_stream.hpp:34</div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="dir_4e888731a8ba874a656d519f9f1001d8.html">fly</a></li><li class="navelem"><a class="el" href="dir_666f9a7ac6e05881746e5257b80aca61.html">types</a></li><li class="navelem"><a class="el" href="dir_b7d285753531f12a3e100ba4a548ea71.html">bit_stream</a></li><li class="navelem"><b>bit_stream_writer.hpp</b></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
</ul>
</div>
</body>
</html>