CMS 3D CMS Logo

setPtr.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_setPtr_h
00002 #define DataFormats_Common_setPtr_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Common
00006 // Class  :     setPtr
00007 // 
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Sat Oct 20 11:45:38 CEST 2007
00019 // $Id: setPtr.h,v 1.3 2008/04/22 22:17:35 wmtan Exp $
00020 //
00021 
00022 // system include files
00023 
00024 // user include files
00025 #include "DataFormats/Common/interface/FillView.h"
00026 #include "Reflex/Object.h"
00027 #include "Reflex/Type.h"
00028 
00029 // forward declarations
00030 namespace edm {
00031   namespace detail {
00032     
00033     
00034     template <class COLLECTION>
00035     void
00036     reallySetPtr(COLLECTION const& coll,
00037                  const std::type_info& iToType,
00038                  unsigned long iIndex,
00039                  void const*& oPtr)
00040     {
00041       typedef COLLECTION                            product_type;
00042       typedef typename GetProduct<product_type>::element_type     element_type;
00043       typedef typename product_type::const_iterator iter;
00044       typedef typename product_type::size_type      size_type;
00045       
00046       if(iToType == typeid(element_type)) {
00047         iter it = coll.begin();
00048         advance(it,iIndex);
00049         element_type const* address = GetProduct<product_type>::address( it );
00050         oPtr = address;
00051       } else {
00052         using ROOT::Reflex::Type;
00053         using ROOT::Reflex::Object;
00054         static const Type s_type(Type::ByTypeInfo(typeid(element_type)));
00055 
00056         iter it = coll.begin();
00057         advance(it,iIndex);
00058         element_type const* address = GetProduct<product_type>::address( it );
00059 
00060         // The const_cast below is needed because
00061         // Object's constructor requires a pointer to
00062         // non-const void, although the implementation does not, of
00063         // course, modify the object to which the pointer points.
00064         Object obj(s_type, const_cast<void*>(static_cast<const void*>(address)));
00065         Object cast = obj.CastObject(Type::ByTypeInfo(iToType));
00066         if(0 != cast.Address()) {
00067           oPtr = cast.Address(); // returns void*, after pointer adjustment
00068         } else {
00069           throw cms::Exception("TypeConversionError")
00070           << "edm::Ptr<> : unable to convert type " << typeid(element_type).name()
00071           << " to " << iToType.name() << "\n";
00072         }
00073       }
00074     }
00075   }
00076   
00077   template <class T, class A>
00078   void
00079   setPtr(std::vector<T,A> const& obj,
00080          const std::type_info& iToType,
00081          unsigned long iIndex,
00082          void const*& oPtr) {
00083     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00084   }
00085   
00086   template <class T, class A>
00087   void
00088   setPtr(std::list<T,A> const& obj,
00089          const std::type_info& iToType,
00090          unsigned long iIndex,
00091          void const*& oPtr) {
00092     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00093   }
00094   
00095   template <class T, class A>
00096   void
00097   setPtr(std::deque<T,A> const& obj,
00098          const std::type_info& iToType,
00099          unsigned long iIndex,
00100          void const*& oPtr) {
00101     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00102   }
00103   
00104   template <class T, class A, class Comp>
00105   void
00106   setPtr(std::set<T,A,Comp> const& obj,
00107          const std::type_info& iToType,
00108          unsigned long iIndex,
00109          void const*& oPtr) {
00110     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00111   }
00112 
00113 }
00114 
00115 #endif

Generated on Tue Jun 9 17:30:27 2009 for CMSSW by  doxygen 1.5.4