File indexing completed on 2025-01-18 09:55:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DDG4_IOSTREAMS_H
0014 #define DDG4_IOSTREAMS_H
0015
0016
0017 #include <string>
0018
0019 #ifdef __GNUC__
0020 #pragma GCC diagnostic push
0021 #pragma GCC diagnostic ignored "-Wshadow"
0022 #endif
0023
0024
0025 #include <boost/iostreams/categories.hpp>
0026 #include <boost/iostreams/detail/ios.hpp>
0027 #include <boost/iostreams/detail/path.hpp>
0028 #include <boost/iostreams/positioning.hpp>
0029
0030 #ifdef __GNUC__
0031
0032 #define __DD4HEP_LOCAL_GNUC__ __GNUC__
0033 #undef __GNUC__
0034 #include <boost/iostreams/stream.hpp>
0035 #define __GNUC__ __DD4HEP_LOCAL_GNUC__
0036 #else
0037 #include <boost/iostreams/stream.hpp>
0038 #endif
0039
0040
0041 class TFile;
0042
0043
0044 namespace dd4hep {
0045
0046
0047 template <typename T> class dd4hep_file_source;
0048 template <typename T> class dd4hep_file_sink;
0049
0050 enum dd4hep_file_flags {
0051 never_close_handle = 0,
0052 close_handle = 3
0053 };
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073 template <typename T> class dd4hep_file {
0074 public:
0075 friend class dd4hep_file_source<T>;
0076 friend class dd4hep_file_sink<T>;
0077 typedef T handle_type;
0078 typedef char char_type;
0079 typedef boost::iostreams::stream_offset stream_offset;
0080 typedef boost::iostreams::detail::path detail_path;
0081
0082 struct category : boost::iostreams::seekable_device_tag, boost::iostreams::closable_tag { };
0083
0084
0085 dd4hep_file() = default;
0086
0087 dd4hep_file(handle_type fd, dd4hep_file_flags);
0088
0089 dd4hep_file(const char* fname, BOOST_IOS::openmode mode);
0090
0091
0092
0093 void open(handle_type fd, dd4hep_file_flags flags);
0094
0095 void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out );
0096
0097 void close();
0098
0099 std::streamsize read(char_type* s, std::streamsize n);
0100
0101 std::streamsize write(const char_type* s, std::streamsize n);
0102
0103 std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
0104
0105 bool is_open() const { return m_handle != 0; }
0106
0107 handle_type handle() const { return m_handle; }
0108
0109 private:
0110
0111 handle_type m_handle = 0;
0112
0113 dd4hep_file_flags m_flag = close_handle;
0114 };
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 template <typename T=int> class dd4hep_file_source : private dd4hep_file<T> {
0136 public:
0137 typedef dd4hep_file<T> descriptor;
0138
0139 struct category : boost::iostreams::input_seekable,
0140 boost::iostreams::device_tag,
0141 boost::iostreams::closable_tag { };
0142 typedef typename descriptor::handle_type handle_type;
0143 typedef typename descriptor::char_type char_type;
0144 using descriptor::is_open;
0145 using descriptor::close;
0146 using descriptor::read;
0147 using descriptor::seek;
0148 using descriptor::handle;
0149
0150
0151 dd4hep_file_source() : descriptor() { }
0152
0153
0154 dd4hep_file_source(const dd4hep_file_source<T>& other)
0155 : descriptor(other) { }
0156
0157
0158 explicit dd4hep_file_source(handle_type h, dd4hep_file_flags flags)
0159 : descriptor(h,flags) { }
0160
0161
0162 explicit dd4hep_file_source(const char* name, BOOST_IOS::openmode mode = BOOST_IOS::in)
0163 : descriptor(name,mode) { }
0164
0165
0166 void open(handle_type h, dd4hep_file_flags flags)
0167 { this->descriptor::open(h, flags); }
0168
0169
0170 void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in)
0171 { this->descriptor::open(path,mode); }
0172
0173
0174 void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
0175 { open(path.c_str(), mode); }
0176
0177
0178 template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
0179 { open(detail_path(path), mode); }
0180 };
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 template <typename T>
0201 class dd4hep_file_sink : private dd4hep_file<T> {
0202 public:
0203 typedef dd4hep_file<T> descriptor;
0204
0205 struct category : boost::iostreams::output_seekable,
0206 boost::iostreams::device_tag,
0207 boost::iostreams::closable_tag { };
0208 typedef typename descriptor::handle_type handle_type;
0209 typedef typename descriptor::char_type char_type;
0210 using descriptor::is_open;
0211 using descriptor::close;
0212 using descriptor::write;
0213 using descriptor::seek;
0214 using descriptor::handle;
0215
0216
0217 dd4hep_file_sink() { }
0218
0219
0220 dd4hep_file_sink(const dd4hep_file_sink<T>& other)
0221 : descriptor(other) { }
0222
0223
0224 explicit dd4hep_file_sink(handle_type fd, dd4hep_file_flags flags)
0225 : descriptor(fd, flags) { }
0226
0227
0228 explicit dd4hep_file_sink(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out)
0229 : descriptor(path.c_str(), mode) { }
0230
0231
0232 explicit dd4hep_file_sink(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0233 : descriptor(path, mode) { }
0234
0235
0236 template<typename Path>
0237 explicit dd4hep_file_sink(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0238 : descriptor(detail_path(path), mode) { }
0239
0240
0241 void open(handle_type fd, dd4hep_file_flags flags)
0242 { this->descriptor::open(fd,flags); }
0243
0244
0245 void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0246 { open(path.c_str(),mode); }
0247
0248
0249 void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0250 { this->descriptor::open(path, mode); }
0251
0252
0253 template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0254 { open(detail_path(path), mode); }
0255 };
0256 }
0257
0258
0259 #ifdef __GNUC__
0260 #pragma GCC diagnostic pop
0261 #endif
0262
0263 #endif