DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IoStreams.h
Go to the documentation of this file.
1 // $Id: $
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 #ifndef DD4HEP_DD4HEP_IOSTREAMS_H
15 #define DD4HEP_DD4HEP_IOSTREAMS_H
16 
17 // C/C++ include files
18 #include <string>
19 
20 #ifdef __GNUC__
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wshadow" // Code that causes warning goes here
23 #endif
24 
25 // booost iostreams include files
26 #include <boost/iostreams/categories.hpp>
27 #include <boost/iostreams/detail/ios.hpp>
28 #include <boost/iostreams/detail/path.hpp>
29 #include <boost/iostreams/positioning.hpp>
30 
31 #ifdef __GNUC__
32 // Boost spits out an error if __GNUC__ is defined!
33 #define __DD4HEP_LOCAL_GNUC__ __GNUC__
34 #undef __GNUC__
35 #include <boost/iostreams/stream.hpp>
36 #define __GNUC__ __DD4HEP_LOCAL_GNUC__
37 #else
38 #include <boost/iostreams/stream.hpp>
39 #endif
40 
41 // Forward declarations
42 class TFile;
43 
45 namespace DD4hep {
46 
47  // Forward declarations
48  template <typename T> class dd4hep_file_source;
49  template <typename T> class dd4hep_file_sink;
50 
54  };
55 
57 
74  template <typename T> class dd4hep_file {
75  public:
76  friend class dd4hep_file_source<T>;
77  friend class dd4hep_file_sink<T>;
78  typedef T handle_type;
79  typedef char char_type;
80  typedef boost::iostreams::stream_offset stream_offset;
81  typedef boost::iostreams::detail::path detail_path;
82  struct category : boost::iostreams::seekable_device_tag, boost::iostreams::closable_tag { };
83 
84  // Default constructor
85  dd4hep_file() : m_handle(0) { }
86  // Constructors taking file desciptors
88  // Constructors taking file desciptors
89  dd4hep_file(const char* fname, BOOST_IOS::openmode mode);
90 
91  // open overloads taking file descriptors
92  void open(handle_type fd, dd4hep_file_flags flags);
93 
94  // open overload taking C-style string
95  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out );
96 
97  bool is_open() const { return m_handle != 0; }
98  void close();
99  std::streamsize read(char_type* s, std::streamsize n);
100  std::streamsize write(const char_type* s, std::streamsize n);
101  std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
102  handle_type handle() const { return m_handle; }
103  private:
106  };
107 
108 
110 
127  template <typename T=int> class dd4hep_file_source : private dd4hep_file<T> {
128  public:
130  struct category : boost::iostreams::input_seekable,
131  boost::iostreams::device_tag,
132  boost::iostreams::closable_tag { };
135  using descriptor::is_open;
136  using descriptor::close;
137  using descriptor::read;
138  using descriptor::seek;
139  using descriptor::handle;
140 
143 
146  : descriptor(other) { }
147 
150  : descriptor(h,flags) { }
151 
153  explicit dd4hep_file_source(const char* name, BOOST_IOS::openmode mode = BOOST_IOS::in)
154  : descriptor(name,mode) { }
155 
158  { this->descriptor::open(h, flags); }
159 
161  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in)
162  { this->descriptor::open(path,mode); }
163 
165  void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
166  { open(path.c_str(), mode); }
167 
169  template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
170  { open(detail_path(path), mode); }
171  };
172 
174 
191  template <typename T>
192  class dd4hep_file_sink : private dd4hep_file<T> {
193  public:
195  struct category : boost::iostreams::output_seekable,
196  boost::iostreams::device_tag,
197  boost::iostreams::closable_tag { };
200  using descriptor::is_open;
201  using descriptor::close;
202  using descriptor::write;
203  using descriptor::seek;
204  using descriptor::handle;
205 
208 
211  : descriptor(other) { }
212 
215  : descriptor(fd, flags) { }
216 
218  explicit dd4hep_file_sink(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out)
219  : descriptor(path.c_str(), mode) { }
220 
222  explicit dd4hep_file_sink(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
223  : descriptor(path, mode) { }
224 
226  template<typename Path>
227  explicit dd4hep_file_sink(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
228  : descriptor(detail_path(path), mode) { }
229 
232  { this->descriptor::open(fd,flags); }
233 
235  void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
236  { open(path.c_str(),mode); }
237 
239  void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
240  { this->descriptor::open(path, mode); }
241 
243  template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
244  { open(detail_path(path), mode); }
245  };
246 } // End namespace boost
247 
248 
249 #ifdef __GNUC__
250 #pragma GCC diagnostic pop
251 #endif
252 
253 #endif // DD4HEP_DD4HEP_IOSTREAMs_H
std::streampos seek(stream_offset off, BOOST_IOS::seekdir way)
DD4hep file handling extension to boost::iostreams.
Definition: IoStreams.h:74
void open(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking a std::string
Definition: IoStreams.h:235
descriptor::char_type char_type
Definition: IoStreams.h:134
dd4hep_file_sink(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a Boost.Filesystem path.
Definition: IoStreams.h:227
dd4hep_file_source(const char *name, BOOST_IOS::openmode mode=BOOST_IOS::in)
Constructors taking file desciptors.
Definition: IoStreams.h:153
void open(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking a Boost.Filesystem path
Definition: IoStreams.h:169
dd4hep_file_flags
Definition: IoStreams.h:51
Path handling class.
Definition: Path.h:45
TGeoShape * s
Definition: Volumes.cpp:294
dd4hep_file_source()
Default Constructor.
Definition: IoStreams.h:142
dd4hep_file_sink(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a C-style string.
Definition: IoStreams.h:222
descriptor::handle_type handle_type
Definition: IoStreams.h:198
dd4hep_file_source(const dd4hep_file_source< T > &other)
Copy constructor.
Definition: IoStreams.h:145
boost::iostreams::stream_offset stream_offset
Definition: IoStreams.h:80
dd4hep_file_sink(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
Constructor taking a std::string.
Definition: IoStreams.h:218
dd4hep_file_sink(handle_type fd, dd4hep_file_flags flags)
Constructors taking file desciptors.
Definition: IoStreams.h:214
dd4hep_file< T > descriptor
Definition: IoStreams.h:129
dd4hep_file_source(handle_type h, dd4hep_file_flags flags)
Constructors taking file desciptors.
Definition: IoStreams.h:149
void open(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking C-style string
Definition: IoStreams.h:239
boost::iostreams::detail::path detail_path
Definition: IoStreams.h:81
descriptor::handle_type handle_type
Definition: IoStreams.h:133
std::streamsize read(char_type *s, std::streamsize n)
dd4hep_file< T > descriptor
Definition: IoStreams.h:194
DD4hep file sink extension to boost::iostreams.
Definition: IoStreams.h:49
dd4hep_file_sink()
Default constructor.
Definition: IoStreams.h:207
void open(const std::string &path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking a std::string
Definition: IoStreams.h:165
void open(const char *path, BOOST_IOS::openmode mode=BOOST_IOS::in)
open overload taking C-style string
Definition: IoStreams.h:161
std::streamsize write(const char_type *s, std::streamsize n)
void open(handle_type h, dd4hep_file_flags flags)
open overload taking file desciptors
Definition: IoStreams.h:157
handle_type m_handle
Definition: IoStreams.h:104
handle_type handle() const
Definition: IoStreams.h:102
DD4hep file source extension to boost::iostreams.
Definition: IoStreams.h:48
descriptor::char_type char_type
Definition: IoStreams.h:199
void open(handle_type fd, dd4hep_file_flags flags)
dd4hep_file_sink(const dd4hep_file_sink< T > &other)
Copy constructor.
Definition: IoStreams.h:210
void open(const Path &path, BOOST_IOS::openmode mode=BOOST_IOS::out)
open overload taking a Boost.Filesystem path
Definition: IoStreams.h:243
void open(handle_type fd, dd4hep_file_flags flags)
open overloads taking file descriptors
Definition: IoStreams.h:231
bool is_open() const
Definition: IoStreams.h:97
dd4hep_file_flags m_flag
Definition: IoStreams.h:105