artdaq_core_demo  v1_07_01
AsciiFragmentWriter.hh
1 #ifndef artdaq_demo_Overlays_AsciiFragmentWriter_hh
2 #define artdaq_demo_Overlays_AsciiFragmentWriter_hh
3 
4 #include "artdaq-core-demo/Overlays/AsciiFragment.hh"
5 #include "artdaq-core/Data/Fragment.hh"
6 
7 namespace demo {
8 class AsciiFragmentWriter;
9 }
10 
23 {
24 public:
32  explicit AsciiFragmentWriter(artdaq::Fragment& f);
33 
34  // These functions form overload sets with const functions from
35  // demo::AsciiFragment
36 
41  char* dataBegin();
42 
47  char* dataEnd();
48 
54  {
55  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header));
56  return reinterpret_cast<Header*>(artdaq_Fragment_.dataBeginBytes()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
57  }
58 
63  void set_hdr_line_number(Header::line_number_t line_number) { header_()->line_number = line_number; }
64 
69  void resize(size_t nChars);
70 
71 private:
72  static size_t calc_event_size_words_(size_t nChars);
73 
74  static size_t chars_to_words_(size_t nChars);
75 
76  // Note that this non-const reference hides the const reference in the base class
77  artdaq::Fragment& artdaq_Fragment_;
78 };
79 
80 // The constructor will expect the artdaq::Fragment object it's been
81 // passed to contain the artdaq::Fragment header + the
82 // AsciiFragment::Metadata object, otherwise it throws
83 
85  : AsciiFragment(f), artdaq_Fragment_(f)
86 {
87  if (!f.hasMetadata() || f.dataSizeBytes() > 0)
88  {
89  throw cet::exception(
90  "Error in AsciiFragmentWriter: Raw artdaq::Fragment object does not appear to consist of (and only of) its own "
91  "header + the AsciiFragment::Metadata object");
92  }
93 
94  // Allocate space for the header
95  artdaq_Fragment_.resizeBytes(sizeof(Header));
96 }
97 
99 {
100  // Make sure there's data past the AsciiFragment header
101  assert(artdaq_Fragment_.dataSizeBytes() >= sizeof(Header) + sizeof(artdaq::Fragment::value_type));
102  return reinterpret_cast<char*>(header_() + 1); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
103 }
104 
106 {
107  return dataBegin() + total_line_characters(); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
108 }
109 
110 inline void demo::AsciiFragmentWriter::resize(size_t nChars)
111 {
112  artdaq_Fragment_.resizeBytes(sizeof(Header::data_t) * calc_event_size_words_(nChars));
113  header_()->event_size = calc_event_size_words_(nChars);
114 }
115 
116 inline size_t demo::AsciiFragmentWriter::calc_event_size_words_(size_t nChars)
117 {
118  return chars_to_words_(nChars) + hdr_size_words();
119 }
120 
121 inline size_t demo::AsciiFragmentWriter::chars_to_words_(size_t nChars)
122 {
123  auto mod(nChars % chars_per_word_());
124  return (mod == 0) ? nChars / chars_per_word_() : nChars / chars_per_word_() + 1;
125 }
126 
127 #endif /* artdaq_demo_Overlays_AsciiFragmentWriter_hh */
AsciiFragmentWriter(artdaq::Fragment &f)
The constructor will expect the artdaq::Fragment object it&#39;s been passed to contain the artdaq::Fragm...
char * dataEnd()
Return a non-const (for writing) pointer to the end of the Fragment payload.
The AsciiFragment::Header contains information about the payload size and the &quot;line number&quot; of the da...
Header * header_()
Returns a pointer to the AsciiFragment::Header object, non-const for writing.
Class derived from AsciiFragment which allows writes to the data (for simulation purposes).
char * dataBegin()
Return a non-const (for writing) pointer to the beginning of the Fragment payload.
An artdaq::Fragment overlay class designed to hold string data for pedagogical purposes.
void resize(size_t nChars)
Resize the Fragment so that it can contain nChars characters.
uint64_t line_number_t
Type for the line_number field.
void set_hdr_line_number(Header::line_number_t line_number)
Sets the line number in the AsciiFragment::Header to the given (ASCII-encoded) value.