MCPcopy Create free account
hub / github.com/davisking/dlib / pack_int

Function pack_int

dlib/serialize.h:343–388  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341 typename T
342 >
343 typename enable_if_c<std::numeric_limits<T>::is_signed,bool>::type pack_int (
344 T item,
345 std::ostream& out
346 )
347 /*!
348 requires
349 - T is a signed integral type
350 ensures
351 - if (no problems occur serializing item) then
352 - writes item to out
353 - returns false
354 - else
355 - returns true
356 !*/
357 {
358 COMPILE_TIME_ASSERT(sizeof(T) <= 8);
359 unsigned char buf[9];
360 unsigned char size = sizeof(T);
361 unsigned char neg;
362 if (item < 0)
363 {
364 neg = 0x80;
365 item *= -1;
366 }
367 else
368 {
369 neg = 0;
370 }
371
372 for (unsigned char i = 1; i <= sizeof(T); ++i)
373 {
374 buf[i] = static_cast<unsigned char>(item&0xFF);
375 item >>= 8;
376 if (item == 0) { size = i; break; }
377 }
378
379 std::streambuf* sbuf = out.rdbuf();
380 buf[0] = size|neg;
381 if (sbuf->sputn(reinterpret_cast<char*>(buf),size+1) != size+1)
382 {
383 out.setstate(std::ios::eofbit | std::ios::badbit);
384 return true;
385 }
386
387 return false;
388 }
389
390 // ------------------------------------------------------------------------------------
391

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected