LCOV - code coverage report
Current view: top level - libs/cache - single_pointer_cache.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 9 9 100.0 %
Date: 2018-12-05 17:11:35 Functions: 9 13 69.2 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_SINGLE_POINTER_CACHE_HPP
       7             : #define IROHA_SINGLE_POINTER_CACHE_HPP
       8             : 
       9             : #include <memory>
      10             : #include <mutex>
      11             : 
      12             : namespace iroha {
      13             :   namespace cache {
      14             : 
      15             :     /**
      16             :      * Thread-safely stores and returns shared pointer to an element of template
      17             :      * type
      18             :      */
      19             :     template <typename DataType>
      20             :     class SinglePointerCache {
      21             :      public:
      22             :       /**
      23             :        * Pointer to data type
      24             :        */
      25             :       using DataPointer = std::shared_ptr<std::decay_t<DataType>>;
      26             : 
      27             :       /**
      28             :        * Insert data to the cache
      29             :        * @param pointer to the data to be inserted
      30             :        */
      31             :       void insert(DataPointer data);
      32             : 
      33             :       /**
      34             :        * Get data from the cache
      35             :        * @return pointer to the stored data
      36             :        */
      37             :       DataPointer get() const;
      38             : 
      39             :       /**
      40             :        * Delete data inside the cache
      41             :        */
      42             :       void release();
      43             : 
      44             :      private:
      45             :       DataPointer stored_data_;
      46             : 
      47             :       mutable std::mutex mutex_;
      48             :     };
      49             : 
      50             :     template <typename DataType>
      51             :     void SinglePointerCache<DataType>::insert(
      52             :         SinglePointerCache::DataPointer data) {
      53         737 :       std::lock_guard<std::mutex> lock(mutex_);
      54             : 
      55         737 :       stored_data_ = std::move(data);
      56         737 :     }
      57             : 
      58             :     template <typename DataType>
      59             :     typename SinglePointerCache<DataType>::DataPointer
      60             :     SinglePointerCache<DataType>::get() const {
      61          18 :       std::lock_guard<std::mutex> lock(mutex_);
      62             : 
      63          18 :       return stored_data_;
      64          18 :     }
      65             : 
      66             :     template <typename DataType>
      67             :     void SinglePointerCache<DataType>::release() {
      68          15 :       std::lock_guard<std::mutex> lock(mutex_);
      69             : 
      70          15 :       stored_data_.reset();
      71          15 :     }
      72             : 
      73             :   }  // namespace cache
      74             : }  // namespace iroha
      75             : 
      76             : #endif  // IROHA_SINGLE_POINTER_CACHE_HPP

Generated by: LCOV version 1.13