CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
setPtr.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_setPtr_h
2 #define DataFormats_Common_setPtr_h
3 // -*- C++ -*-
4 //
5 // Package: Common
6 // Class : setPtr
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Sat Oct 20 11:45:38 CEST 2007
19 //
20 
21 // user include files
25 #include "Reflex/Object.h"
26 #include "Reflex/Type.h"
27 
28 // system include files
29 #include <typeinfo>
30 #include <vector>
31 
32 // forward declarations
33 namespace edm {
34  namespace detail {
35 
36  template <typename COLLECTION>
37  void
38  reallySetPtr(COLLECTION const& coll,
39  std::type_info const& iToType,
40  unsigned long iIndex,
41  void const*& oPtr) {
42  typedef COLLECTION product_type;
43  typedef typename GetProduct<product_type>::element_type element_type;
44  typedef typename product_type::const_iterator iter;
45  typedef typename product_type::size_type size_type;
46 
47  if(iToType == typeid(element_type)) {
48  iter it = coll.begin();
49  std::advance(it,iIndex);
50  element_type const* address = GetProduct<product_type>::address( it );
51  oPtr = address;
52  } else {
53  using Reflex::Type;
54  using Reflex::Object;
55  static Type const s_type(Type::ByTypeInfo(typeid(element_type)));
56 
57  iter it = coll.begin();
58  std::advance(it,iIndex);
59  element_type const* address = GetProduct<product_type>::address( it );
60 
61  // The const_cast below is needed because
62  // Object's constructor requires a pointer to
63  // non-const void, although the implementation does not, of
64  // course, modify the object to which the pointer points.
65  Object obj(s_type, const_cast<void*>(static_cast<void const*>(address)));
66  Object cast = obj.CastObject(Type::ByTypeInfo(iToType));
67  if(0 != cast.Address()) {
68  oPtr = cast.Address(); // returns void*, after pointer adjustment
69  } else {
71  "TypeConversionError"
72  "edm::Ptr<> : unable to convert type ",
73  typeid(element_type).name(),
74  " to ",
75  iToType.name(),
76  "\n");
77  }
78  }
79  }
80  }
81 
82  template <typename T, typename A>
83  void
84  setPtr(std::vector<T, A> const& obj,
85  std::type_info const& iToType,
86  unsigned long iIndex,
87  void const*& oPtr) {
88  detail::reallySetPtr(obj, iToType, iIndex, oPtr);
89  }
90 
91  template <typename T, typename A>
92  void
93  setPtr(std::list<T, A> const& obj,
94  std::type_info const& iToType,
95  unsigned long iIndex,
96  void const*& oPtr) {
97  detail::reallySetPtr(obj, iToType, iIndex, oPtr);
98  }
99 
100  template <typename T, typename A>
101  void
102  setPtr(std::deque<T, A> const& obj,
103  std::type_info const& iToType,
104  unsigned long iIndex,
105  void const*& oPtr) {
106  detail::reallySetPtr(obj, iToType, iIndex, oPtr);
107  }
108 
109  template <typename T, typename A, typename Comp>
110  void
111  setPtr(std::set<T, A, Comp> const& obj,
112  std::type_info const& iToType,
113  unsigned long iIndex,
114  void const*& oPtr) {
115  detail::reallySetPtr(obj, iToType, iIndex, oPtr);
116  }
117 
118 }
119 
120 #endif
char * address
Definition: mlp_lapack.h:14
static const element_type * address(const iter &i)
Definition: GetProduct.h:34
uint16_t size_type
void setPtr(std::vector< T, A > const &obj, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:84
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
COLLECTION::value_type element_type
Definition: GetProduct.h:32
void reallySetPtr(COLLECTION const &coll, std::type_info const &iToType, unsigned long iIndex, void const *&oPtr)
Definition: setPtr.h:38
JetCorrectorParametersCollection coll
Definition: classes.h:14