Project
Loading...
Searching...
No Matches
IOStruct.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_MCH_DEVIO_DIGITS_IO_STRUCT_H
13#define O2_MCH_DEVIO_DIGITS_IO_STRUCT_H
14
15#include <gsl/span>
16#include <iostream>
17#include "IO.h"
18#include <fmt/format.h>
19
20namespace o2::mch::io::impl
21{
22template <typename T>
23bool writeBinaryStruct(std::ostream& os,
24 gsl::span<const T> items)
25{
26 uint32_t nofItems = static_cast<uint32_t>(items.size());
27 if (!nofItems) {
28 return !os.bad();
29 }
30 writeNofItems(os, nofItems);
31 os.write(reinterpret_cast<const char*>(items.data()), items.size_bytes());
32 return !os.bad();
33}
34template <typename T>
35bool readBinaryStruct(std::istream& in, std::vector<T>& items, const char* itemName)
36{
37 if (in.peek() == EOF) {
38 return false;
39 }
40 // get the number of items
41 int nitems = readNofItems(in, itemName);
42 // get the items if any
43 if (nitems > 0) {
44 auto offset = items.size();
45 items.resize(offset + nitems);
46 in.read(reinterpret_cast<char*>(&items[offset]), nitems * sizeof(T));
47 if (in.fail()) {
48 throw std::length_error(fmt::format("invalid input : cannot read {} {}", nitems, itemName));
49 }
50 }
51 return true;
52}
53
54} // namespace o2::mch::io::impl
55
56#endif
GLintptr offset
Definition glcorearb.h:660
bool readBinaryStruct(std::istream &in, std::vector< T > &items, const char *itemName)
Definition IOStruct.h:35
bool writeBinaryStruct(std::ostream &os, gsl::span< const T > items)
Definition IOStruct.h:23
void writeNofItems(std::ostream &out, uint32_t nofItems)
Definition IO.cxx:30
int readNofItems(std::istream &in, const char *itemName)
Definition IO.cxx:20