LCOV - code coverage report
Current view: top level - include/sl3 - container.hpp (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 100.0 % 34 34
Test Date: 2026-09-06 14:01:09 Functions: 91.8 % 49 45
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /******************************************************************************
       2                 :             :  ------------- Copyright (c) 2009-2023 H a r a l d  A c h i t z ---------------
       3                 :             :  ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------
       4                 :             :  ---- This Source Code Form is subject to the terms of the Mozilla Public -----
       5                 :             :  ---- License, v. 2.0. If a copy of the MPL was not distributed with this -----
       6                 :             :  ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------
       7                 :             :  ******************************************************************************/
       8                 :             : 
       9                 :             : #ifndef SL3_CONTAINER_HPP_
      10                 :             : #define SL3_CONTAINER_HPP_
      11                 :             : 
      12                 :             : #include <cstddef>
      13                 :             : #include <initializer_list>
      14                 :             : #include <iterator>
      15                 :             : #include <string>
      16                 :             : #include <type_traits>
      17                 :             : #include <utility>
      18                 :             : 
      19                 :             : #include <sl3/error.hpp>
      20                 :             : 
      21                 :             : namespace sl3
      22                 :             : {
      23                 :             :   /**
      24                 :             :    * \brief Wrapper to provide begin, end and random access of a container
      25                 :             :    *
      26                 :             :    * \tparam ContainerType a container like for example std::vector
      27                 :             :    *
      28                 :             :    * A ContainerType has begin, end, random access and size.
      29                 :             :    *
      30                 :             :    * This class makes these methods accessible and hides the others, like a
      31                 :             :    * push_back, erase, ...
      32                 :             :    * A derived class can decide which other methods shall become visible.
      33                 :             :    *
      34                 :             :    */
      35                 :             :   template <typename ContainerType> class Container
      36                 :             :   {
      37                 :             :   public:
      38                 :             :     //@{
      39                 :             :     using container_type  = ContainerType;
      40                 :             :     using iterator        = typename container_type::iterator;
      41                 :             :     using const_iterator  = typename container_type::const_iterator;
      42                 :             :     using value_type      = typename container_type::value_type;
      43                 :             :     using reference       = typename container_type::reference;
      44                 :             :     using const_reference = typename container_type::const_reference;
      45                 :             :     using size_type       = typename container_type::size_type;
      46                 :             :     //@}
      47                 :             : 
      48                 :             :     /**
      49                 :             :      * \brief Constructor.
      50                 :             :      *
      51                 :             :      * Create an empty container.
      52                 :             :      *
      53                 :             :      */
      54                 :         251 :     Container () noexcept {}
      55                 :             :     /**
      56                 :             :      * \brief Constructor
      57                 :             :      *
      58                 :             :      * \param container values
      59                 :             :      */
      60                 :         109 :     Container (ContainerType container)
      61                 :         109 :     : _cont (std::move (container))
      62                 :             :     {
      63                 :         109 :     }
      64                 :             : 
      65                 :             :     /**
      66                 :             :      * \brief Constructor
      67                 :             :      *
      68                 :             :      * \param container values
      69                 :             :      */
      70                 :          83 :     Container (std::initializer_list<value_type> container)
      71                 :         166 :     : _cont (std::move (container))
      72                 :             :     {
      73                 :          83 :     }
      74                 :             : 
      75                 :             :     /**
      76                 :             :      * \brief Copy Constructor
      77                 :             :      */
      78                 :          60 :     Container (const Container&) = default;
      79                 :             : 
      80                 :             :     /**
      81                 :             :      * \brief Assignment
      82                 :             :      * \return reference to this
      83                 :             :      */
      84                 :           1 :     Container& operator= (const Container&) = default;
      85                 :             : 
      86                 :             :     /**
      87                 :             :      * \brief Move constructor
      88                 :             :      */
      89                 :         150 :     Container (Container&&) noexcept (
      90                 :             :         std::is_nothrow_move_constructible<ContainerType>::value) = default;
      91                 :             : 
      92                 :             :     /**
      93                 :             :      * \brief Move assignment
      94                 :             :      * \return reference to this
      95                 :             :      */
      96                 :           4 :     Container& operator= (Container&&) = default;
      97                 :             : 
      98                 :             :     /**
      99                 :             :      * \brief Destructor
     100                 :             :      */
     101                 :         653 :     virtual ~Container () noexcept (
     102                 :             :         std::is_nothrow_destructible<ContainerType>::value) = default;
     103                 :             : 
     104                 :             :     /**
     105                 :             :      * \brief Iterator access
     106                 :             :      * \return requested iterator
     107                 :             :      */
     108                 :             :     iterator
     109                 :         127 :     begin ()
     110                 :             :     {
     111                 :         254 :       return std::begin (_cont);
     112                 :             :     }
     113                 :             : 
     114                 :             :     /**
     115                 :             :      * \brief Iterator access
     116                 :             :      * \return requested iterator
     117                 :             :      */
     118                 :             :     const_iterator
     119                 :          19 :     begin () const
     120                 :             :     {
     121                 :          19 :       return _cont.cbegin ();
     122                 :             :     }
     123                 :             : 
     124                 :             :     /**
     125                 :             :      * \brief Iterator access
     126                 :             :      * \return requested iterator
     127                 :             :      */
     128                 :             :     iterator
     129                 :         127 :     end ()
     130                 :             :     {
     131                 :         254 :       return std::end (_cont);
     132                 :             :     }
     133                 :             : 
     134                 :             :     /**
     135                 :             :      * \brief Iterator access
     136                 :             :      * \return requested iterator
     137                 :             :      */
     138                 :             :     const_iterator
     139                 :          19 :     end () const
     140                 :             :     {
     141                 :          19 :       return _cont.cend ();
     142                 :             :     }
     143                 :             : 
     144                 :             :     /**
     145                 :             :      * \brief Iterator access
     146                 :             :      * \return requested iterator
     147                 :             :      */
     148                 :             :     const_iterator
     149                 :             :     cbegin () const
     150                 :             :     {
     151                 :             :       return _cont.cbegin ();
     152                 :             :     }
     153                 :             : 
     154                 :             :     /**
     155                 :             :      * \brief Iterator access
     156                 :             :      * \return requested iterator
     157                 :             :      */
     158                 :             :     const_iterator
     159                 :             :     cend () const
     160                 :             :     {
     161                 :             :       return _cont.cend ();
     162                 :             :     };
     163                 :             : 
     164                 :             :     ///
     165                 :             : 
     166                 :             :     /**
     167                 :             :      * \brief Container size
     168                 :             :      * \return number of elements
     169                 :             :      */
     170                 :             :     size_type
     171                 :         473 :     size () const
     172                 :             :     {
     173                 :         473 :       return _cont.size ();
     174                 :             :     }
     175                 :             : 
     176                 :             :     /**\brief checked random access
     177                 :             :      * \param i index
     178                 :             :      * \throw sl3::ErrOutOfRange if index is invalid
     179                 :             :      * \return reference to element at requested index
     180                 :             :      */
     181                 :             :     reference
     182                 :          44 :     at (size_t i)
     183                 :             :     {
     184                 :             :       try
     185                 :             :         {
     186                 :          44 :           return _cont.at (i);
     187                 :             :         }
     188                 :           2 :       catch (...)
     189                 :             :         {
     190                 :           1 :           throw ErrOutOfRange ("no data at: " + std::to_string (i));
     191                 :             :         }
     192                 :             :     }
     193                 :             : 
     194                 :             :     /** \brief checked random access
     195                 :             :      *
     196                 :             :      * \param i index
     197                 :             :      * \throw sl3::ErrOutOfRange if index is invalid
     198                 :             :      * \return const reference to element at requested index
     199                 :             :      */
     200                 :             :     const_reference
     201                 :          46 :     at (size_t i) const
     202                 :             :     {
     203                 :             :       try
     204                 :             :         {
     205                 :          46 :           return _cont.at (i);
     206                 :             :         }
     207                 :           2 :       catch (...)
     208                 :             :         {
     209                 :           1 :           throw ErrOutOfRange ("no data at: " + std::to_string (i));
     210                 :             :         }
     211                 :             :     }
     212                 :             : 
     213                 :             :     /**\brief unchecked random access
     214                 :             :      * \param i index
     215                 :             :      * behavior is undefined if the given index is invalid
     216                 :             :      * \return
     217                 :             :      * reference to element at requested index
     218                 :             :      */
     219                 :             :     reference
     220                 :         263 :     operator[] (size_t i)
     221                 :             :     {
     222                 :         263 :       return _cont[i];
     223                 :             :     }
     224                 :             :     /**\brief unchecked random access
     225                 :             :      * \param i index
     226                 :             :      * behavior is undefined if the given index is invalid
     227                 :             :      * \return
     228                 :             :      * reference to element at requested index
     229                 :             :      */
     230                 :             :     const_reference
     231                 :         236 :     operator[] (size_t i) const
     232                 :             :     {
     233                 :         236 :       return _cont[i];
     234                 :             :     }
     235                 :             : 
     236                 :             :   protected:
     237                 :             :     /// Container  T
     238                 :             :     ContainerType _cont;
     239                 :             :   };
     240                 :             : }
     241                 :             : 
     242                 :             : #endif /* SL3_CONTAINER_HPP_ */
        

Generated by: LCOV version 2.0-1