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
ToStream.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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 DD4HEPPROPERTYPARSERS_PARSERVALUETOSTREAM_H
14 #define DD4HEPPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
15 // ============================================================================
16 // Include files
17 // ============================================================================
18 // STD & STL
19 // ============================================================================
20 #include <iostream>
21 #include <iomanip>
22 #include <vector>
23 #include <map>
24 #include <set>
25 #include <list>
26 #include <deque>
27 #include <string>
28 #include <sstream>
29 
30 // ============================================================================
40 // ============================================================================
41 
43 namespace DD4hep {
44  // ==========================================================================
46  namespace Utils {
47  // ========================================================================
53  template <class TYPE>
54  std::ostream& toStream(const TYPE& obj, std::ostream& s);
55  // ========================================================================
57 
67  template <class ITERATOR>
68  inline std::ostream& toStream(ITERATOR first, // begin of the sequence
69  ITERATOR last, // end of the sequence
70  std::ostream& s, // the stream
71  const std::string& open, // opening
72  const std::string& close, // closing
73  const std::string& delim); // delimiter
74  // ========================================================================
76 
81  inline std::ostream& toStream(const std::string& obj, std::ostream& s) {
82  if (std::string::npos == obj.find('\'')) {
83  s << "\'" << obj << "\'";
84  }
85  else {
86  s << "\"" << obj << "\"";
87  }
88  return s;
89  }
91 
94  inline std::ostream& toStream(const bool obj, std::ostream& s) {
95  return s << (obj ? "True" : "False");
96  }
98 
101  inline std::ostream& toStream(const float obj, std::ostream& s, const int prec = 6) {
102  const int p = s.precision();
103  return s << std::setprecision(prec) << obj << std::setprecision(p);
104  }
106 
109  inline std::ostream& toStream(const double obj, std::ostream& s, const int prec = 8) {
110  const int p = s.precision();
111  return s << std::setprecision(prec) << obj << std::setprecision(p);
112  }
114 
117  inline std::ostream& toStream(const long double obj, std::ostream& s, const int prec = 10) {
118  const int p = s.precision();
119  return s << std::setprecision(prec) << obj << std::setprecision(p);
120  }
121  // ========================================================================
129  template <class KTYPE, class VTYPE>
130  inline std::ostream& toStream(const std::pair<KTYPE, VTYPE>& obj, std::ostream& s) {
131  s << "( ";
132  toStream(obj.first, s);
133  s << " , ";
134  toStream(obj.second, s);
135  return s << " )";
136  }
137  // ========================================================================
144  template <class TYPE, class ALLOCATOR>
145  inline std::ostream& toStream(const std::vector<TYPE, ALLOCATOR>& obj, std::ostream& s) {
146  return toStream(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
147  }
148  // ========================================================================
155  template <class TYPE, class ALLOCATOR>
156  inline std::ostream& toStream(const std::list<TYPE, ALLOCATOR>& obj, std::ostream& s) {
157  return toStream(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
158  }
159  // ========================================================================
166  template <class TYPE, class ALLOCATOR>
167  inline std::ostream& toStream(const std::deque<TYPE, ALLOCATOR>& obj, std::ostream& s) {
168  return toStream(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
169  }
170  // ========================================================================
177  template <class TYPE, class CMP, class ALLOCATOR>
178  inline std::ostream& toStream(const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream& s) {
179  return toStream(obj.begin(), obj.end(), s, "[ ", " ]", " , ");
180  }
181  // ========================================================================
189  template <class KTYPE, class VTYPE, class CMP, class ALLOCATOR>
190  inline std::ostream& toStream(const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream& s) {
191  s << "{ ";
192  for (typename std::map<KTYPE, VTYPE, CMP, ALLOCATOR>::const_iterator cur = obj.begin(); obj.end() != cur; ++cur) {
193  if (obj.begin() != cur) {
194  s << " , ";
195  }
196  toStream(cur->first, s);
197  s << " : ";
198  toStream(cur->second, s);
199  }
200  return s << " }";
201  }
202 
203  // ========================================================================
208  template <class TYPE, unsigned int N>
209  std::ostream& toStream(TYPE (&obj)[N], std::ostream& s) {
210  return toStream(obj, obj + N, s, "( ", " )", " , ");
211  }
212  // ========================================================================
217  template <class TYPE, unsigned int N>
218  std::ostream& toStream(const TYPE (&obj)[N], std::ostream& s) {
219  return toStream(obj, obj + N, s, "( ", " )", " , ");
220  }
221  // ========================================================================
226  template <unsigned int N>
227  std::ostream& toStream(char (&obj)[N], std::ostream& s) {
228  return toStream(std::string(obj, obj + N), s);
229  }
230  // ========================================================================
235  template <unsigned int N>
236  std::ostream& toStream(const char (&obj)[N], std::ostream& s) {
237  return toStream(std::string(obj, obj + N), s);
238  }
239  // ========================================================================
244  inline std::ostream& toStream(const char* obj, std::ostream& s) {
245  return toStream(std::string(obj), s);
246  }
247  // ========================================================================
253  template <class TYPE>
254  inline std::ostream& toStream(const TYPE& obj, std::ostream& s) {
255  return s << obj;
256  }
257  // ========================================================================
269  template <class ITERATOR>
270  inline std::ostream& toStream(ITERATOR first, // begin of the sequence
271  ITERATOR last, // end of the sequence
272  std::ostream& s, // the stream
273  const std::string& open, // opening
274  const std::string& close, // closing
275  const std::string& delim) // delimiter
276  {
277  s << open;
278  for (ITERATOR curr = first; curr != last; ++curr) {
279  if (first != curr) {
280  s << delim;
281  }
282  toStream(*curr, s);
283  }
284  s << close;
285  //
286  return s;
287  }
288  // ========================================================================
296  template <class TYPE>
297  inline std::string toString(const TYPE& obj) {
298  std::ostringstream s;
299  std::ios::fmtflags orig_flags = s.flags();
300  s.setf(std::ios::showpoint); // to display correctly floats
301  toStream(obj, s);
302  s.flags(orig_flags);
303  return s.str();
304  }
305  // ========================================================================
306  }// end of namespace DD4hep::Utils
307  // ==========================================================================
308 }// end of namespace DD4hep
309 
310 #ifndef DD4HEP_PARSERS_NO_ROOT
311 #include "Math/Point3D.h"
312 #include "Math/Vector3D.h"
313 #include "Math/Vector4D.h"
315 namespace DD4hep {
316  // ==========================================================================
318  namespace Utils {
319  // ============================================================================
321  std::ostream& toStream(const ROOT::Math::XYZPoint& obj, std::ostream& s);
322  // print XYZ-vector
323  std::ostream& toStream(const ROOT::Math::XYZVector& obj, std::ostream& s);
325  std::ostream& toStream(const ROOT::Math::PxPyPzEVector& obj, std::ostream& s);
326  // ========================================================================
327  }// end of namespace DD4hep::Utils
328  // ==========================================================================
329 }// end of namespace DD4hep
330 #endif
331 // ============================================================================
332 // The END
333 // ============================================================================
334 #endif
335 // ============================================================================
336 
std::string toString(const TYPE &obj)
Definition: ToStream.h:297
TGeoShape * s
Definition: Volumes.cpp:294
std::ostream & toStream(const Property &result, std::ostream &os)