Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:31

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 PARSERS_SPIRIT_TOSTREAM_H
0014 #define PARSERS_SPIRIT_TOSTREAM_H 1
0015 // ============================================================================
0016 // Include files
0017 #include "Parsers/config.h"
0018 // ============================================================================
0019 // STD & STL
0020 // ============================================================================
0021 #include <iostream>
0022 #include <iomanip>
0023 #include <vector>
0024 #include <map>
0025 #include <set>
0026 #include <list>
0027 #include <deque>
0028 #include <string>
0029 #include <sstream>
0030 
0031 // ============================================================================
0032 /** @file dd4hep/ToStream.h
0033  *  implemenattiono fvarioud functions for streaming.
0034  *  this functionality is essenital for usag eof varuodu types as property for
0035  *  the various dd4hep components
0036  *  @attention the implemenation of the specific specializations must be done
0037  *                    before the inclusion of this file
0038  *  @todo ToStream.h : reimplement in terms of functors, to allow
0039  *                     easier especializations
0040  */
0041 // ============================================================================
0042 
0043 /// Namespace for the AIDA detector description toolkit
0044 namespace dd4hep {
0045   // ==========================================================================
0046   /// Utility namespace to support boost::spirit
0047   namespace Parsers {
0048 
0049     template <class TYPE>
0050     std::ostream& toStream(const TYPE& obj, std::ostream& s);
0051 
0052     // ========================================================================
0053     /** the generic implementation of the printout to the std::ostream
0054      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0055      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0056      *  @date 2006-05-12
0057      */
0058     template <class TYPE>
0059     std::ostream& toStream_(const TYPE& obj, std::ostream& s);
0060     // ========================================================================
0061     /// the helper function to print the sequence
0062     /** @param first (INPUT)  begin-iterator for the sequence
0063      *  @param last  (INPUT)  end-iterator for the sequence
0064      *  @param s     (UPDATE) the stream itself
0065      *  @param open  (INPUT)  "open"-symbol
0066      *  @param close (INPUT)  "close"-symbol
0067      *  @param delim (INPUT)  "delimiter"-symbol
0068      *  @return the stream
0069      *  @author Vanya BELYAEV Ivan.BElyaev@nikhef.nl
0070      *  @date 2009-09-15
0071      */
0072     template <class ITERATOR>
0073     inline std::ostream& toStream_(ITERATOR first, // begin of the sequence
0074                                   ITERATOR last,                            //   end of the sequence
0075                                   std::ostream& s,                          //            the stream
0076                                   const std::string& open,                  //               opening
0077                                   const std::string& close,                 //               closing
0078                                   const std::string& delim);                //             delimiter
0079     // ========================================================================
0080     /// the printtout of the strings.
0081     /** the string is printed a'la Python using the quotes
0082      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0083      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0084      *  @date 2006-05-12
0085      */
0086     inline std::ostream& toStream_(const std::string& obj, std::ostream& s) {
0087       if (std::string::npos == obj.find('\'')) {
0088         s << "\'" << obj << "\'";
0089       }
0090       else {
0091         s << "\"" << obj << "\"";
0092       }
0093       return s;
0094     }
0095     /// the printout of boolean values "a'la Python"
0096     /** @author Vanya BELYAEV ibelyaev@physics.syr.edu
0097      *  @date 2006-09-09
0098      */
0099     inline std::ostream& toStream_(const bool obj, std::ostream& s) {
0100       return s << (obj ? "True" : "False");
0101     }
0102     /// the printout of float values with the reasonable precision
0103     /** @author Vanya BELYAEV ibelyaev@physics.syr.edu
0104      *  @date 2006-09-09
0105      */
0106     inline std::ostream& toStream_(const float obj, std::ostream& s, const int prec = 6) {
0107       const int p = s.precision();
0108       return s << std::setprecision(prec) << obj << std::setprecision(p);
0109     }
0110     /// the printout of double values with the reasonable precision
0111     /** @author Vanya BELYAEV ibelyaev@physics.syr.edu
0112      *  @date 2006-09-09
0113      */
0114     inline std::ostream& toStream_(const double obj, std::ostream& s, const int prec = 8) {
0115       const int p = s.precision();
0116       return s << std::setprecision(prec) << obj << std::setprecision(p);
0117     }
0118     /// the printout of long double values with the reasonable precision
0119     /** @author Vanya BELYAEV ibelyaev@physics.syr.edu
0120      *  @date 2006-09-09
0121      */
0122     inline std::ostream& toStream_(const long double obj, std::ostream& s, const int prec = 10) {
0123       const int p = s.precision();
0124       return s << std::setprecision(prec) << obj << std::setprecision(p);
0125     }
0126     // ========================================================================
0127     /** the partial template specialization of
0128      *  <c>std::pair<KTYPE,VTYPE></c> printout
0129      *  the pair is printed a'la Python tuple: " ( a , b )"
0130      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0131      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0132      *  @date 2006-05-12
0133      */
0134     template <class KTYPE, class VTYPE>
0135     inline std::ostream& toStream_(const std::pair<KTYPE, VTYPE>& obj, std::ostream& s) {
0136       s << "( ";
0137       toStream_(obj.first, s);
0138       s << " , ";
0139       toStream_(obj.second, s);
0140       return s << " )";
0141     }
0142     // ========================================================================
0143     /** the partial template specialization of <c>std::vector<TYPE,ALLOCATOR></c>
0144      *  printout. The vector is printed a'la Python list: "[ a, b, c ]"
0145      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0146      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0147      *  @date 2006-05-12
0148      */
0149     template <class TYPE, class ALLOCATOR>
0150     inline std::ostream& toStream_(const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s) {
0151       return toStream_(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
0152     }
0153     // ========================================================================
0154     /** the partial template specialization of <c>std::list<TYPE,ALLOCATOR></c>
0155      *  printout. The vector is printed a'la Python list: "[ a, b, c ]"
0156      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0157      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0158      *  @date 2007-04-08
0159      */
0160     template <class TYPE, class ALLOCATOR>
0161     inline std::ostream& toStream_(const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s) {
0162       return toStream_(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
0163     }
0164     // ========================================================================
0165     /** the partial template specialization of <c>std::deque<TYPE,ALLOCATOR></c>
0166      *  printout. The vector is printed a'la Python list: "[ a, b, c ]"
0167      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0168      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0169      *  @date 2006-05-12
0170      */
0171     template <class TYPE, class ALLOCATOR>
0172     inline std::ostream& toStream_(const std::deque<TYPE, ALLOCATOR>& obj, std::ostream& s) {
0173       return toStream_(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
0174     }
0175     // ========================================================================
0176     /** the partial template specialization of <c>std::set<TYPE,CMP,ALLOCATOR></c>
0177      *  printout. The vector is printed a'la Python list: "[ a, b, c ]"
0178      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0179      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0180      *  @date 2006-05-12
0181      */
0182     template <class TYPE, class CMP, class ALLOCATOR>
0183     inline std::ostream& toStream_(const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s) {
0184       return toStream_(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
0185     }
0186     // ========================================================================
0187     /** the partial template specialization of
0188      *  <c>std::map<KTYPE,VTYPE,CMP,ALLOCATOR></c> printout
0189      *  the map is printed a'la Python dict: " ( a : b , c: d , e : f )"
0190      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0191      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0192      *  @date 2006-05-12
0193      */
0194     template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
0195     inline std::ostream& toStream_(const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s) {
0196       s << "{ ";
0197       for (typename std::map<KTYPE, VTYPE, CMP, ALLOCATOR>::const_iterator cur = obj.begin(); obj.end() != cur; ++cur) {
0198         if (obj.begin() != cur) {
0199           s << " , ";
0200         }
0201         toStream_(cur->first, s);
0202         s << " : ";
0203         toStream_(cur->second, s);
0204       }
0205       return s << " }";
0206     }
0207 
0208     // ========================================================================
0209     /** the specialization for C-arrays, a'la python tuple
0210      *  @author Vanya BELYAEV Ivan.Belyaev@nikhenf.nl
0211      *  @date 2009-10-05
0212      */
0213     template <class TYPE, unsigned int N>
0214     std::ostream& toStream_(TYPE (&obj)[N], std::ostream& s) {
0215       return toStream_(obj, obj + N, s, "( ", " )", " , ");
0216     }
0217     // ========================================================================
0218     /** the specialization for C-arrays, a'la python tuple
0219      *  @author Vanya BELYAEV Ivan.Belyaev@nikhenf.nl
0220      *  @date 2009-10-05
0221      */
0222     template <class TYPE, unsigned int N>
0223     std::ostream& toStream_(const TYPE (&obj)[N], std::ostream& s) {
0224       return toStream_(obj, obj + N, s, "( ", " )", " , ");
0225     }
0226     // ========================================================================
0227     /** the specialization for C-string, a'la python tuple
0228      *  @author Vanya BELYAEV Ivan.Belyaev@nikhenf.nl
0229      *  @date 2009-10-05
0230      */
0231     template <unsigned int N>
0232     std::ostream& toStream_(char (&obj)[N], std::ostream& s) {
0233       return toStream_(std::string(obj, obj + N), s);
0234     }
0235     // ========================================================================
0236     /** the specialization for C-string, a'la python tuple
0237      *  @author Vanya BELYAEV Ivan.Belyaev@nikhenf.nl
0238      *  @date 2009-10-05
0239      */
0240     template <unsigned int N>
0241     std::ostream& toStream_(const char (&obj)[N], std::ostream& s) {
0242       return toStream_(std::string(obj, obj + N), s);
0243     }
0244     // ========================================================================
0245     /** the specialization for C-string, a'la python tuple
0246      *  @author Vanya BELYAEV Ivan.Belyaev@nikhenf.nl
0247      *  @date 2009-10-05
0248      */
0249     inline std::ostream& toStream_(const char* obj, std::ostream& s) {
0250       return toStream_(std::string(obj), s);
0251     }
0252     // ========================================================================
0253     /** the generic implementation of the printout to the std::ostream
0254      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0255      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0256      *  @date 2006-05-12
0257      */
0258     template <class TYPE>
0259     inline std::ostream& toStream_(const TYPE& obj, std::ostream& s) {
0260       return s << obj;
0261     }
0262     // ========================================================================
0263     /** the helper function to print the sequence
0264      *  @param first (INPUT)  begin-iterator for the sequence
0265      *  @param last  (INPUT)  end-iterator for the sequence
0266      *  @param s     (UPDATE) the stream itself
0267      *  @param open  (INPUT)  "open"-symbol
0268      *  @param close (INPUT)  "close"-symbol
0269      *  @param delim (INPUT)  "delimiter"-symbol
0270      *  @return the stream
0271      *  @author Vanya BELYAEV Ivan.BElyaev@nikhef.nl
0272      *  @date 2009-09-15
0273      */
0274     template <class ITERATOR>
0275     inline std::ostream& toStream_(ITERATOR first,     // begin of the sequence
0276                                   ITERATOR last,                                //   end of the sequence
0277                                   std::ostream& s,                              //            the stream
0278                                   const std::string& open,                      //               opening
0279                                   const std::string& close,                     //               closing
0280                                   const std::string& delim)                     //             delimiter
0281     {
0282       s << open;
0283       for (ITERATOR curr = first; curr != last; ++curr) {
0284         if (first != curr) {
0285           s << delim;
0286         }
0287         toStream_(*curr, s);
0288       }
0289       s << close;
0290       //
0291       return s;
0292     }
0293     // ========================================================================
0294     /** the generic implementation of the type conversion to the string
0295      *  @author Alexander MAZUROV Alexander.Mazurov@gmail.com
0296      *  @author Vanya BELYAEV ibelyaev@physics.syr.edu
0297      *  @date 2006-05-12
0298      *  @see dd4hep::Parsers::toStream_
0299      *  @todo need to be compared with boost::lexical_cast
0300      */
0301     template <class TYPE>
0302     inline std::string toString(const TYPE& obj) {
0303       std::ostringstream s;
0304       std::ios::fmtflags orig_flags = s.flags();
0305       s.setf(std::ios::showpoint);   // to display correctly floats
0306       toStream_(obj, s);
0307       s.flags(orig_flags);
0308       return s.str();
0309     }
0310     // ========================================================================
0311   }//                                          end of namespace dd4hep::Parsers
0312   // ==========================================================================
0313 }//                                                     end of namespace dd4hep
0314 
0315 #ifndef DD4HEP_PARSERS_NO_ROOT
0316 #include "Math/Point3D.h"
0317 #include "Math/Vector3D.h"
0318 #include "Math/Vector4D.h"
0319 #include "Math/RotationZYX.h"
0320 
0321 /// Namespace for the AIDA detector description toolkit
0322 namespace dd4hep {
0323   // ==========================================================================
0324   /// Utility namespace to support boost::spirit
0325   namespace Parsers {
0326     // ============================================================================
0327     /// print XYZ point
0328     std::ostream& toStream_(const ROOT::Math::XYZPoint& obj, std::ostream& s);
0329     // print XYZ-vector
0330     std::ostream& toStream_(const ROOT::Math::XYZVector& obj, std::ostream& s);
0331     /// print Lorentz vector
0332     std::ostream& toStream_(const ROOT::Math::PxPyPzEVector& obj, std::ostream& s);
0333     /// print RotationZYX
0334     std::ostream& toStream_(const ROOT::Math::RotationZYX& obj, std::ostream& s);
0335     // ========================================================================
0336   }//                                          end of namespace dd4hep::Parsers
0337   // ==========================================================================
0338 }//                                                     end of namespace dd4hep
0339 #endif
0340 // ============================================================================
0341 // The END
0342 // ============================================================================
0343 #endif
0344 // ============================================================================
0345