DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
IoStreams.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 #ifndef DDG4_IOSTREAMS_H
14 #define DDG4_IOSTREAMS_H
15 
16 // C/C++ include files
17 #include <string>
18 
19 #ifdef __GNUC__
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wshadow" // Code that causes warning goes here
22 #endif
23 
24 // booost iostreams include files
25 #include <boost/iostreams/categories.hpp>
26 #include <boost/iostreams/detail/ios.hpp>
27 #include <boost/iostreams/detail/path.hpp>
28 #include <boost/iostreams/positioning.hpp>
29 
30 #ifdef __GNUC__
31 // Boost spits out an error if __GNUC__ is defined!
32 #define __DD4HEP_LOCAL_GNUC__ __GNUC__
33 #undef __GNUC__
34 #include <boost/iostreams/stream.hpp>
35 #define __GNUC__ __DD4HEP_LOCAL_GNUC__
36 #else
37 #include <boost/iostreams/stream.hpp>
38 #endif
39 
40 // Forward declarations
41 class TFile;
42 
44 namespace dd4hep {
45 
46  // Forward declarations
47  template <typename T> class dd4hep_file_source;
48  template <typename T> class dd4hep_file_sink;
49 
52  close_handle = 3
53  };
54 
56 
73  template <typename T> class dd4hep_file {
74  public:
75  friend class dd4hep_file_source<T>;
76  friend class dd4hep_file_sink<T>;
77  typedef T handle_type;
78  typedef char char_type;
79  typedef boost::iostreams::stream_offset stream_offset;
80  typedef boost::iostreams::detail::path detail_path;
82  struct category : boost::iostreams::seekable_device_tag, boost::iostreams::closable_tag { };
83 
85  dd4hep_file() = default;
89  dd4hep_file(const char* fname, BOOST_IOS::openmode mode);
91  //~dd4hep_file() = default;
95  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out );
97  void close();
99  std::streamsize read(char_type* s, std::streamsize n);
101  std::streamsize write(const char_type* s, std::streamsize n);
103  std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
105  bool is_open() const { return m_handle != 0; }
107  handle_type handle() const { return m_handle; }
108 
109  private:
114  };
115 
116 
118 
135  template <typename T=int> class dd4hep_file_source : private dd4hep_file<T> {
136  public:
139  struct category : boost::iostreams::input_seekable,
140  boost::iostreams::device_tag,
141  boost::iostreams::closable_tag { };
144  using descriptor::is_open;
145  using descriptor::close;
146  using descriptor::read;
147  using descriptor::seek;
148  using descriptor::handle;
149 
152 
155  : descriptor(other) { }
156 
159  : descriptor(h,flags) { }
160 
162  explicit dd4hep_file_source(const char* name, BOOST_IOS::openmode mode = BOOST_IOS::in)
163  : descriptor(name,mode) { }
164 
167  { this->descriptor::open(h, flags); }
168 
170  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in)
171  { this->descriptor::open(path,mode); }
172 
174  void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
175  { open(path.c_str(), mode); }
176 
178  template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
179  { open(detail_path(path), mode); }
180  };
181 
183 
200  template <typename T>
201  class dd4hep_file_sink : private dd4hep_file<T> {
202  public:
205  struct category : boost::iostreams::output_seekable,
206  boost::iostreams::device_tag,
207  boost::iostreams::closable_tag { };
210  using descriptor::is_open;
211  using descriptor::close;
212  using descriptor::write;
213  using descriptor::seek;
214  using descriptor::handle;
215 
218 
221  : descriptor(other) { }
222 
225  : descriptor(fd, flags) { }
226 
228  explicit dd4hep_file_sink(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out)
229  : descriptor(path.c_str(), mode) { }
230 
232  explicit dd4hep_file_sink(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
233  : descriptor(path, mode) { }
234 
236  template<typename Path>
237  explicit dd4hep_file_sink(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
238  : descriptor(detail_path(path), mode) { }
239 
242  { this->descriptor::open(fd,flags); }
243 
245  void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
246  { open(path.c_str(),mode); }
247 
249  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
250  { this->descriptor::open(path, mode); }
251 
253  template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
254  { open(detail_path(path), mode); }
255  };
256 } // End namespace boost
257 
258 
259 #ifdef __GNUC__
260 #pragma GCC diagnostic pop
261 #endif
262 
263 #endif // DDG4_IOSTREAMS_H
dd4hep::dd4hep_file_flags
dd4hep_file_flags
Definition: IoStreams.h:50
dd4hep::dd4hep_file::detail_path
boost::iostreams::detail::path detail_path
Definition: IoStreams.h:80
dd4hep::dd4hep_file_sink::open
void open(handle_type fd, dd4hep_file_flags flags)
open overloads taking file descriptors
Definition: IoStreams.h:241
dd4hep::dd4hep_file_sink::open
void open(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking C-style string
Definition: IoStreams.h:249
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink()
Default constructor.
Definition: IoStreams.h:217
dd4hep::dd4hep_file_sink
dd4hep file sink extension to boost::iostreams
Definition: IoStreams.h:48
dd4hep::dd4hep_file::dd4hep_file
dd4hep_file(handle_type fd, dd4hep_file_flags)
Constructors taking file desciptors.
dd4hep::dd4hep_file::is_open
bool is_open() const
Check if the file stream is opened.
Definition: IoStreams.h:105
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a std::string.
Definition: IoStreams.h:228
dd4hep::dd4hep_file::read
std::streamsize read(char_type *s, std::streamsize n)
Read from input stream.
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink(const dd4hep_file_sink< T > &other)
Copy constructor.
Definition: IoStreams.h:220
dd4hep::never_close_handle
@ never_close_handle
Definition: IoStreams.h:51
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink(handle_type fd, dd4hep_file_flags flags)
Constructors taking file desciptors.
Definition: IoStreams.h:224
dd4hep::dd4hep_file_source::open
void open(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking a std::string
Definition: IoStreams.h:174
dd4hep::dd4hep_file_source::char_type
descriptor::char_type char_type
Definition: IoStreams.h:143
dd4hep::dd4hep_file_sink::descriptor
dd4hep_file< T > descriptor
Definition: IoStreams.h:203
dd4hep::dd4hep_file::open
void open(handle_type fd, dd4hep_file_flags flags)
Default destructor.
dd4hep::dd4hep_file_source::category
Helper structure to define boost::iostreams.
Definition: IoStreams.h:141
dd4hep::dd4hep_file_sink::handle_type
descriptor::handle_type handle_type
Definition: IoStreams.h:208
dd4hep::dd4hep_file::seek
std::streampos seek(stream_offset off, BOOST_IOS::seekdir way)
Direct access: set file pointer of the stream.
dd4hep::dd4hep_file::write
std::streamsize write(const char_type *s, std::streamsize n)
Write to output stream.
dd4hep::dd4hep_file::open
void open(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::in|BOOST_IOS::out)
open overload taking C-style string
dd4hep::dd4hep_file::stream_offset
boost::iostreams::stream_offset stream_offset
Definition: IoStreams.h:79
dd4hep::dd4hep_file::close
void close()
Close the file stream.
dd4hep::dd4hep_file_source::open
void open(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking a Boost.Filesystem path
Definition: IoStreams.h:178
dd4hep::close_handle
@ close_handle
Definition: IoStreams.h:52
dd4hep::dd4hep_file_source::dd4hep_file_source
dd4hep_file_source(const char *name, BOOST_IOS::openmode mode=BOOST_IOS::in)
Constructors taking file desciptors.
Definition: IoStreams.h:162
dd4hep::dd4hep_file_sink::open
void open(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking a Boost.Filesystem path
Definition: IoStreams.h:253
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a C-style string.
Definition: IoStreams.h:232
dd4hep::dd4hep_file_source::dd4hep_file_source
dd4hep_file_source(const dd4hep_file_source< T > &other)
Copy constructor.
Definition: IoStreams.h:154
dd4hep::dd4hep_file::dd4hep_file
dd4hep_file()=default
Default constructor.
dd4hep::dd4hep_file_source::dd4hep_file_source
dd4hep_file_source(handle_type h, dd4hep_file_flags flags)
Constructors taking file desciptors.
Definition: IoStreams.h:158
dd4hep::dd4hep_file_source::descriptor
dd4hep_file< T > descriptor
Definition: IoStreams.h:137
dd4hep::dd4hep_file_source::handle_type
descriptor::handle_type handle_type
Definition: IoStreams.h:142
dd4hep::dd4hep_file::m_flag
dd4hep_file_flags m_flag
Stream flag(s)
Definition: IoStreams.h:113
dd4hep::dd4hep_file::handle
handle_type handle() const
Access to native stream handle.
Definition: IoStreams.h:107
dd4hep::dd4hep_file_source
dd4hep file source extension to boost::iostreams
Definition: IoStreams.h:47
dd4hep::dd4hep_file::m_handle
handle_type m_handle
Native stream handle.
Definition: IoStreams.h:111
dd4hep::dd4hep_file_source::open
void open(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking C-style string
Definition: IoStreams.h:170
dd4hep::dd4hep_file_sink::open
void open(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking a std::string
Definition: IoStreams.h:245
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::dd4hep_file::char_type
char char_type
Definition: IoStreams.h:78
dd4hep::dd4hep_file_source::dd4hep_file_source
dd4hep_file_source()
Default Constructor.
Definition: IoStreams.h:151
dd4hep::dd4hep_file_sink::category
Helper structure to define boost::iostreams.
Definition: IoStreams.h:207
dd4hep::dd4hep_file_sink::char_type
descriptor::char_type char_type
Definition: IoStreams.h:209
dd4hep::dd4hep_file::category
Helper structure to define boost::iostreams.
Definition: IoStreams.h:82
dd4hep::dd4hep_file
dd4hep file handling extension to boost::iostreams
Definition: IoStreams.h:73
dd4hep::dd4hep_file_sink::dd4hep_file_sink
dd4hep_file_sink(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a Boost.Filesystem path.
Definition: IoStreams.h:237
dd4hep::dd4hep_file::dd4hep_file
dd4hep_file(const char *fname, BOOST_IOS::openmode mode)
Constructors taking file desciptors.
dd4hep::dd4hep_file_source::open
void open(handle_type h, dd4hep_file_flags flags)
open overload taking file desciptors
Definition: IoStreams.h:166
dd4hep::dd4hep_file::handle_type
T handle_type
Definition: IoStreams.h:77
dd4hep::Path
Path handling class.
Definition: Path.h:45