![](/img/trans.png)
[英]QuickFIX C++ library - general question regarding ThreadedSocketInitiator
[英]A general question about possible future C++ language or standard library features regarding bit streams
在 C++20 以后的 C++ 未來版本中或在標准庫中,是否有任何提及具有特定的bitstream
對象? 例如; 我們可以有一個std::bitset<n>
對象,其中n
是一個常數已知整數值。
偽示例:
#include <bitstream>
template<unsigned N>
class Foo {
public:
std::bitset<N> bits_;
};
這里我們有一個類模板,它將指定std::bitset
對象中有多少位。 這很好。
我們還有stream
對象,例如iostream
、 fstream
、 sstream
等,我們可以在其中將各種數據類型的值提供給這些流,或者將它們讀取或寫入字符串流、文件流、I/O 流到控制台或終端等...
我想知道的是,有沒有像上面那樣的流對象的任何建議,但特定於類似於std::bitset
性質的任意位序列,您可以在其中將一組位輸入到流中。 例如,它可能看起來像這樣:
#include <bitset>
#include <bitstream> // not an actual library - only suggestive
int main() {
std::bitset<8> byte{0xFE};
std::bitset<16> word{0xFEDC};
std::bitset<32> dword{0xFEDCBA98};
std::bitset<64> qword{0xFEDCBA9876543210};
std::bitstream bs;
bs << byte << word << dword << qword;
std::cout << bs;
return 0;
}
這里的預期輸出是:
11111110111111101101110011111110110111001011101010011000111111101101110010111010100110000111011001010100001100100001000
根據預期用途的字節序表示法...
逆轉也適用。 如果我們有一個已經填充的bitstream
對象,就像上面例子中的那個,我們可以從bitstream
提取特定的比特序列,如下所示:
#include <bitset>
#include <bitstream> // not an actual library - only suggestive
#include <iomanip> // manipulators for bitstreams not yet implemented...
int main() {
std::bitset<8> byte{0xFE};
std::bitset<16> word{0xFEDC};
std::bitset<32> dword{0xFEDCBA98};
std::bitset<64> qword{0xFEDCBA9876543210};
std::bitstream bs;
bs << byte << word << dword << qword;
std::bitset<3> odd3;
std::bitset<5> odd5;
std::bitset<7> odd7;
bs >> bs::msb >> odd3 >> odd7 >> odd5;
std::cout << odd3 << '\n' << odd7 << '\n' << odd5;
bs >> bs::lsb >> odd5 << odd3 << odd7;
std::cout << odd5 << '\n' << odd3 << '\n' << odd7;
// Or by using a constructor with a value for the bit size...
std::bitset<9> odd9( bs ); // odd9(bs::msb), odd9(bs::lsb)...
return 0;
}
並且預期的結果將分別從 MSB 或 LSB 方向按bitset
對象的大小量拉取位。
就像iostream
和其他流庫一樣,通過使用<iomanip>
庫,我們可以擁有格式化修飾符,將這個輸入或輸出分解為任何指定長度的離散bit-segments
,例如每 4 位或 8 位,或將其顯示為十六進制,等等......所有的位操作和計算仍然是std::bitset
的責任。
如果能夠推動或檢索任意bitset
和縮小的對象bitstream
對象,將與所有其他的兼容stream
狀物體可能是一個非常強大的和有用的工具或功能。
有沒有提到類似的東西,或者是否已經存在類似於這種性質的東西,而不必在字符串或其他類型之間來回轉換等等?
編輯-User- walnut
在他的回答中給出了一些很好的例子,並且已經可以使用現有的iostream
對象和運算符來完成。 您可以閱讀他的回答和討論中的評論,但是,我只是想說明我stream
對象庫的推理,該庫被編寫和設計為顯式和專門用於bitset
數據類型。
想象一個這樣的函數:
// Not written with perfect syntax or compilation in mind,
// only psuedo to illustrate a point.
template<typename... T>
std::ostream& concatinateBitSequences( T&& ... t ) { // assuming all t are bitset<n>
std::stringstream sstream;
sstream << t...;
return sstream;
}
並比較...
// Not written with perfect syntax or compilation in mind,
// only psuedo to illustrate a point.
template<typename... T>
std::bitstream& concatinateBitSequences( T&& ... t ) { // assuming all t are bitset<n>
std::bitstream bs;
bs << t...;
return bs;
}
調用者,用戶將知道它顯式或專門返回一個bitstream
,與任意stream
對象進行比較! 這是我的主要意圖或目標......它更適合可讀性,並在顯示正確意圖的代碼中更具表現力。 此外,它將與bitset
對象緊密耦合,這意味着bitstream
對象將只接受來自bitset
s 或其他bitstream
對象的數據,但它可以將其數據傳遞給bitset
s、 bitstream
s 或其他stream
對象。
這已經如您所願:
std::cout << byte << word << dword << qword;
和
std::cin >> odd5 >> odd3 >> odd7;
對於中間存儲,可以簡單地使用std::stringstream
,它也可以按照您的描述工作。
然而沒有字節序,因為操作是按位的,而不是按字節的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.