PandaRoot
BinaryStoreHelper.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 /*
14  Copyright (C) 2009 Matthias Kretz <kretz@kde.org>
15 
16  This program is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Library General Public
18  License as published by the Free Software Foundation; either
19  version 2 of the License, or (at your option) version 3.
20 
21  This library is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  Library General Public License for more details.
25 
26  You should have received a copy of the GNU Library General Public License
27  along with this library; see the file COPYING.LIB. If not, write to
28  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29  Boston, MA 02110-1301, USA.
30 
31 */
32 
33 #ifndef BINARYSTOREHELPER_H
34 #define BINARYSTOREHELPER_H
35 
36 #include <assert.h>
37 #include "PndFTSCADef.h"
38 #include <cstdio>
39 
40 // writes
41 
42 template <typename T>
43 static inline void BinaryStoreWrite(const T *mem, int count, FILE *f)
44 {
45  const int written = std::fwrite(mem, sizeof(T), count, f);
46  assert(written == count);
47  UNUSED_PARAM1(written);
48 }
49 
50 template <typename T>
51 static inline void BinaryStoreWrite(const T &mem, FILE *f)
52 {
53  const size_t written = std::fwrite(&mem, sizeof(T), 1, f);
54  assert(written == 1);
55  UNUSED_PARAM1(written);
56 }
57 
58 static inline void BinaryStoreWrite(const void *offsetPtr, const void *mem, FILE *f)
59 {
60  const int offset = static_cast<const char *>(offsetPtr) - static_cast<const char *>(mem);
61  const size_t written = std::fwrite(&offset, sizeof(int), 1, f);
62  assert(written == 1);
63  UNUSED_PARAM1(written);
64 }
65 
66 // reads
67 
68 template <typename T>
69 static inline void BinaryStoreRead(T *mem, int count, FILE *f)
70 {
71  const int read = std::fread(mem, sizeof(T), count, f);
72  assert(read == count);
73  UNUSED_PARAM1(read);
74 }
75 
76 template <typename T>
77 static inline void BinaryStoreRead(T &mem, FILE *f)
78 {
79  const size_t read = std::fread(&mem, sizeof(T), 1, f);
80  assert(read == 1);
81  UNUSED_PARAM1(read);
82 }
83 
84 template <typename T>
85 static inline void BinaryStoreRead(T *&offsetPtr, char *mem, FILE *f)
86 {
87  int offset;
88  const size_t read = std::fread(&offset, sizeof(int), 1, f);
89  assert(read == 1);
90  UNUSED_PARAM1(read);
91  offsetPtr = reinterpret_cast<T *>(mem + offset);
92 }
93 
94 #endif // BINARYSTOREHELPER_H
static void BinaryStoreWrite(const T *mem, int count, FILE *f)
static void BinaryStoreRead(T *mem, int count, FILE *f)
float f
Definition: P4_F32vec4.h:32