CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/IOPool/Streamer/interface/MsgTools.h

Go to the documentation of this file.
00001 #ifndef IOPool_Streamer_MsgTools_h
00002 #define IOPool_Streamer_MsgTools_h
00003 
00004 #include <vector>
00005 #include <string>
00006 #include <sstream>
00007 #include <iterator>
00008 #include "FWCore/Utilities/interface/Algorithms.h"
00009 
00010 // could just use the c99 names here from stdint.h
00011 typedef unsigned char uint8;
00012 typedef unsigned short uint16; 
00013 typedef unsigned int  uint32;
00014 typedef unsigned long long uint64;
00015 typedef unsigned char char_uint64[sizeof(uint64)];
00016 typedef unsigned char char_uint32[sizeof(uint32)];
00017 typedef unsigned char char_uint16[sizeof(uint16)];
00018 typedef std::vector<std::string> Strings;
00019 
00020 
00021 inline uint64 convert64(char_uint64 v)
00022 {
00023   // first four bytes are code,  LSB first
00024   unsigned long long a=v[0], b=v[1], c=v[2], d=v[3];
00025   unsigned long long e=v[4], f=v[5], g=v[6], h=v[7];
00026   a|=(b<<8)|(c<<16)|(d<<24)|(e<<32)|(f<<40)|(g<<48)|(h<<56);
00027   return a;
00028 }
00029 
00030 inline uint32 convert32(char_uint32 v)
00031 { 
00032   // first four bytes are code,  LSB first
00033   unsigned int a=v[0], b=v[1], c=v[2], d=v[3];
00034   a|=(b<<8)|(c<<16)|(d<<24);
00035   return a;
00036 }
00037 
00038 inline uint16 convert16(char_uint16 v)
00039 { 
00040   // first four bytes are code,  LSB first
00041   unsigned int a=v[0], b=v[1];
00042   a|=(b<<8);
00043   return a;
00044 }
00045 
00046 inline void convert(uint32 i, char_uint32 v)
00047 {
00048   v[0]=i&0xff;
00049   v[1]=(i>>8)&0xff;
00050   v[2]=(i>>16)&0xff;
00051   v[3]=(i>>24)&0xff;
00052 }
00053 
00054 inline void convert(uint16 i, char_uint16 v)
00055 {
00056   v[0]=i&0xff;
00057   v[1]=(i>>8)&0xff;
00058 }
00059 
00060 inline void convert(uint64 li, char_uint64 v)
00061 {
00062   v[0]=li&0xff;
00063   v[1]=(li>>8)&0xff;
00064   v[2]=(li>>16)&0xff;
00065   v[3]=(li>>24)&0xff;
00066   v[4]=(li>>32)&0xff;
00067   v[5]=(li>>40)&0xff;
00068   v[6]=(li>>48)&0xff;
00069   v[7]=(li>>56)&0xff;
00070 }
00071 
00072 namespace MsgTools
00073 {
00074 
00075 inline uint8* fillNames(const Strings& names, uint8* pos)
00076 {
00077   uint32 sz = names.size();
00078   convert(sz,pos); // save number of strings
00079   uint8* len_pos = pos + sizeof(char_uint32); // area for length
00080   pos = len_pos + sizeof(char_uint32); // area for full string of names
00081   bool first = true;
00082 
00083   for(Strings::const_iterator beg = names.begin(); beg != names.end(); ++beg) {
00084       if(first) first = false; else *pos++ = ' ';
00085       pos = edm::copy_all(*beg,pos);
00086   }
00087   convert((uint32)(pos-len_pos-sizeof(char_uint32)),len_pos);
00088   return pos;
00089 }
00090 
00091 inline void getNames(uint8* from, uint32 from_len, Strings& to)
00092 {
00093   // not the most efficient way to do this
00094   std::istringstream ist(std::string(reinterpret_cast<char *>(from),from_len));
00095   typedef std::istream_iterator<std::string> Iter;
00096   std::copy(Iter(ist),Iter(),std::back_inserter(to));
00097 }
00098 
00099 }
00100 
00101 #endif