CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DataFormats/Common/interface/fillPtrVector.h

Go to the documentation of this file.
00001 #ifndef DataFormats_Common_fillPtrVector_h
00002 #define DataFormats_Common_fillPtrVector_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Common
00006 // Class  :     fillPtrVector
00007 //
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Sat Oct 20 11:45:38 CEST 2007
00019 //
00020 
00021 // system include files
00022 
00023 // user include files
00024 #include "DataFormats/Common/interface/FillView.h"
00025 #include "FWCore/Utilities/interface/EDMException.h"
00026 #include "FWCore/Utilities/interface/TypeWithDict.h"
00027 
00028 #include "DataFormats/Common/interface/fwd_fillPtrVector.h"
00029 
00030 #include <typeinfo>
00031 #include <vector>
00032 
00033 // forward declarations
00034 namespace edm {
00035   namespace detail {
00036     template <typename COLLECTION>
00037     void
00038     reallyfillPtrVector(COLLECTION const& coll,
00039                         std::type_info const& iToType,
00040                         std::vector<unsigned long> const& iIndicies,
00041                         std::vector<void const*>& oPtr)
00042     {
00043       typedef COLLECTION                            product_type;
00044       typedef typename GetProduct<product_type>::element_type     element_type;
00045       typedef typename product_type::const_iterator iter;
00046       typedef typename product_type::size_type      size_type;
00047 
00048       oPtr.reserve(iIndicies.size());
00049       if(iToType == typeid(element_type)) {
00050         for(std::vector<unsigned long>::const_iterator itIndex = iIndicies.begin(),
00051             itEnd = iIndicies.end();
00052             itIndex != itEnd;
00053             ++itIndex) {
00054           iter it = coll.begin();
00055           std::advance(it, *itIndex);
00056           element_type const* address = GetProduct<product_type>::address(it);
00057           oPtr.push_back(address);
00058         }
00059       } else {
00060         static TypeWithDict const s_type(typeid(element_type));
00061 
00062         for(std::vector<unsigned long>::const_iterator itIndex = iIndicies.begin(),
00063             itEnd = iIndicies.end();
00064             itIndex != itEnd;
00065             ++itIndex) {
00066           iter it = coll.begin();
00067           std::advance(it, *itIndex);
00068           element_type const* address = GetProduct<product_type>::address(it);
00069           void const* ptr = TypeWithDict(iToType).pointerToBaseType(address, s_type);
00070           if(0 != ptr) {
00071             oPtr.push_back(ptr);
00072           } else {
00073             Exception::throwThis(errors::LogicError,
00074             "TypeConversionError "
00075             "edm::PtrVector<> : unable to convert type ",
00076             typeid(element_type).name(),
00077             " to ",
00078             iToType.name(),
00079             "\n");
00080           }
00081         }
00082       }
00083     }
00084   }
00085 
00086   template <typename T, typename A>
00087   void
00088   fillPtrVector(std::vector<T, A> const& obj,
00089                 std::type_info const& iToType,
00090                 std::vector<unsigned long> const& iIndicies,
00091                 std::vector<void const*>& oPtr) {
00092     detail::reallyfillPtrVector(obj, iToType, iIndicies, oPtr);
00093   }
00094 
00095   template <typename T, typename A>
00096   void
00097   fillPtrVector(std::list<T, A> const& obj,
00098                 std::type_info const& iToType,
00099                 std::vector<unsigned long> const& iIndicies,
00100                 std::vector<void const*>& oPtr) {
00101     detail::reallyfillPtrVector(obj, iToType, iIndicies, oPtr);
00102   }
00103 
00104   template <typename T, typename A>
00105   void
00106   fillPtrVector(std::deque<T, A> const& obj,
00107                 std::type_info const& iToType,
00108                 std::vector<unsigned long> const& iIndicies,
00109                 std::vector<void const*>& oPtr) {
00110     detail::reallyfillPtrVector(obj, iToType, iIndicies, oPtr);
00111   }
00112 
00113   template <typename T, typename A, typename Comp>
00114   void
00115   fillPtrVector(std::set<T, A, Comp> const& obj,
00116                 std::type_info const& iToType,
00117                 std::vector<unsigned long> const& iIndicies,
00118                 std::vector<void const*>& oPtr) {
00119     detail::reallyfillPtrVector(obj, iToType, iIndicies, oPtr);
00120   }
00121 }
00122 
00123 #endif