LCOV - code coverage report
Current view: top level - Utils - EnumMap.h (source / functions) Coverage Total Hit
Test: OdbDesign Test Coverage Lines: 52.2 % 23 12
Test Date: 2026-11-21 03:51:50 Functions: 66.7 % 24 16
Legend: Lines: hit not hit

            Line data    Source code
       1              : #pragma once
       2              : 
       3              : #include <vector>
       4              : #include <string>
       5              : #include <exception>
       6              : #include "str_utils.h"
       7              : #include <algorithm>
       8              : #include <iterator>
       9              : 
      10              : namespace Utils
      11              : {
      12              :         template<typename E>
      13              :         class EnumMap
      14              :         {
      15              :         public:
      16              :                 EnumMap(const std::vector<std::string>& names, bool caseInsensitive = false);
      17              : 
      18              :                 const std::string& getValue(const E e) const;
      19              :                 E getValue(const std::string& name) const;
      20              : 
      21              :                 bool contains(const std::string& name) const;
      22              :                 bool contains(const E e) const;
      23              : 
      24              :                 //typedef std::exception Exception;
      25              : 
      26              :         private:
      27              :                 const std::vector<std::string> m_names;
      28              :                 const bool m_bCaseInsensitive;          
      29              :                 
      30              :         };
      31              : 
      32              :         template<typename E>
      33         2552 :         inline EnumMap<E>::EnumMap(const std::vector<std::string>& names, bool caseInsensitive)
      34         2552 :                 : m_names(names)
      35         2552 :                 , m_bCaseInsensitive(caseInsensitive)
      36         2552 :         {}
      37              : 
      38              :         template<typename E>
      39            0 :         inline const std::string& EnumMap<E>::getValue(const E e) const
      40              :         {
      41            0 :                 auto index = static_cast<size_t>(e);
      42            0 :                 if (index < 0 || index >= m_names.size())
      43              :                 {
      44            0 :                         std::string msg = "no name found for value: " + std::to_string(index);
      45            0 :                         throw std::invalid_argument(msg.c_str());
      46            0 :                 }
      47            0 :                 return m_names[index];
      48              :         }
      49              : 
      50              :         template<typename E>
      51         9058 :         inline E EnumMap<E>::getValue(const std::string& name) const
      52              :         {
      53         9058 :                 std::vector<std::string>::const_iterator findIt;
      54         9058 :                 if (m_bCaseInsensitive)
      55              :                 {
      56            0 :                         findIt = Utils::find_str_icmp(m_names.begin(), m_names.end(), name);
      57              :                 }
      58              :                 else
      59              :                 {
      60         9058 :                         findIt = std::find(m_names.begin(), m_names.end(), name);
      61              :                 }
      62              : 
      63         9058 :                 if (findIt == m_names.end())
      64              :                 {
      65            0 :                         std::string msg = "no value found for name: (" + name + ")";
      66            0 :                         throw std::invalid_argument(msg.c_str());
      67            0 :                 }
      68        27174 :                 return static_cast<E>(std::distance(m_names.begin(), findIt));
      69              :         }
      70              : 
      71              :         template<typename E>
      72         9058 :         inline bool EnumMap<E>::contains(const std::string& name) const
      73              :         {
      74         9058 :                 return std::find(m_names.begin(), m_names.end(), name) != m_names.end();
      75              :         }
      76              : 
      77              :         template<typename E>
      78              :         inline bool EnumMap<E>::contains(const E e) const
      79              :         {
      80              :                 auto index = static_cast<size_t>(e);
      81              :                 return index >= 0 && index < m_names.size();
      82              :         }
      83              : }
        

Generated by: LCOV version 2.0-1