Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:15

0001 #ifndef FWCore_SOA_RowView_h
0002 #define FWCore_SOA_RowView_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/SOA
0006 // Class  :     RowView
0007 //
0008 /**\class RowView RowView.h "RowView.h"
0009 
0010  Description: Provides access to all elements of a row in a edm::soa::Table
0011 
0012  Usage:
0013     Individual column entries of a row are accessed via the 'get' method
0014  by specifying the edm::soa::Column type for the column.
0015  \code
0016  RowView<Eta,Phi> rv{...};
0017  
0018  auto eta = rv.get<Eta>();
0019  \endcode
0020 
0021  The MutableRowView allows one to modify the values associated with the row.
0022  \code
0023  MutableRowView<Eta,Phi> rv = table.row(3);
0024  
0025  rv.get<Eta>() = 4;
0026  rv.set<Eta>(5).set<Phi>(6);
0027  \endcode
0028  
0029  If the necessary fillers (See ColumnFillers.h) have been defined, then
0030  one can directly copy values from an object into the row elements
0031  \code
0032  MutableRowView<Eta,Phi> rv = table.row(3);
0033  ...
0034  rv.copyValuesFrom( Angles{0.2,3.1415} );
0035  \endcode
0036  
0037 */
0038 //
0039 // Original Author:  Chris Jones
0040 //         Created:  Thu, 24 Aug 2017 16:49:17 GMT
0041 //
0042 
0043 // system include files
0044 #include <tuple>
0045 #include <array>
0046 
0047 // user include files
0048 #include "FWCore/SOA/interface/tablehelpers.h"
0049 #include "FWCore/SOA/interface/ColumnFillers.h"
0050 
0051 // forward declarations
0052 
0053 namespace edm {
0054   namespace soa {
0055 
0056     template <typename... Args>
0057     class RowView {
0058       using Layout = std::tuple<Args...>;
0059       std::array<void const*, sizeof...(Args)> m_values;
0060 
0061     public:
0062       explicit RowView(std::array<void const*, sizeof...(Args)> const& iValues) : m_values{iValues} {}
0063 
0064       template <typename U>
0065       typename U::type const& get() const {
0066         return *(static_cast<typename U::type const*>(columnAddress<U>()));
0067       }
0068 
0069       template <typename U>
0070       void const* columnAddress() const {
0071         return m_values[impl::GetIndex<0, U, Layout>::index];
0072       }
0073     };
0074 
0075     template <typename... Args>
0076     class MutableRowView {
0077       using Layout = std::tuple<Args...>;
0078       std::array<void*, sizeof...(Args)> m_values;
0079 
0080     public:
0081       explicit MutableRowView(std::array<void*, sizeof...(Args)>& iValues) : m_values{iValues} {}
0082 
0083       template <typename U>
0084       typename U::type& get() {
0085         return *(static_cast<typename U::type*>(columnAddress<U>()));
0086       }
0087       template <typename U>
0088       typename U::type const& get() const {
0089         return *(static_cast<typename U::type const*>(columnAddress<U>()));
0090       }
0091 
0092       template <typename U>
0093       MutableRowView<Args...>& set(typename U::type const& iValue) {
0094         get<U>() = iValue;
0095         return *this;
0096       }
0097 
0098       template <typename U>
0099       void* columnAddress() {
0100         return m_values[impl::GetIndex<0, U, Layout>::index];
0101       }
0102       template <typename U>
0103       void const* columnAddress() const {
0104         return m_values[impl::GetIndex<0, U, Layout>::index];
0105       }
0106 
0107       template <typename O>
0108       void copyValuesFrom(O const& iObj) {
0109         copyValueFromImpl<0>(iObj);
0110       }
0111       template <typename O, typename... CArgs>
0112       void copyValuesFrom(O const& iObj, ColumnFillers<CArgs...> iFiller) {
0113         copyValuesUsingFiller<0>(iFiller, iObj, m_values);
0114       }
0115 
0116     private:
0117       template <int I, typename O>
0118       void copyValueFromImpl(O const& iObj) {
0119         if constexpr (I < sizeof...(Args)) {
0120           using ColumnType = typename std::tuple_element<I, Layout>::type;
0121           using Type = typename ColumnType::type;
0122           auto ptr = static_cast<Type*>(m_values[I]);
0123           *ptr = value_for_column(iObj, static_cast<ColumnType*>(nullptr));
0124           copyValueFromImpl<I + 1>(iObj);
0125         }
0126       }
0127 
0128       template <int I, typename E, typename F>
0129       static void copyValuesUsingFiller(F& iFiller, E const& iItem, std::array<void*, sizeof...(Args)>& oValues) {
0130         if constexpr (I < sizeof...(Args)) {
0131           using Layout = std::tuple<Args...>;
0132           using ColumnType = typename std::tuple_element<I, Layout>::type;
0133           using Type = typename ColumnType::type;
0134           Type* pElement = static_cast<Type*>(oValues[I]);
0135           *pElement = iFiller.value(iItem, static_cast<ColumnType*>(nullptr));
0136           copyValuesUsingFiller<I + 1>(iFiller, iItem, oValues);
0137         }
0138       }
0139     };
0140 
0141   }  // namespace soa
0142 }  // namespace edm
0143 
0144 #endif