Newer
Older
Markus Frank
committed
// $Id: Primitives.h 603 2013-06-13 21:15:14Z markus.frank $
//====================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------
//
// Author : M.Frank
//
//====================================================================
// Framework includes
#include "DDG4/IoStreams.h"
Markus Frank
committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdio>
#include "TFile.h"
using namespace DD4hep;
namespace {
/// Anonymous cast class to get access to protected members of TFile ;-)
class MyTFile : public TFile {
private:
/// Destructor (unused!)
virtual ~MyTFile() {}
public:
/// Basic write call
virtual Int_t SysWrite(Int_t fd, const void* buf, Int_t len) { return TFile::SysWrite(fd, buf, len); }
/// Basic read call
virtual Int_t SysRead(Int_t fd, void* buf, Int_t len) { return TFile::SysRead(fd,buf,len); }
/// Basic seek call
virtual Long64_t SysSeek(Int_t fd, Long64_t off, Int_t way) { return TFile::SysSeek(fd, off, way); }
};
}
namespace DD4hep {
/// Specialization for standard file descriptor files according to the posix standard
template<> void dd4hep_file<int>::open(const char* path, BOOST_IOS::openmode mode) {
if ( m_handle ) ::close(m_handle);
m_handle = ::open(path,mode);
}
/// Specialization for standard file descriptor files according to the posix standard
template<> dd4hep_file<int>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
: m_handle(fd), m_flag(flags) { }
/// Specialization for standard file descriptor files according to the posix standard
template<> dd4hep_file<int>::dd4hep_file(const char* fn, BOOST_IOS::openmode mode)
: m_handle(0), m_flag(close_handle) {open(fn,mode); }
/// Specialization for standard file descriptor files according to the posix standard
template<> std::streamsize dd4hep_file<int>::read(char_type* s, std::streamsize n)
{ return ::read(m_handle,s,n); }
/// Specialization for standard file descriptor files according to the posix standard
template<> std::streamsize dd4hep_file<int>::write(const char_type* s, std::streamsize n)
{ return ::write(m_handle,s,n); }
/// Specialization for standard file descriptor files according to the posix standard
template<> std::streampos dd4hep_file<int>::seek(stream_offset off, BOOST_IOS::seekdir way)
{ return ::lseek(m_handle,off,way); }
/// Specialization for standard file descriptor files according to the posix standard
template<> void dd4hep_file<int>::close() {
if ( m_handle ) ::close(m_handle);
m_handle = 0;
}
/// Specialization for the usage of TFile structures
template<> void dd4hep_file<TFile*>::open(const char* path, BOOST_IOS::openmode mode) {
if ( m_handle ) {
m_handle->Close();
delete m_handle;
}
std::string p = path;
p += "?filetype=raw";
if ( mode&BOOST_IOS::out )
m_handle = TFile::Open(p.c_str(),"RECREATE","ROOT");
else
m_handle = TFile::Open(p.c_str());
if ( m_handle->IsZombie() ) {
delete m_handle;
m_handle = 0;
throw 1;
}
}
#define _p(x) ((MyTFile*)x)
/// Specialization for the usage of TFile structures
template<> dd4hep_file<TFile*>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
: m_handle(fd), m_flag(flags) { }
/// Specialization for the usage of TFile structures
template<> dd4hep_file<TFile*>::dd4hep_file(const char* fname, BOOST_IOS::openmode mode)
: m_handle(0), m_flag(close_handle) { open(fname,mode); }
/// Specialization for the usage of TFile structures
template<> std::streamsize dd4hep_file<TFile*>::read(char_type* s, std::streamsize n) {
if ( m_handle ) {
Long64_t nb1 = m_handle->GetBytesRead();
Bool_t res = _p(m_handle)->ReadBuffer(s,nb1,n);
if ( res ) {
Long64_t nb2 = m_handle->GetBytesRead();
return nb2-nb1;
}
}
return -1;
}
/// Specialization for the usage of TFile structures
template<> std::streamsize dd4hep_file<TFile*>::write(const char_type* s, std::streamsize n)
{ return m_handle ? _p(m_handle)->SysWrite(m_handle->GetFd(),s,n) : -1; }
/// Specialization for the usage of TFile structures
template<> std::streampos dd4hep_file<TFile*>::seek(stream_offset off, BOOST_IOS::seekdir way)
{ return m_handle ? _p(m_handle)->SysSeek(m_handle->GetFd(),off,way) : -1; }
/// Specialization for the usage of TFile structures
template<> void dd4hep_file<TFile*>::close()
{ if ( m_handle ) { m_handle->Close(); delete m_handle; m_handle=0; } }
}