Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-08-22 04:57:28

0001 #ifndef DataFormats_Common_WrapperDetail_h
0002 #define DataFormats_Common_WrapperDetail_h
0003 
0004 /*----------------------------------------------------------------------
0005 
0006 WrapperDetail: Metafunction support for compile-time selection of code.
0007 
0008 ----------------------------------------------------------------------*/
0009 
0010 #include <memory>
0011 #include <typeinfo>
0012 #include <type_traits>
0013 #include <vector>
0014 
0015 namespace edm {
0016 
0017   //Need to specialize the case of std::vector<edm::Ptr<T>>
0018   template <typename T>
0019   class Ptr;
0020 
0021   namespace detail {
0022     using no_tag = std::false_type;  // type indicating FALSE
0023     using yes_tag = std::true_type;  // type indicating TRUE
0024 
0025     // valueTypeInfo_() will return typeid(T::value_type) if T::value_type is declared and typeid(void) otherwise.
0026     // Definitions for the following struct and function templates are not needed; we only require the declarations.
0027     template <typename T>
0028     static yes_tag has_value_type(typename T::value_type*);
0029     template <typename T>
0030     static no_tag has_value_type(...);
0031 
0032     template <typename T>
0033     struct has_typedef_value_type {
0034       static constexpr bool value = std::is_same<decltype(has_value_type<T>(nullptr)), yes_tag>::value;
0035     };
0036     template <typename T, bool = has_typedef_value_type<T>::value>
0037     struct getValueType;
0038     template <typename T>
0039     struct getValueType<T, true> {
0040       std::type_info const& operator()() { return typeid(typename T::value_type); }
0041     };
0042     template <typename T>
0043     struct getValueType<T, false> {
0044       std::type_info const& operator()() { return typeid(void); }
0045     };
0046 
0047     // memberTypeInfo_() will return typeid(T::member_type) if T::member_type is declared and typeid(void) otherwise.
0048     // Definitions for the following struct and function templates are not needed; we only require the declarations.
0049     template <typename T>
0050     static yes_tag has_member_type(typename T::member_type*);
0051     template <typename T>
0052     static no_tag has_member_type(...);
0053 
0054     template <typename T>
0055     struct has_typedef_member_type {
0056       static constexpr bool value = std::is_same<decltype(has_member_type<T>(nullptr)), yes_tag>::value;
0057     };
0058     template <typename T, bool = has_typedef_member_type<T>::value>
0059     struct getMemberType;
0060     template <typename T>
0061     struct getMemberType<T, true> {
0062       std::type_info const& operator()() { return typeid(typename T::member_type); }
0063     };
0064     template <typename T>
0065     struct getMemberType<T, false> {
0066       std::type_info const& operator()() { return typeid(void); }
0067     };
0068 
0069     template <typename T>
0070     struct has_typedef_member_type<std::vector<edm::Ptr<T> > > {
0071       static constexpr bool value = true;
0072     };
0073 
0074     template <typename T>
0075     struct getMemberType<std::vector<edm::Ptr<T> >, true> {
0076       std::type_info const& operator()() { return typeid(T); }
0077     };
0078 
0079     template <typename T, typename Deleter>
0080     struct has_typedef_member_type<std::vector<std::unique_ptr<T, Deleter> > > {
0081       static constexpr bool value = true;
0082     };
0083 
0084     template <typename T, typename Deleter>
0085     struct getMemberType<std::vector<std::unique_ptr<T, Deleter> >, true> {
0086       std::type_info const& operator()() { return typeid(T); }
0087     };
0088 
0089   }  // namespace detail
0090 }  // namespace edm
0091 #endif