HepMC event record
include/HepMC/Common.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2015 The HepMC collaboration (see AUTHORS for details)
5 #ifndef HEPMC_COMMON_H
6 #define HEPMC_COMMON_H
7 
8 #include <vector>
9 #include <map>
10 #include <string>
11 #include <stdexcept>
12 #include <iostream>
13 #include <memory>
14 
15 namespace HepMC {
16 
17 
18  // Use unqualified namespace std within the HepMC namespace
19  using namespace std;
20 
21 
22  /// Handy number squaring function
23  template <typename NUM>
24  inline NUM sqr(NUM x) { return x*x; }
25 
26 
27  /** @brief List of methods of searching starting from a particle or vertex
28  @todo Add 'family'='parents'+'children' and 'relatives'='ancestors'+'descendants'(+'siblings')?
29  */
30  enum Relationship {
31  ANCESTORS = 0, FIND_ANCESTORS = 0, FIND_ALL_ANCESTORS = 0, ancestors = 0,
32  DESCENDANTS = 1, FIND_DESCENDANTS = 1, FIND_ALL_DESCENDANTS = 1, descendants = 1,
33  PARENTS = 2, FIND_PARENTS = 2, FIND_MOTHERS = 2, parents = 2,
34  CHILDREN = 3, FIND_CHILDREN = 3, FIND_DAUGHTERS = 3, children = 3,
35  PRODUCTION_SIBLINGS = 4, FIND_PRODUCTION_SIBLINGS = 4
36  };
37 
38  /// Compatibility name
40  #ifndef HEPMC_NO_DEPRECATED
41  /// Compatibility name
43  #endif
44 
45 }
46 
47 
48 /// Neater/extensible C++11 availability test
49 #if __cplusplus >= 201103L
50 #define HEPMC_HAS_CXX11
51 #endif
52 #if !defined(HEPMC_HAS_CXX11) && (__GNUC__) && (__cplusplus) && (__GXX_EXPERIMENTAL_CXX0X__)
53 #define HEPMC_HAS_CXX0X_GCC_ONLY
54 #endif
55 
56 
57 /// Define a FOREACH directive
58 #ifdef HEPMC_HAS_CXX11
59 #define FOREACH( iterator, container ) for ( iterator: container )
60 #else
61 #if !defined(__CINT__)
62 /* This code was adapted from
63  * https://github.com/assimp/assimp/blob/master/code/BoostWorkaround/boost/foreach.hpp
64  * to rid of boost dependency.
65  */
66 ///////////////////////////////////////////////////////////////////////////////
67 // A stripped down version of FOREACH for
68 // illustration purposes. NOT FOR GENERAL USE.
69 // For a complete implementation, see BOOST_FOREACH at
70 // http://boost-sandbox.sourceforge.net/vault/index.php?directory=eric_niebler
71 //
72 // Copyright 2004 Eric Niebler.
73 // Distributed under the Boost Software License, Version 1.0. (See
74 // accompanying file LICENSE_1_0.txt or copy at
75 // http://www.boost.org/LICENSE_1_0.txt)
76 //
77 // Adapted to Assimp November 29th, 2008 (Alexander Gessler).
78 // Added code to handle both const and non-const iterators, simplified some
79 // parts.
80 //////////////////////////////////////////////////////////////////////////////
81 namespace hepmc_embedded_boost {
82 namespace foreach_detail {
83 
84 ///////////////////////////////////////////////////////////////////////////////
85 // auto_any
86 
87 struct auto_any_base
88 {
89  operator bool() const { return false; }
90 };
91 
92 template<typename T>
93 struct auto_any : auto_any_base
94 {
95  auto_any(T const& t) : item(t) {}
96  mutable T item;
97 };
98 
99 template<typename T>
100 T& auto_any_cast(auto_any_base const& any)
101 {
102  return static_cast<auto_any<T> const&>(any).item;
103 }
104 
105 ///////////////////////////////////////////////////////////////////////////////
106 // FOREACH helper function
107 
108 template<typename T>
109 auto_any<typename T::const_iterator> begin(T const& t)
110 {
111  return t.begin();
112 }
113 
114 template<typename T>
115 auto_any<typename T::const_iterator> end(T const& t)
116 {
117  return t.end();
118 }
119 
120 // iterator
121 template<typename T>
122 bool done(auto_any_base const& cur, auto_any_base const& end, T&)
123 {
124  typedef typename T::iterator iter_type;
125  return auto_any_cast<iter_type>(cur) == auto_any_cast<iter_type>(end);
126 }
127 
128 template<typename T>
129 void next(auto_any_base const& cur, T&)
130 {
131  ++auto_any_cast<typename T::iterator>(cur);
132 }
133 
134 template<typename T>
135 typename T::reference deref(auto_any_base const& cur, T&)
136 {
137  return *auto_any_cast<typename T::iterator>(cur);
138 }
139 
140 } // end foreach_detail
141 
142 ///////////////////////////////////////////////////////////////////////////////
143 // FOREACH
144 
145 #define FOREACH(item, container) \
146  if(hepmc_embedded_boost::foreach_detail::auto_any_base const& b = hepmc_embedded_boost::foreach_detail::begin(container)) {} else \
147  if(hepmc_embedded_boost::foreach_detail::auto_any_base const& e = hepmc_embedded_boost::foreach_detail::end(container)) {} else \
148  for(;!hepmc_embedded_boost::foreach_detail::done(b,e,container); hepmc_embedded_boost::foreach_detail::next(b,container)) \
149  if (bool ugly_and_unique_break = false) {} else \
150  for(item = hepmc_embedded_boost::foreach_detail::deref(b,container); !ugly_and_unique_break; ugly_and_unique_break = true)
151 
152 } // end hepmc_embedded_boost
153 #endif
154 #endif
155 
156 
157 /// Deprecation macro
158 #ifndef HEPMC_DEPRECATED
159 #define HEPMC_DEPRECATED(x)
160 #endif
161 /// @todo Activate in version 3.1.0
162 // #ifndef HEPMC_DEPRECATED
163 // #if __GNUC__ && __cplusplus && HEPMC_NO_DEPRECATION_WARNINGS == 0
164 // #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
165 // #if GCC_VERSION >= 40500
166 // #define HEPMC_DEPRECATED(x) __attribute__((deprecated(x)))
167 // #else
168 // #define HEPMC_DEPRECATED(x) __attribute__((deprecated))
169 // #endif
170 // #else
171 // #define HEPMC_DEPRECATED(x)
172 // #endif
173 // #endif
174 
175 
176 #endif
Neater/extensible C++11 availability test.
Relationship FilterParticle
Compatibility name.
STL namespace.
Relationship IteratorRange
Compatibility name.
NUM sqr(NUM x)
Handy number squaring function.
Definition of template class SmartPointer.
Relationship
List of methods of searching starting from a particle or vertex.