13 #include <stk_util/util/Marshal.hpp> 20 typedef std::map<uint32_t, std::string> TypeNameMap;
22 bool s_crcInitialized =
false;
23 uint32_t s_crcTable[256];
24 TypeNameMap s_typeNameMap;
34 for(
int i = 1; i < (c + 1); i++) {
36 value |= (1 << (c - i));
47 s_crcInitialized =
true;
50 uint32_t polynomial = 0x04C11DB7;
52 std::fill(s_crcTable, s_crcTable + 256, 0);
55 for (
int i = 0; i <= 0xFF; i++) {
56 s_crcTable[i] = crc32_reflect(i, 8) << 24;
58 for (
int j = 0; j < 8; j++)
59 s_crcTable[i] = (s_crcTable[i] << 1) ^ ((s_crcTable[i] & (1u << 31)) ? polynomial : 0);
61 s_crcTable[i] = crc32_reflect(s_crcTable[i], 32);
69 const unsigned char * s,
73 crc = (crc >> 8)^s_crcTable[(crc & 0xFF)^*s++];
79 const unsigned char * s,
82 uint32_t crc = 0xffffffff;
83 crc32_part(crc, s, l);
84 return crc ^ 0xffffffff;
90 std::stringstream & os,
91 const std::type_info & typeinfo)
93 if (!s_crcInitialized)
96 const char *name = typeinfo.name();
97 unsigned length = std::strlen(name);
99 uint32_t crc = crc32((
const unsigned char *) name, length);
101 TypeNameMap::iterator it = s_typeNameMap.find(crc);
102 if (it == s_typeNameMap.end())
103 s_typeNameMap.insert(std::make_pair(crc, std::string(name)));
105 os.write((
const char *) &crc,
sizeof(uint32_t));
111 std::stringstream & is,
112 const std::type_info & typeinfo)
114 if (!s_crcInitialized)
117 const char *name = typeinfo.name();
118 unsigned length = std::strlen(name);
120 uint32_t crc_check = crc32((
const unsigned char *) name, length);
124 TypeNameMap::iterator it = s_typeNameMap.find(crc_check);
125 if (it == s_typeNameMap.end())
126 s_typeNameMap.insert(std::make_pair(crc_check, std::string(name)));
129 is.read((
char *) &crc,
sizeof(uint32_t));
131 if (crc_check != crc) {
132 std::ostringstream ss;
133 ss <<
"Marshaller encountered type ";
134 TypeNameMap::const_iterator it = s_typeNameMap.find(crc);
135 if (it == s_typeNameMap.end())
136 ss <<
"code " << std::hex << crc;
140 ss <<
" when expecting type ";
141 it = s_typeNameMap.find(crc_check);
142 if (it == s_typeNameMap.end())
143 ss <<
"code " << std::hex << crc_check;
147 throw std::runtime_error(ss.str());
155 : stream(
std::ios_base::out),
156 m_typeCheck(TYPE_CHECK_NONE)
158 (*this) << type_check;
164 const std::string & s)
182 return stream.str().size();
186 Marshal::operator
void * ()
const 188 return static_cast<void *
>(
const_cast<std::stringstream*
>(&
stream));
194 const char * address,
197 stream.write(address, byte_count);
206 stream.read(address, byte_count);
212 crc32_write(mout.
stream, t);
218 crc32_check(min.
stream, t);
226 return write(mout, t);
233 return write(mout, t);
240 return write(mout, t);
247 return write(mout, t);
254 return write(mout, t);
261 return write(mout, t);
268 return write(mout, t);
275 return write(mout, t);
282 return write(mout, t);
289 return write(mout, t);
296 return write(mout, t);
303 return write(mout, t);
310 return write(mout, t);
318 size_t ul = s.
size();
320 mout.
stream.write(s.data(), s.size());
423 std::vector<char> c(size);
425 min.
stream.read(&c[0], size);
426 s.assign(&c[0], size);
unsigned m_typeCheck
Type checking to activate.
size_t size() const
Member function size returns the byte count of the string of packed bytes creates by put-to operation...
void read(char *address, size_t byte_count)
Member function read reads bytes from the packed byte stream.
Struct Marshal is a data packer for sending and receiving parallel messages. The data put-to (<<) is ...
std::stringstream stream
Packed byte stream to put-to or get-from.
std::string str() const
Member function str returns the string of packed bytes created by put-to operations to the stream...
void write(const char *address, size_t byte_count)
Member function write writer bytes to the packed byte stream.
Marshal(unsigned type_check=TYPE_CHECK_NONE)