LCOV - code coverage report
Current view: top level - model - Map.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 100.0 % 36 36
Test Date: 2025-02-14 17:40:40 Functions: 92.7 % 55 51

            Line data    Source code
       1              : /*
       2              :  ___________________________________________
       3              : |    _     ___                        _     |
       4              : |   | |   |__ \                      | |    |
       5              : |   | |__    ) |__ _  __ _  ___ _ __ | |_   |
       6              : |   | '_ \  / // _` |/ _` |/ _ \ '_ \| __|  |  HTTP/2 AGENT FOR MOCK TESTING
       7              : |   | | | |/ /| (_| | (_| |  __/ | | | |_   |  Version 0.0.z
       8              : |   |_| |_|____\__,_|\__, |\___|_| |_|\__|  |  https://github.com/testillano/h2agent
       9              : |                     __/ |                 |
      10              : |                    |___/                  |
      11              : |___________________________________________|
      12              : 
      13              : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
      14              : SPDX-License-Identifier: MIT
      15              : Copyright (c) 2021 Eduardo Ramos
      16              : 
      17              : Permission is hereby  granted, free of charge, to any  person obtaining a copy
      18              : of this software and associated  documentation files (the "Software"), to deal
      19              : in the Software  without restriction, including without  limitation the rights
      20              : to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
      21              : copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
      22              : furnished to do so, subject to the following conditions:
      23              : 
      24              : The above copyright notice and this permission notice shall be included in all
      25              : copies or substantial portions of the Software.
      26              : 
      27              : THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
      28              : IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
      29              : FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
      30              : AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
      31              : LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      32              : OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
      33              : SOFTWARE.
      34              : */
      35              : 
      36              : #pragma once
      37              : 
      38              : // Better unordered_map than map:
      39              : // Slighly more memory consumption (not significative in load tests) due to the hash map.
      40              : // But order is not important for us, and the size is not very big (prune is normally
      41              : //  applied in load test provisions), so the cache is not used.
      42              : // As insertion and deletion are equally fast for both containers, we focus on search
      43              : //  (O(log2(n)) for map as binary tree, O(1) constant as average (O(n) in worst case)
      44              : //  for unordered map as hash table), so for our case, unordered_map seems to be the best choice.
      45              : #include <unordered_map>
      46              : 
      47              : #include <common.hpp>
      48              : 
      49              : 
      50              : namespace h2agent
      51              : {
      52              : namespace model
      53              : {
      54              : 
      55              : template<typename Key, typename Value>
      56              : class Map {
      57              : public:
      58              : 
      59              :     typedef typename std::unordered_map<Key, Value> map_t;
      60              :     typedef typename std::unordered_map<Key, Value>::const_iterator map_it;
      61              : 
      62         1418 :     Map() {};
      63              : 
      64              :     /** copy constructor */
      65              :     explicit Map(const map_t& m): map_(m) {};
      66              : 
      67         1337 :     ~Map() = default;
      68              : 
      69              :     // getters
      70              : 
      71              :     /** Returns a reference of the entire map */
      72          253 :     const map_t &get() const
      73              :     {
      74          253 :         read_guard_t guard(mutex_);
      75          253 :         return map_;
      76          253 :     }
      77              : 
      78              :     /** copy operator */
      79              :     Map& operator= (const map_t& other)
      80              :     {
      81              :         write_guard_t guard(mutex_);
      82              :         map_ = other;
      83              :         return *this;
      84              :     }
      85              : 
      86              :     /**
      87              :      * Searchs map key
      88              :      *
      89              :      * @param key key to find
      90              :      * @return no const iterator for provided key
      91              :      */
      92          709 :     map_it get(const Key& key) const
      93              :     {
      94              :         //read_guard_t guard(mutex_);
      95          709 :         return map_.find(key);
      96              :     }
      97              : 
      98              :     /** begin iterator */
      99           11 :     map_it begin() const
     100              :     {
     101           11 :         read_guard_t guard(mutex_);
     102           22 :         return map_.begin();
     103           11 :     }
     104              : 
     105              :     /** end iterator */
     106          734 :     map_it end() const
     107              :     {
     108          734 :         read_guard_t guard(mutex_);
     109         1468 :         return map_.end();
     110          734 :     }
     111              : 
     112              :     /** map size */
     113          256 :     size_t size() const
     114              :     {
     115          256 :         read_guard_t guard(mutex_);
     116          512 :         return map_.size();
     117          256 :     }
     118              : 
     119              :     // setters
     120              : 
     121              :     /**
     122              :      * Adds a new value to the map
     123              :      *
     124              :      * @param key key to add
     125              :      * @param value stored
     126              :      */
     127          784 :     void add(const Key& key, const Value &value)
     128              :     {
     129          784 :         write_guard_t guard(mutex_);
     130          784 :         map_[key] = value;
     131          784 :     }
     132              : 
     133              :     /**
     134              :      * Adds another map of same kind to the map
     135              :      *
     136              :      * @param map map to add
     137              :      */
     138            5 :     void add(const map_t& m)
     139              :     {
     140            5 :         write_guard_t guard(mutex_);
     141            5 :         map_.insert(m.begin(), m.end());
     142            5 :     }
     143              : 
     144              :     /**
     145              :      * Removes key
     146              :      *
     147              :      * @param it key iterator to remove
     148              :      */
     149            4 :     void remove(map_it it)
     150              :     {
     151            4 :         write_guard_t guard(mutex_);
     152            4 :         map_.erase(it);
     153            4 :     }
     154              : 
     155              :     /**
     156              :      * Removes key
     157              :      *
     158              :      * @param key key to remove
     159              :      */
     160            2 :     void remove(const Key& key)
     161              :     {
     162            2 :         write_guard_t guard(mutex_);
     163            2 :         map_.erase(key);
     164            2 :     }
     165              : 
     166              :     /** Clear map */
     167              :     void clear()
     168              :     {
     169              :         write_guard_t guard(mutex_);
     170              :         map_.clear();
     171              :     }
     172              : 
     173              : protected:
     174              :     mutable mutex_t mutex_{};
     175              :     map_t map_{};
     176              : };
     177              : 
     178              : }
     179              : }
     180              : 
        

Generated by: LCOV version 2.0-1