StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BinaryStoreHelper.h
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 "AliHLTTPCCADef.h"
26 #include <cstdio>
27 
28 // writes
29 
30 template<typename T> static inline void BinaryStoreWrite( const T *mem, int count, FILE *f )
31 {
32  const int written = std::fwrite( mem, sizeof( T ), count, f );
33  assert( written == count ); UNUSED_PARAM1( written );
34 }
35 
36 template<typename T> static inline void BinaryStoreWrite( const T &mem, FILE *f )
37 {
38  const size_t written = std::fwrite( &mem, sizeof( T ), 1, f );
39  assert( written == 1 ); UNUSED_PARAM1( written );
40 }
41 
42 static inline void BinaryStoreWrite( const void *offsetPtr, const void *mem, FILE *f )
43 {
44  const int offset = static_cast<const char *>( offsetPtr ) - static_cast<const char *>( mem );
45  const size_t written = std::fwrite( &offset, sizeof( int ), 1, f );
46  assert( written == 1 ); UNUSED_PARAM1( written );
47 }
48 
49 // reads
50 
51 template<typename T> static inline void BinaryStoreRead( T *mem, int count, FILE *f )
52 {
53  const int read = std::fread( mem, sizeof( T ), count, f );
54  assert( read == count ); UNUSED_PARAM1( read );
55 }
56 
57 template<typename T> static inline void BinaryStoreRead( T &mem, FILE *f )
58 {
59  const size_t read = std::fread( &mem, sizeof( T ), 1, f );
60  assert( read == 1 ); UNUSED_PARAM1( read );
61 }
62 
63 template<typename T>
64 static inline void BinaryStoreRead( T *&offsetPtr, char *mem, FILE *f )
65 {
66  int offset;
67  const size_t read = std::fread( &offset, sizeof( int ), 1, f );
68  assert( read == 1 ); UNUSED_PARAM1( read );
69  offsetPtr = reinterpret_cast<T *>( mem + offset );
70 }
71 
72 #endif // BINARYSTOREHELPER_H