Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:25

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DDG4_IOSTREAMS_H
0014 #define DDG4_IOSTREAMS_H
0015 
0016 // C/C++ include files
0017 #include <string>
0018 
0019 #ifdef __GNUC__
0020 #pragma GCC diagnostic push
0021 #pragma GCC diagnostic ignored "-Wshadow" // Code that causes warning goes here
0022 #endif
0023 
0024 // booost iostreams include files
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 // Boost spits out an error if __GNUC__ is defined!
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 // Forward declarations
0041 class TFile;
0042 
0043 /// Namespace for the AIDA detector description toolkit
0044 namespace dd4hep {
0045 
0046   // Forward declarations
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   /// dd4hep file handling extension to boost::iostreams
0056   /**
0057    *  Basic file handling for data sources and data sinks.
0058    *  Please see boost::iostreams for details.
0059    *  This got inspired by boost::iostreams::device::file_descriptor
0060    *
0061    *  The final aim is to hide all this behind a TFile object,
0062    *  so that we get the entire network file handling for free!
0063    *
0064    *  Note:
0065    *  Credits go to a large extend to the authors of boost::iostreams.
0066    *  Without their work I could not have done this!
0067    *
0068    *  \author  M.Frank
0069    *  \version 1.0
0070    *  \ingroup DD4HEP
0071    *  \see http://www.boost.org/libs/iostreams for further documentation.
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     /// Helper structure to define boost::iostreams
0082     struct  category : boost::iostreams::seekable_device_tag, boost::iostreams::closable_tag { };
0083 
0084     /// Default constructor
0085     dd4hep_file() = default;
0086     /// Constructors taking file desciptors
0087     dd4hep_file(handle_type fd, dd4hep_file_flags);
0088     /// Constructors taking file desciptors
0089     dd4hep_file(const char* fname, BOOST_IOS::openmode mode);
0090     /// Default destructor
0091     //~dd4hep_file() = default;
0092     /// open overloads taking file descriptors
0093     void open(handle_type fd, dd4hep_file_flags flags);
0094     /// open overload taking C-style string
0095     void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in | BOOST_IOS::out );
0096     /// Close the file stream
0097     void close();
0098     /// Read from input stream
0099     std::streamsize read(char_type* s, std::streamsize n);
0100     /// Write to output stream
0101     std::streamsize write(const char_type* s, std::streamsize n);
0102     /// Direct access: set file pointer of the stream
0103     std::streampos seek(stream_offset off, BOOST_IOS::seekdir way);
0104     /// Check if the file stream is opened
0105     bool is_open() const  {  return m_handle != 0;  }
0106     /// Access to native stream handle
0107     handle_type handle() const   {   return m_handle; }
0108 
0109   private:
0110     /// Native stream handle
0111     handle_type       m_handle = 0;
0112     /// Stream flag(s)
0113     dd4hep_file_flags m_flag = close_handle;
0114   };
0115 
0116 
0117   /// dd4hep file source extension to boost::iostreams
0118   /**
0119    *  Basic data sources implementation.
0120    *  Please see boost::iostreams for details.
0121    *  This got inspired by boost::iostreams::device::file_descriptor
0122    *
0123    *  The final aim is to hide all this behind a TFile object,
0124    *  so that we get the entire network file handling for free!
0125    *
0126    *  Note:
0127    *  Credits go to a large extend to the authors of boost::iostreams.
0128    *  Without their work I could not have done this!
0129    *
0130    *  \author  M.Frank
0131    *  \version 1.0
0132    *  \ingroup DD4HEP
0133    *  \see http://www.boost.org/libs/iostreams for further documentation.
0134    */
0135   template <typename T=int> class dd4hep_file_source : private dd4hep_file<T> {
0136   public:
0137     typedef dd4hep_file<T> descriptor;
0138     /// Helper structure to define boost::iostreams
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     /// Default Constructor
0151     dd4hep_file_source() : descriptor() {  }
0152 
0153     /// Copy constructor
0154     dd4hep_file_source(const dd4hep_file_source<T>& other)
0155       : descriptor(other)      {                     }
0156 
0157     /// Constructors taking file desciptors
0158     explicit dd4hep_file_source(handle_type h, dd4hep_file_flags flags)
0159       : descriptor(h,flags)  {                       }
0160 
0161     /// Constructors taking file desciptors
0162     explicit dd4hep_file_source(const char* name, BOOST_IOS::openmode mode = BOOST_IOS::in)
0163       : descriptor(name,mode) {                      }
0164 
0165     /// open overload taking file desciptors
0166     void open(handle_type h, dd4hep_file_flags flags)
0167     {     this->descriptor::open(h, flags);              }
0168 
0169     /// open overload taking C-style string
0170     void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::in)
0171     {     this->descriptor::open(path,mode);             }
0172 
0173     /// open overload taking a std::string
0174     void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::in)
0175     {     open(path.c_str(), mode);                                 }
0176 
0177     /// open overload taking a Boost.Filesystem path
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   /// dd4hep file sink extension to boost::iostreams
0183   /**
0184    *  Basic data sink implementation.
0185    *  Please see boost::iostreams for details.
0186    *  This got inspired by boost::iostreams::device::file_descriptor
0187    *
0188    *  The final aim is to hide all this behind a TFile object,
0189    *  so that we get the entire network file handling for free!
0190    *
0191    *  Note:
0192    *  Credits go to a large extend to the authors of boost::iostreams.
0193    *  Without their work I could not have done this!
0194    *
0195    *  \author  M.Frank
0196    *  \version 1.0
0197    *  \ingroup DD4HEP
0198    *  \see http://www.boost.org/libs/iostreams for further documentation.
0199    */
0200   template <typename T>
0201   class  dd4hep_file_sink : private dd4hep_file<T> {
0202   public:
0203     typedef dd4hep_file<T> descriptor;
0204     /// Helper structure to define boost::iostreams
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     /// Default constructor
0217     dd4hep_file_sink()  {                        }
0218 
0219     /// Copy constructor
0220     dd4hep_file_sink(const dd4hep_file_sink<T>& other)
0221       : descriptor(other) {                     }
0222 
0223     /// Constructors taking file desciptors
0224     explicit dd4hep_file_sink(handle_type fd, dd4hep_file_flags flags)
0225       : descriptor(fd, flags)      {            }
0226 
0227     /// Constructor taking a std::string
0228     explicit dd4hep_file_sink(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out)
0229       : descriptor(path.c_str(), mode)     {    }
0230 
0231     /// Constructor taking a C-style string
0232     explicit dd4hep_file_sink(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0233       : descriptor(path, mode)     {            }
0234 
0235     /// Constructor taking a Boost.Filesystem path
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     /// open overloads taking file descriptors
0241     void open(handle_type fd, dd4hep_file_flags flags)
0242     {   this->descriptor::open(fd,flags);         }
0243 
0244     /// open overload taking a std::string
0245     void open(const std::string& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0246     {  open(path.c_str(),mode);                            }
0247 
0248     /// open overload taking C-style string
0249     void open(const char* path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0250     {  this->descriptor::open(path, mode);      }
0251 
0252     /// open overload taking a Boost.Filesystem path
0253     template<typename Path> void open(const Path& path, BOOST_IOS::openmode mode = BOOST_IOS::out )
0254     {  open(detail_path(path), mode);                     }
0255   };
0256 }   // End namespace boost
0257 
0258 
0259 #ifdef __GNUC__
0260 #pragma GCC diagnostic pop
0261 #endif
0262 
0263 #endif // DDG4_IOSTREAMS_H