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