CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DataFormats/Common/interface/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 //
00020 
00021 // user include files
00022 #include "DataFormats/Common/interface/FillView.h"
00023 #include "DataFormats/Common/interface/fwd_setPtr.h"
00024 #include "FWCore/Utilities/interface/EDMException.h"
00025 #include "FWCore/Utilities/interface/TypeWithDict.h"
00026 
00027 // system include files
00028 #include <typeinfo>
00029 #include <vector>
00030 
00031 // forward declarations
00032 namespace edm {
00033   namespace detail {
00034 
00035     template <typename COLLECTION>
00036     void
00037     reallySetPtr(COLLECTION const& coll,
00038                  std::type_info const& iToType,
00039                  unsigned long iIndex,
00040                  void const*& oPtr) {
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         std::advance(it,iIndex);
00049         element_type const* address = GetProduct<product_type>::address( it );
00050         oPtr = address;
00051       } else {
00052         static TypeWithDict const s_type(TypeWithDict(typeid(element_type)));
00053 
00054         iter it = coll.begin();
00055         std::advance(it,iIndex);
00056         element_type const* address = GetProduct<product_type>::address( it );
00057 
00058         oPtr = TypeWithDict(iToType).pointerToBaseType(address, s_type);
00059 
00060         if(0 == oPtr) {
00061           Exception::throwThis(errors::LogicError,
00062             "TypeConversionError"
00063              "edm::Ptr<> : unable to convert type ",
00064              typeid(element_type).name(),
00065              " to ",
00066              iToType.name(),
00067              "\n");
00068         }
00069       }
00070     }
00071   }
00072 
00073   template <typename T, typename A>
00074   void
00075   setPtr(std::vector<T, A> const& obj,
00076          std::type_info const& iToType,
00077          unsigned long iIndex,
00078          void const*& oPtr) {
00079     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00080   }
00081 
00082   template <typename T, typename A>
00083   void
00084   setPtr(std::list<T, A> const& obj,
00085          std::type_info const& iToType,
00086          unsigned long iIndex,
00087          void const*& oPtr) {
00088     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00089   }
00090 
00091   template <typename T, typename A>
00092   void
00093   setPtr(std::deque<T, A> const& obj,
00094          std::type_info const& iToType,
00095          unsigned long iIndex,
00096          void const*& oPtr) {
00097     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00098   }
00099 
00100   template <typename T, typename A, typename Comp>
00101   void
00102   setPtr(std::set<T, A, Comp> const& obj,
00103          std::type_info const& iToType,
00104          unsigned long iIndex,
00105          void const*& oPtr) {
00106     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
00107   }
00108 
00109 }
00110 
00111 #endif