Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:19

0001 #ifndef IOPool_Streamer_MsgTools_h
0002 #define IOPool_Streamer_MsgTools_h
0003 
0004 #include <vector>
0005 #include <string>
0006 #include <sstream>
0007 #include <iterator>
0008 #include "FWCore/Utilities/interface/Algorithms.h"
0009 
0010 // could just use the c99 names here from stdint.h
0011 typedef unsigned char uint8;
0012 typedef unsigned short uint16;
0013 typedef unsigned int uint32;
0014 typedef unsigned long long uint64;
0015 typedef unsigned char char_uint64[sizeof(uint64)];
0016 typedef unsigned char char_uint32[sizeof(uint32)];
0017 typedef unsigned char char_uint16[sizeof(uint16)];
0018 typedef std::vector<std::string> Strings;
0019 
0020 inline uint64 convert64(char_uint64 v) {
0021   // first four bytes are code,  LSB first
0022   unsigned long long a = v[0], b = v[1], c = v[2], d = v[3];
0023   unsigned long long e = v[4], f = v[5], g = v[6], h = v[7];
0024   a |= (b << 8) | (c << 16) | (d << 24) | (e << 32) | (f << 40) | (g << 48) | (h << 56);
0025   return a;
0026 }
0027 
0028 inline uint32 convert32(char_uint32 v) {
0029   // first four bytes are code,  LSB first
0030   unsigned int a = v[0], b = v[1], c = v[2], d = v[3];
0031   a |= (b << 8) | (c << 16) | (d << 24);
0032   return a;
0033 }
0034 
0035 inline uint16 convert16(char_uint16 v) {
0036   // first four bytes are code,  LSB first
0037   unsigned int a = v[0], b = v[1];
0038   a |= (b << 8);
0039   return a;
0040 }
0041 
0042 inline void convert(uint32 i, char_uint32 v) {
0043   v[0] = i & 0xff;
0044   v[1] = (i >> 8) & 0xff;
0045   v[2] = (i >> 16) & 0xff;
0046   v[3] = (i >> 24) & 0xff;
0047 }
0048 
0049 inline void convert(uint16 i, char_uint16 v) {
0050   v[0] = i & 0xff;
0051   v[1] = (i >> 8) & 0xff;
0052 }
0053 
0054 inline void convert(uint64 li, char_uint64 v) {
0055   v[0] = li & 0xff;
0056   v[1] = (li >> 8) & 0xff;
0057   v[2] = (li >> 16) & 0xff;
0058   v[3] = (li >> 24) & 0xff;
0059   v[4] = (li >> 32) & 0xff;
0060   v[5] = (li >> 40) & 0xff;
0061   v[6] = (li >> 48) & 0xff;
0062   v[7] = (li >> 56) & 0xff;
0063 }
0064 
0065 namespace MsgTools {
0066 
0067   inline uint8* fillNames(const Strings& names, uint8* pos) {
0068     uint32 sz = names.size();
0069     convert(sz, pos);                            // save number of strings
0070     uint8* len_pos = pos + sizeof(char_uint32);  // area for length
0071     pos = len_pos + sizeof(char_uint32);         // area for full string of names
0072     bool first = true;
0073 
0074     for (Strings::const_iterator beg = names.begin(); beg != names.end(); ++beg) {
0075       if (first)
0076         first = false;
0077       else
0078         *pos++ = ' ';
0079       pos = edm::copy_all(*beg, pos);
0080     }
0081     convert((uint32)(pos - len_pos - sizeof(char_uint32)), len_pos);
0082     return pos;
0083   }
0084 
0085   inline void getNames(uint8* from, uint32 from_len, Strings& to) {
0086     // not the most efficient way to do this
0087     std::istringstream ist(std::string(reinterpret_cast<char*>(from), from_len));
0088     typedef std::istream_iterator<std::string> Iter;
0089     std::copy(Iter(ist), Iter(), std::back_inserter(to));
0090   }
0091 
0092 }  // namespace MsgTools
0093 
0094 #endif