LCOV - code coverage report
Current view: top level - shared_model/utils - reference_holder.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 7 7 100.0 %
Date: 2018-12-05 17:11:35 Functions: 217 262 82.8 %

          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_REFERENCE_HOLDER_HPP
       7             : #define IROHA_REFERENCE_HOLDER_HPP
       8             : 
       9             : #include <boost/variant.hpp>
      10             : #include "utils/lazy_initializer.hpp"
      11             : 
      12             : namespace shared_model {
      13             :   namespace detail {
      14             :     /**
      15             :      * Container designed to store reference or value depending on called ctor
      16             :      * @tparam T type of stored value
      17             :      */
      18             :     template <typename T>
      19             :     class ReferenceHolder {
      20             :      private:
      21             :       using VariantType = boost::variant<T, T &>;
      22             : 
      23             :      public:
      24             :       template <typename V>
      25             :       explicit ReferenceHolder(V &&value) : variant_(std::forward<V>(value)) {}
      26             : 
      27             :       using ReferenceType = typename std::add_lvalue_reference_t<T>;
      28             :       using PointerType = typename std::add_pointer_t<T>;
      29             : 
      30             :       using ConstReferenceType = typename std::add_lvalue_reference_t<const T>;
      31             :       using ConstPointerType = typename std::add_pointer_t<const T>;
      32             : 
      33             :       ReferenceType operator*() {
      34       92578 :         return *ptr();
      35             :       }
      36             : 
      37             :       PointerType operator->() {
      38      315799 :         return ptr();
      39             :       }
      40             : 
      41             :       PointerType ptr() {
      42      408387 :         return &boost::apply_visitor(
      43             :             [](auto &value) -> decltype(auto) { return (value); }, variant_);
      44             :       }
      45             : 
      46             :       ConstReferenceType operator*() const {
      47         976 :         return *ptr();
      48             :       }
      49             : 
      50             :       ConstPointerType operator->() const {
      51       12762 :         return ptr();
      52             :       }
      53             : 
      54             :       ConstPointerType ptr() const {
      55       13686 :         return &boost::apply_visitor(
      56             :             [](const auto &value) -> decltype(auto) { return (value); },
      57       13686 :             variant_);
      58             :       }
      59             : 
      60             :      private:
      61             :       VariantType variant_;
      62             :     };
      63             :   }  // namespace detail
      64             : }  // namespace shared_model
      65             : 
      66             : #endif  // IROHA_REFERENCE_HOLDER_HPP

Generated by: LCOV version 1.13