Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-31 04:19:41

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