Skip to content
Snippets Groups Projects
Commit 07c2f381 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by operator for beamline
Browse files

test_bitset: second version

parent 538f3e8d
No related branches found
No related tags found
No related merge requests found
......@@ -15,54 +15,46 @@ struct sls_detector_header {
#define PMASK_ULONGS \
(MAX_NUM_PACKETS / PMASK_ULONG_BITS)
typedef struct {
sls_detector_header detHeader;
typedef std::bitset<MAX_NUM_PACKETS> sls_receiver_packets_mask;
typedef unsigned long sls_receiver_packets_mask_storage[PMASK_ULONGS];
typedef std::bitset<MAX_NUM_PACKETS> packets_mask;
packets_mask packetsMask;
typedef unsigned long packets_mask_block[PMASK_ULONGS];
void dumpPacketsMask(packets_mask_block& b) const;
void loadPacketsMask(const packets_mask_block& b);
} sls_receiver_header;
inline
void sls_receiver_header::dumpPacketsMask(packets_mask_block& b) const
inline void operator <<(sls_receiver_packets_mask_storage& pm_storage,
const sls_receiver_packets_mask& pm)
{
packets_mask aux = packetsMask, mask = (unsigned long)(-1);
sls_receiver_packets_mask aux = pm, mask = (unsigned long)(-1);
for (unsigned int i = 0; i < PMASK_ULONGS; ++i) {
b[i] = (aux & mask).to_ulong();
pm_storage[i] = (aux & mask).to_ulong();
aux >>= PMASK_ULONG_BITS;
}
}
inline
void sls_receiver_header::loadPacketsMask(const packets_mask_block& b)
inline void operator >>(const sls_receiver_packets_mask_storage& pm_storage,
sls_receiver_packets_mask& pm)
{
packetsMask.reset();
pm.reset();
for (unsigned int i = 0; i < PMASK_ULONGS; ++i) {
packets_mask aux = b[i];
packetsMask |= aux << PMASK_ULONG_BITS * i;
sls_receiver_packets_mask aux = pm_storage[i];
pm |= aux << (PMASK_ULONG_BITS * i);
}
}
inline
std::ostream& operator <<(std::ostream& os,
const sls_receiver_header::packets_mask_block& b)
const sls_receiver_packets_mask_storage& pm_storage)
{
os << std::hex << std::noshowbase << std::setfill('0');
const unsigned long *p = b + PMASK_ULONGS;
while (--p >= b)
const unsigned long *p = pm_storage + PMASK_ULONGS;
while (--p >= pm_storage)
os << std::setw(PMASK_ULONG_HEX_DIGITS) << *p;
return os << std::dec << std::showbase << std::setfill(' ');
}
inline
std::istream& operator >>(std::istream& is,
sls_receiver_header::packets_mask_block& b)
sls_receiver_packets_mask_storage& pm_storage)
{
unsigned long *p = b + PMASK_ULONGS;
while (--p >= b) {
unsigned long *p = pm_storage + PMASK_ULONGS;
while (--p >= pm_storage) {
char s[PMASK_ULONG_HEX_DIGITS];
is.read(s, sizeof(s));
std::string ss(s, sizeof(s));
......@@ -71,6 +63,12 @@ std::istream& operator >>(std::istream& is,
return is;
}
typedef struct {
sls_detector_header detHeader;
sls_receiver_packets_mask packetsMask;
} sls_receiver_header;
#include <fstream>
#include <memory.h>
......@@ -78,9 +76,9 @@ using namespace std;
int main(int argc, char *argv[])
{
typedef sls_receiver_header::packets_mask packets_mask;
typedef sls_receiver_packets_mask packets_mask;
sls_receiver_header h;
cout << h.packetsMask << endl;
cout << hex << setfill('0');
for (int f = 0, i = MAX_NUM_PACKETS / 8 - 1, j = 0; i >= 0; --i, ++j, ++f) {
......@@ -91,25 +89,26 @@ int main(int argc, char *argv[])
cout << dec << setfill(' ') << endl;
cout << h.packetsMask << endl;
sls_receiver_header::packets_mask_block b;
h.dumpPacketsMask(b);
sls_receiver_packets_mask_storage pm_storage;
pm_storage << h.packetsMask;
const char *fname = "/tmp/toto";
{
ofstream f(fname, ios::out | ios::trunc | ios::binary);
void *p = &b;
f.write(static_cast<const char *>(p), sizeof(b));
void *p = &pm_storage;
f.write(static_cast<const char *>(p), sizeof(pm_storage));
}
h.packetsMask.reset();
memset(b, 0, sizeof(b));
memset(pm_storage, 0, sizeof(pm_storage));
cout << h.packetsMask << endl;
{
ifstream f(fname, ios::in | ios::binary);
void *p = &b;
f.read(static_cast<char *>(p), sizeof(b));
void *p = &pm_storage;
f.read(static_cast<char *>(p), sizeof(pm_storage));
}
h.loadPacketsMask(b);
pm_storage >> h.packetsMask;
cout << hex << setfill('0');
for (int f = 0, i = MAX_NUM_PACKETS / 8 - 1; i >= 0; --i, ++f) {
packets_mask m = 0xff;
......@@ -120,13 +119,12 @@ int main(int argc, char *argv[])
cout << h.packetsMask << endl;
ostringstream os;
os << b;
os << pm_storage;
cout << os.str() << endl;
memset(b, 0, sizeof(b));
memset(pm_storage, 0, sizeof(pm_storage));
istringstream is(os.str());
is >> b;
h.loadPacketsMask(b);
is >> pm_storage;
pm_storage >> h.packetsMask;
cout << h.packetsMask << endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment