otsdaq  v2_04_01
BinaryStringMacros.h
1 #ifndef _ots_BinaryStringMacros_h_
2 #define _ots_BinaryStringMacros_h_
3 
4 #include "otsdaq-core/Macros/CoutMacros.h"
5 
6 #include <map>
7 #include <set>
8 #include <typeinfo> // operator typeid
9 #include <vector>
10 
11 namespace ots
12 {
14 {
15  private: // private constructor because all static members, should never instantiate
16  // this class
17  BinaryStringMacros(void);
18  ~BinaryStringMacros(void);
19 
20  public:
21  // Here is the list of static helper functions:
22  //
23  // binaryToHexString
24  // binaryTo8ByteHexString
25  //
26 
27  static std::string binaryToHexString(const char* binaryBuffer,
28  unsigned int numberOfBytes,
29  const std::string& resultPreamble = "",
30  const std::string& resultDelimiter = "");
31  static std::string binaryTo8ByteHexString(const std::string& binaryBuffer,
32  const std::string& resultPreamble = "0x",
33  const std::string& resultDelimiter = " ");
34 
35  template<class T>
36  static void insertValueInBinaryString(std::string& binaryBuffer,
37  T value,
38  unsigned int byteIndex = 0,
39  unsigned int bitIndex = 0)
40  {
41  __COUTV__(sizeof(value));
42 
43  __COUT__ << "Original size of binary buffer: " << binaryBuffer.size() << __E__;
44 
45  byteIndex += bitIndex / 8;
46  bitIndex %= 8;
47 
48  binaryBuffer.resize(byteIndex + sizeof(value) + (bitIndex ? 1 : 0));
49 
50  if(bitIndex)
51  {
52  // special handling for bit shift on every byte
53 
54  unsigned char keepMask = ((unsigned char)(-1)) >> bitIndex;
55 
56  // first byte is a mix of what's there and value
57  binaryBuffer[byteIndex] = (binaryBuffer[byteIndex] & keepMask) |
58  (((unsigned char*)(&value))[0] << bitIndex);
59 
60  // middle bytes are mix of value i-1 and i
61  for(unsigned int i = 1; i < sizeof(value); ++i)
62  {
63  }
64 
65  // last bytes is mix of value and what's there
66  }
67 
68  memcpy((void*)&binaryBuffer[byteIndex + 0] /*dest*/,
69  (void*)&value /*src*/,
70  sizeof(value) /*numOfBytes*/);
71  __COUT__ << "Final size of binary buffer: " << binaryBuffer.size() << __E__;
72 
73  } // end insertValueInBinaryString
74  // specialized for string value
75  static void insertValueInBinaryString(std::string& binaryBuffer,
76  const std::string& value,
77  unsigned int byteIndex = 0,
78  unsigned int bitIndex = 0)
79  {
80  }
81 
82 }; // end BinaryStringMacros static class
83 } // namespace ots
84 #endif