Walker test code coverage report
Current view: top level - Base - TaggedTuple.hpp (source / functions) Hit Total Coverage
Commit: test_coverage.info Lines: 33 33 100.0 %
Date: 2022-09-21 13:52:12 Functions: 105 226 46.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 40 96 41.7 %

           Branch data     Line data    Source code
       1                 :            : // *****************************************************************************
       2                 :            : /*!
       3                 :            :   \file      src/Base/TaggedTuple.hpp
       4                 :            :   \copyright 2012-2015 J. Bakosi,
       5                 :            :              2016-2018 Los Alamos National Security, LLC.,
       6                 :            :              2019-2021 Triad National Security, LLC.
       7                 :            :              All rights reserved. See the LICENSE file for details.
       8                 :            :   \brief     Tagged tuple allowing tag-based access
       9                 :            :   \details   Tagged tuple allowing tag-based access. This is very much like
      10                 :            :     [std::tuple](http://en.cppreference.com/w/cpp/utility/tuple), but instead of
      11                 :            :     having to index the elements by integers, it allows access by a tag, which
      12                 :            :     can be an empty struct with a unique name. Credit goes to
      13                 :            :     ecatmur_at_stackoverflow.com, for more details, see
      14                 :            :     http://stackoverflow.com/questions/13065166/c11-tagged-tuple. For tags, see
      15                 :            :     Control/Tags.h. Tagged tuples are extensively used for transferring data
      16                 :            :     from the parser to an internal data structure in a type-save manner, which
      17                 :            :     is a tagged tuple containing a hierarchy of various containers. As an
      18                 :            :     example on how tagged tuples are used for parsing an input file, see
      19                 :            :     Control/Walker/InputDeck/InputDeck.h. Another way to use a tagged tuple is a
      20                 :            :     compile-time associated container between tags and an arbitrary type. As an
      21                 :            :     example, see rngtest::TestU01Stack::runner.
      22                 :            : */
      23                 :            : // *****************************************************************************
      24                 :            : #ifndef TaggedTuple_h
      25                 :            : #define TaggedTuple_h
      26                 :            : 
      27                 :            : #include <type_traits>
      28                 :            : #include <tuple>
      29                 :            : 
      30                 :            : #include <brigand/adapted/tuple.hpp>
      31                 :            : 
      32                 :            : #include "NoWarning/any.hpp"
      33                 :            : #include "NoWarning/partition.hpp"
      34                 :            : #include "NoWarning/index_of.hpp"
      35                 :            : 
      36                 :            : #include "PUPUtil.hpp"
      37                 :            : #include "Exception.hpp"
      38                 :            : 
      39                 :            : namespace tk {
      40                 :            : 
      41                 :            : //! \brief Tagged tuple, allowing tag-based access
      42                 :            : //! \details "Tag" here is any type, but mostly an empty struct with a good name
      43                 :            : //!   for the data member
      44                 :            : //! \tparam List Type list as brigand::list
      45                 :            : //! \see https://stackoverflow.com/a/42988897
      46                 :            : //! \see https://gist.github.com/underdoeg/4c5c616c1ad4cbb718f787eefcab902d
      47                 :            : template< class List >
      48         [ +  - ]:         25 : class TaggedTuple{
      49                 :            : 
      50                 :            :   private:
      51                 :            :     //! Generate index for every 2nd type of a type list
      52                 :            :     template< typename T >
      53                 :            :     using is_odd = brigand::size_t< (brigand::index_of<List,T>::value%2) != 0 >;
      54                 :            : 
      55                 :            :     //! Partition a type list into two lists with the even and the odd types
      56                 :            :     using Pair = brigand::partition< List, brigand::bind<is_odd,brigand::_1> >;
      57                 :            : 
      58                 :            :     //! List of member types
      59                 :            :     using Data = typename Pair::first_type;
      60                 :            : 
      61                 :            :     //! Tuple of member types
      62                 :            :     using Tuple = brigand::as_tuple< Data >;
      63                 :            : 
      64                 :            :     //! False-overload for detecting if T is a tagged tuple
      65                 :            :     template< typename T, typename = std::void_t<> >
      66                 :            :     struct is_tagged_tuple_t : std::false_type {};
      67                 :            : 
      68                 :            :     //! True-overload for detecting if T is a tagged tuple
      69                 :            :     template< typename T >
      70                 :            :     struct is_tagged_tuple_t< T, std::void_t< typename T::i_am_tagged_tuple > >
      71                 :            :      : std::true_type {};
      72                 :            : 
      73                 :            :     //! Member data as a tuple
      74                 :            :     Tuple m_members;
      75                 :            : 
      76                 :            :   public:
      77                 :            :     //! List of key-value pairs
      78                 :            :     using PairList = List;
      79                 :            : 
      80                 :            :     //! List of keys
      81                 :            :     using Keys = typename Pair::second_type;
      82                 :            : 
      83                 :            :     //! Typedef defining self for identifying self
      84                 :            :     using i_am_tagged_tuple = void;
      85                 :            : 
      86                 :            :     //! Acces type in tuple behind tag
      87                 :            :     template< typename Tag >
      88                 :            :     using TupleElement =
      89                 :            :       std::tuple_element_t< brigand::index_of<Keys,Tag>::value, Tuple >;
      90                 :            : 
      91                 :            :     //! Query if the type behind Tag is a TaggedTuple
      92                 :            :     //! Usage: if constexpr( is_tagged_tuple<Tag>::value ) { ... }
      93                 :            :     template< typename Tag >
      94                 :            :     using is_tagged_tuple =
      95                 :            :       is_tagged_tuple_t< std::decay_t< TupleElement<Tag> > >;
      96                 :            : 
      97                 :            :     //! Default constructor
      98                 :            :     explicit TaggedTuple() = default;
      99                 :            :     //! Initializer constructor
     100                 :            :     explicit TaggedTuple( Tuple&& tuple ) : m_members( std::move(tuple) ) {}
     101                 :            : 
     102                 :            :     //! Const-ref access to member tuple
     103                 :            :     const Tuple& tuple() const { return m_members; }
     104                 :            : 
     105                 :            :     //! Const-reference data member accessor of field of tagged tuple at depth
     106                 :            :     template< typename Tag, typename... Tags >
     107                 :            :     const auto& get() const noexcept {
     108                 :            :       constexpr std::size_t idx = brigand::index_of< Keys, Tag >::value;
     109                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     110                 :            :         return std::get< idx >( m_members ).template get< Tags... >();
     111                 :            :       else
     112                 :            :         return std::get< idx >( m_members );
     113                 :            :     }
     114                 :            : 
     115                 :            :     //! Reference data member accessor of field of tagged tuple at depth
     116                 :            :     template< typename Tag, typename... Tags >
     117                 :            :     auto& get() noexcept {
     118                 :            :       constexpr std::size_t idx = brigand::index_of< Keys, Tag >::value;
     119                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     120                 :            :         return std::get< idx >( m_members ).template get< Tags... >();
     121                 :            :       else
     122                 :            :         return std::get< idx >( m_members );
     123                 :            :     }
     124                 :            : 
     125                 :            :     //! Convert and store value converting from string at depth
     126                 :            :     //! \param[in] value Value to convert and store
     127                 :            :     template< typename Tag, typename... Tags >
     128                 :         97 :     void store( const std::string& value ) noexcept {
     129                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     130                 :            :       {
     131                 :            :         using T = std::remove_reference_t< decltype( get<Tag,Tags...>() ) >;
     132 [ -  + ][ -  + ]:        535 :         get< Tag, Tags... >() = convert< T >( value );
         [ -  - ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  + ]
     133                 :            :       } else {
     134                 :            :         using T = std::remove_reference_t< decltype( get< Tag >() ) >;
     135         [ -  + ]:         20 :         get< Tag >() = convert< T >( value );
     136                 :            :       }
     137                 :         97 :     }
     138                 :            : 
     139                 :            :     //! Convert and push back value, converting from string, to vector
     140                 :            :     //! \param[in] value Value to convert and store
     141                 :            :     template< typename Tag, typename... Tags >
     142                 :        221 :     void store_back( const std::string& value ) noexcept {
     143                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     144                 :            :       {
     145                 :            :         using T = typename std::remove_reference_t<
     146                 :            :                     decltype( get<Tag,Tags...>() ) >::value_type;
     147                 :        219 :         get< Tag, Tags... >().push_back( convert< T >( value ) );
     148                 :            :       } else {
     149                 :            :         using T = typename std::remove_reference_t<
     150                 :            :                     decltype( get<Tag>() ) >::value_type;
     151                 :          2 :         get< Tag >().push_back( convert< T >( value ) );
     152                 :            :       }
     153                 :        221 :     }
     154                 :            : 
     155                 :            :     //! Convert bool and push back, converting from string, to vector of ints
     156                 :            :     //! \param[in] value Value to convert and store
     157                 :            :     template< typename Tag, typename... Tags >
     158                 :            :     void store_back_bool( const std::string& value ) noexcept {
     159                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     160                 :            :       {
     161                 :            :         get< Tag, Tags... >().push_back( convert_bool( value ) );
     162                 :            :       } else {
     163                 :            :         get< Tag >().push_back( convert_bool( value ) );
     164                 :            :       }
     165                 :            :     }
     166                 :            : 
     167                 :            :     //! \brief Convert and push back value, converting from string, to back of
     168                 :            :     //!   a nested vector
     169                 :            :     //! \param[in] value Value to convert and store
     170                 :            :     template< typename Tag, typename... Tags >
     171                 :        923 :     void store_back_back( const std::string& value ) noexcept {
     172                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     173                 :            :       {
     174                 :            :         using T = typename std::remove_reference_t<
     175                 :            :                     decltype( get<Tag,Tags...>() ) >::value_type::value_type;
     176                 :        921 :         get< Tag, Tags... >().back().push_back( convert< T >( value ) );
     177                 :            :       } else {
     178                 :            :         using T = typename std::remove_reference_t<
     179                 :            :                     decltype( get<Tag>() ) >::value_type::value_type;
     180                 :          2 :         get< Tag >().back().push_back( convert< T >( value ) );
     181                 :            :       }
     182                 :        923 :     }
     183                 :            : 
     184                 :            :     //! \brief Convert and push back value, converting from string, to back of
     185                 :            :     //!   a doubly nested vector
     186                 :            :     //! \param[in] value Value to convert and store
     187                 :            :     template< typename Tag, typename... Tags >
     188                 :        276 :     void store_back_back_back( const std::string& value ) noexcept {
     189                 :            :       if constexpr( is_tagged_tuple<Tag>::value and sizeof...(Tags) != 0 )
     190                 :            :       {
     191                 :            :         using T = typename std::remove_reference_t<
     192                 :            :           decltype( get<Tag,Tags...>() ) >::value_type::value_type::value_type;
     193                 :        274 :         get< Tag, Tags... >().back().back().push_back( convert< T >( value ) );
     194                 :            :       } else {
     195                 :            :         using T = typename std::remove_reference_t<
     196                 :            :           decltype( get<Tag>() ) >::value_type::value_type::value_type;
     197                 :          2 :         get< Tag >().back().back().push_back( convert< T >( value ) );
     198                 :            :       }
     199                 :        276 :     }
     200                 :            : 
     201                 :            :     //! Insert key-value pair, converting value from string, to map
     202                 :            :     template< typename Tag, typename... Tags, typename Key, typename Value >
     203                 :            :     void insert( const Key& key, const Value& value ) {
     204 [ +  - ][ +  - ]:          9 :       get< Tag, Tags... >()[ key ] = value;
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
     205                 :          9 :     }
     206                 :            : 
     207                 :            :     //! Insert key-value pair to map of nested TaggedTuple
     208                 :            :     template< typename FieldTag, typename FieldType,
     209                 :            :               typename Tag, typename... Tags, typename Key >
     210                 :            :     void insert_field( const Key& key, const FieldType& value ) {
     211 [ +  - ][ +  + ]:          2 :       get< Tag, Tags... >()[ key ].template get< FieldTag >() = value;
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ -  - ]
         [ -  - ][ -  - ]
         [ -  - ][ -  - ]
     212                 :            :     }
     213                 :            : 
     214                 :            :     //! \brief Insert key-value pair, converting value from string, to map of
     215                 :            :     //!   nested TaggedTuple
     216                 :            :     template< typename FieldTag, typename FieldType,
     217                 :            :               typename Tag, typename... Tags, typename Key >
     218                 :         30 :     void insert_field( const Key& key, const std::string& value ) {
     219                 :         29 :       get< Tag, Tags... >()[ key ].template get< FieldTag >() =
     220                 :         30 :         convert< FieldType >( value );
     221                 :         29 :     }
     222                 :            : 
     223                 :            :     //! Operator == between two TaggedTuple objects
     224                 :            :     //! \tparam L Type list as brigand::list for other TaggedTuple
     225                 :            :     //! \return True if the lhs and rhs equal
     226                 :            :     template< typename L >
     227                 :            :     bool operator== ( const TaggedTuple< L >& t ) const {
     228                 :            :       static_assert( std::is_same_v< L, List >, "Invoking operator== on "
     229                 :            :         "TaggedTuple objects with different typelists" );
     230                 :            :       static_assert( !brigand::any< List,
     231                 :            :         std::is_floating_point<brigand::_1> >::value, "Invoking operator== on "
     232                 :            :         "TaggedTuple objects containing a floating point type is unreliable" );
     233         [ +  - ]:          1 :       return m_members == t.tuple();
     234                 :            :     }
     235                 :            : 
     236                 :            :     //! Operator < between two TaggedTuple objects
     237                 :            :     //! \tparam L Type list as brigand::list for other TaggedTuple
     238                 :            :     //! \return True if lhs < rhs
     239                 :            :     template< typename L >
     240                 :            :     bool operator< ( const TaggedTuple< L >& t ) const {
     241                 :            :       static_assert( std::is_same_v< L, List >, "Invoking operator< on "
     242                 :            :         "TaggedTuple objects with different typelists" );
     243                 :            :       return m_members < t.tuple();
     244                 :            :     }
     245                 :            : 
     246                 :            :     //! Return number of tuple entries
     247                 :            :     static constexpr std::size_t size() { return std::tuple_size_v< Tuple >; }
     248                 :            : 
     249                 :            :     //! Pack/Unpack
     250                 :            :     /** @name Charm++ pack/unpack serializer member functions */
     251                 :            :     ///@{
     252                 :            :     //! \brief Pack/Unpack serialize member function
     253                 :            :     //! \param[in,out] p Charm++'s PUP::er serializer object reference
     254 [ -  - ][ +  - ]:      28631 :     void pup( PUP::er& p ) { p | m_members; }
     255                 :            :     //! \brief Pack/Unpack serialize operator|
     256                 :            :     //! \param[in,out] p Charm++'s PUP::er serializer object reference
     257                 :            :     //! \param[in,out] t TaggedTuple object reference
     258                 :          2 :     friend void operator|( PUP::er& p, TaggedTuple<List>& t ) { t.pup(p); }
     259                 :            :     //@}
     260                 :            : 
     261                 :            :     //! Convert/parse string to and return as type given by template argument
     262                 :            :     //! \param[in] str String to convert
     263                 :            :     //! \return A value of type given by the template argument
     264                 :            :     template< typename type >
     265                 :       7787 :     type convert( const std::string& str ) {
     266                 :       7885 :       std::stringstream ss( str );
     267                 :            :       type num;
     268         [ +  - ]:       3151 :       ss >> std::boolalpha >> num;
     269         [ +  + ]:       7787 :       if (ss.fail())
     270 [ +  - ][ +  - ]:          3 :         Throw( "Failed to convert '" + str +
         [ +  - ][ +  - ]
         [ +  - ][ -  + ]
         [ -  + ][ -  + ]
         [ -  + ][ -  - ]
         [ -  - ][ -  - ]
     271                 :            :                "' to typeid " + typeid(num).name() );
     272                 :       7786 :       return num;
     273                 :            :     }
     274                 :            : 
     275                 :            :     //! Convert/parse string to bool and return as int
     276                 :            :     //! \param[in] str String to convert
     277                 :            :     //! \return Bool parsed from str returned as an int
     278                 :            :     //! \details Parsing a bool from a string, e.g., "true" or "false" is
     279                 :            :     //!   special compared to the above convert template, because the type into
     280                 :            :     //!   which we parse to (from stringstream) must be bool, but its value must
     281                 :            :     //!   be returned as an int. Input data stored this way ensures that the
     282                 :            :     //!   data is stored as an int and not a bool, which could be problematic if
     283                 :            :     //!   stored in a std::vector. Using this function via store_back_bool is a
     284                 :            :     //!   better way to achieve type-safety of the user input and ensures data
     285                 :            :     //!   does not get corrupted during Charm++ serialization as distributed to
     286                 :            :     //!   multiple PEs.
     287                 :            :     int convert_bool( const std::string& str ) {
     288                 :            :       std::stringstream ss( str );
     289                 :            :       bool num;
     290                 :            :       ss >> std::boolalpha >> num;
     291                 :            :       if (ss.fail())
     292                 :            :         Throw( "Failed to convert '" + str +
     293                 :            :                "' to typeid " + typeid(num).name() );
     294                 :            :       return num;
     295                 :            :     }
     296                 :            : };
     297                 :            : 
     298                 :            : } // tk::
     299                 :            : 
     300                 :            : #endif // TaggedTuple_h

Generated by: LCOV version 1.14