Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IOTypes.cc
Go to the documentation of this file.
1 ///
2 /// @file IOTypes.cc
3 /// @brief Declarations for Hammer IO structs
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
12 #include <algorithm>
13 
14 #include "Hammer/Tools/IOTypes.hh"
16 
17 using namespace std;
18 
19 namespace Hammer {
20 
21  ostream& operator<<(ostream& os, const IOBuffer& buf) {
22  os.write(reinterpret_cast<const char*>(&buf.kind), sizeof(buf.kind));
23  os.write(reinterpret_cast<const char*>(&buf.length), sizeof(buf.length));
24  os.write(reinterpret_cast<const char*>(buf.start), buf.length);
25  return os;
26  }
27 
28  istream& operator>>(istream& is, IOBuffer& buf) {
29  if(is.eof()) {
31  buf.length = 0;
32  if(buf.start != nullptr) {
33  delete[] buf.start;
34  buf.start = nullptr;
35  }
36  return is;
37  }
38  uint32_t tmplength = 0;
39  try {
40  is.read(reinterpret_cast<char*>(&buf.kind), sizeof(buf.kind));
41  is.read(reinterpret_cast<char*>(&tmplength), sizeof(tmplength));
42  if(tmplength > buf.length) {
43  if(buf.start != nullptr) {
44  delete[] buf.start;
45  }
46  buf.start = new uint8_t[tmplength];
47  if(buf.start != nullptr) {
48  buf.length = tmplength;
49  }
50  else {
51  buf.length = 0;
52  }
53  }
54  is.read(reinterpret_cast<char*>(buf.start), tmplength);
55  }
56  catch(ios_base::failure&) {
57  tmplength = 0;
58  }
59  if(tmplength == 0 && buf.start != nullptr) {
60  delete[] buf.start;
61  buf.start = nullptr;
63  buf.length = 0;
64  }
65  return is;
66  }
67 
68  IOBuffers::IOBuffers() {
69 
70  }
71 
72  IOBuffers::IOBuffers(unique_ptr<Serial::DetachedBuffers>&& data) : _pOwner{move(data)} {
73  init();
74  }
75 
76  IOBuffers::IOBuffers(IOBuffers&& other) : _buffers{move(other._buffers)}, _pOwner{move(other._pOwner)} {
77 
78  }
79 
81  _pOwner = move(other._pOwner);
82  _buffers.swap(other._buffers);
83  return *this;
84  }
85 
87  clear();
88  }
89 
90  IOBuffer& IOBuffers::at(size_t pos) {
91  return _buffers.at(pos);
92  }
93 
94  const IOBuffer& IOBuffers::at(size_t pos) const {
95  return _buffers.at(pos);
96  }
97 
99  return _buffers[pos];
100  }
101 
102  const IOBuffer& IOBuffers::operator[](size_t pos) const {
103  return _buffers[pos];
104  }
105 
107  return _buffers.front();
108  }
109 
110  const IOBuffer& IOBuffers::front() const {
111  return _buffers.front();
112  }
113 
115  return _buffers.back();
116  }
117 
118  const IOBuffer& IOBuffers::back() const {
119  return _buffers.back();
120  }
121 
122  size_t IOBuffers::size() const {
123  return _buffers.size();
124  }
125 
126  bool IOBuffers::empty() const {
127  return _buffers.empty();
128  }
129 
131  return _buffers.begin();
132  }
133 
135  return _buffers.begin();
136  }
137 
139  return _buffers.cbegin();
140  }
141 
143  return _buffers.end();
144  }
145 
147  return _buffers.end();
148  }
149 
151  return _buffers.cend();
152  }
153 
155  return _buffers.rbegin();
156  }
157 
159  return _buffers.rbegin();
160  }
161 
163  return _buffers.crbegin();
164  }
165 
167  return _buffers.rend();
168  }
169 
171  return _buffers.rend();
172  }
173 
175  return _buffers.crend();
176  }
177 
179  _buffers.clear();
180  _pOwner.reset();
181  }
182 
184  _buffers.clear();
185  _buffers.reserve(_pOwner->size());
186  for(size_t i = 0; i< _pOwner->size(); ++i) {
187  IOBuffer buf;
188  buf.kind = static_cast<RecordType>(_pOwner->bufferType(i));
189  buf.start = const_cast<uint8_t*>(_pOwner->buffer(i).data());
190  buf.length = static_cast<uint32_t>(_pOwner->buffer(i).size());
191  _buffers.push_back(buf);
192  }
193  }
194 
195  ostream& operator<<(ostream& os, const IOBuffers& buf) {
196  for(auto& elem: buf) {
197  os << elem;
198  }
199  return os;
200  }
201 
202 #ifdef HAVE_ROOT
203 
204  RootIOBuffer& RootIOBuffer::operator=(const IOBuffer& other) {
205  kind = other.kind;
206  if(other.length > static_cast<uint32_t>(maxLength)) {
207  throw out_of_range("Need reallocating: " + to_string(other.length) + " vs " + to_string(maxLength));
208  }
209  length = static_cast<Int_t>(other.length);
210  copy(other.start, other.start + other.length, start);
211  return *this;
212  }
213 
214 #endif
215 
216 } // namespace Hammer
std::vector< IOBuffer > _buffers
Definition: IOTypes.hh:104
std::vector< IOBuffer >::reverse_iterator reverse_iterator
Definition: IOTypes.hh:62
std::vector< IOBuffer >::iterator iterator
Definition: IOTypes.hh:60
const_iterator cend() const noexcept
Definition: IOTypes.cc:150
from libcpp map cimport map from libcpp set cimport set as cset from libcpp string cimport string from libcpp pair cimport pair from libcpp vector cimport vector from libcpp unordered_map cimport unordered_map from libcpp cimport bool from libc stdint cimport uint32_t
Definition: cppdefs.pxd:17
istream & operator>>(istream &is, IOBuffer &buf)
Definition: IOTypes.cc:28
YAML::Emitter & operator<<(YAML::Emitter &out, const ProcessDefinitions &s)
IOBuffer & operator[](size_t pos)
Definition: IOTypes.cc:98
std::unique_ptr< Serial::DetachedBuffers > _pOwner
Definition: IOTypes.hh:105
reverse_iterator rend() noexcept
Definition: IOTypes.cc:166
IOBuffer & at(size_t pos)
Definition: IOTypes.cc:90
RecordType kind
Definition: IOTypes.hh:33
IOBuffer & back()
Definition: IOTypes.cc:114
std::vector< IOBuffer >::const_reverse_iterator const_reverse_iterator
Definition: IOTypes.hh:63
const_iterator cbegin() const noexcept
Definition: IOTypes.cc:138
RecordType
Definition: IOTypes.hh:30
bool empty() const
Definition: IOTypes.cc:126
const_reverse_iterator crbegin() const noexcept
Definition: IOTypes.cc:162
size_t size() const
Definition: IOTypes.cc:122
iterator begin() noexcept
Definition: IOTypes.cc:130
from libcpp map cimport map from libcpp set cimport set as cset from libcpp string cimport string from libcpp pair cimport pair from libcpp vector cimport vector from libcpp unordered_map cimport unordered_map from libcpp cimport bool from libc stdint cimport uint8_t
Definition: cppdefs.pxd:17
iterator end() noexcept
Definition: IOTypes.cc:142
IOBuffer & front()
Definition: IOTypes.cc:106
reverse_iterator rbegin() noexcept
Definition: IOTypes.cc:154
Declarations for Hammer IO structs.
uint8_t * start
Definition: IOTypes.hh:35
const_reverse_iterator crend() const noexcept
Definition: IOTypes.cc:174
uint32_t length
Definition: IOTypes.hh:34
std::vector< IOBuffer >::const_iterator const_iterator
Definition: IOTypes.hh:61
Serialization related typedefs and includes.
IOBuffers & operator=(const IOBuffers &)=delete