Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:49:27

0001 #ifndef DataFormats_Common_setPtr_h
0002 #define DataFormats_Common_setPtr_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Common
0006 // Class  :     setPtr
0007 //
0008 /**\class setPtr setPtr.h DataFormats/Common/interface/setPtr.h
0009 
0010  Description: Helper function used to implement the edm::Ptr class
0011 
0012  Usage:
0013     This is an internal detail of edm::Ptr interaction with edm::Wrapper and should not be used by others
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Sat Oct 20 11:45:38 CEST 2007
0019 //
0020 
0021 // user include files
0022 #include "DataFormats/Common/interface/FillView.h"
0023 #include "DataFormats/Common/interface/fwd_setPtr.h"
0024 #include "FWCore/Utilities/interface/EDMException.h"
0025 #include "FWCore/Utilities/interface/OffsetToBase.h"
0026 
0027 // system include files
0028 #include <typeinfo>
0029 #include <vector>
0030 
0031 // forward declarations
0032 namespace edm {
0033   namespace detail {
0034 
0035     template <typename COLLECTION>
0036     void reallySetPtr(COLLECTION const& coll, std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) {
0037       typedef COLLECTION product_type;
0038       typedef typename GetProduct<product_type>::element_type element_type;
0039       typedef typename product_type::const_iterator iter;
0040 
0041       if (iToType == typeid(element_type)) {
0042         iter it = coll.begin();
0043         std::advance(it, iIndex);
0044         element_type const* address = GetProduct<product_type>::address(it);
0045         oPtr = address;
0046       } else {
0047         iter it = coll.begin();
0048         std::advance(it, iIndex);
0049         element_type const* address = GetProduct<product_type>::address(it);
0050 
0051         oPtr = pointerToBase(iToType, address);
0052 
0053         if (nullptr == oPtr) {
0054           Exception::throwThis(errors::LogicError,
0055                                "TypeConversionError"
0056                                "edm::Ptr<> : unable to convert type ",
0057                                typeid(element_type).name(),
0058                                " to ",
0059                                iToType.name(),
0060                                "\n");
0061         }
0062       }
0063     }
0064   }  // namespace detail
0065 
0066   template <typename T, typename A>
0067   void setPtr(std::vector<T, A> const& obj, std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) {
0068     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
0069   }
0070 
0071   template <typename T, typename A>
0072   void setPtr(std::list<T, A> const& obj, std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) {
0073     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
0074   }
0075 
0076   template <typename T, typename A>
0077   void setPtr(std::deque<T, A> const& obj, std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) {
0078     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
0079   }
0080 
0081   template <typename T, typename A, typename Comp>
0082   void setPtr(std::set<T, A, Comp> const& obj, std::type_info const& iToType, unsigned long iIndex, void const*& oPtr) {
0083     detail::reallySetPtr(obj, iToType, iIndex, oPtr);
0084   }
0085 
0086 }  // namespace edm
0087 
0088 #endif