LCOV - code coverage report
Current view: top level - shared_model/utils - reference_holder.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 7 7 100.0 %
Date: 2019-03-07 14:46:43 Functions: 189 256 73.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             : 
      11             : namespace shared_model {
      12             :   namespace detail {
      13             :     /**
      14             :      * Container designed to store reference or value depending on called ctor
      15             :      * @tparam T type of stored value
      16             :      */
      17             :     template <typename T>
      18             :     class ReferenceHolder {
      19             :      private:
      20             :       using VariantType = boost::variant<T, T &>;
      21             : 
      22             :      public:
      23             :       template <typename V>
      24             :       explicit ReferenceHolder(V &&value) : variant_(std::forward<V>(value)) {}
      25             : 
      26             :       using ReferenceType = typename std::add_lvalue_reference_t<T>;
      27             :       using PointerType = typename std::add_pointer_t<T>;
      28             : 
      29             :       using ConstReferenceType = typename std::add_lvalue_reference_t<const T>;
      30             :       using ConstPointerType = typename std::add_pointer_t<const T>;
      31             : 
      32             :       ReferenceType operator*() {
      33       31307 :         return *ptr();
      34             :       }
      35             : 
      36             :       PointerType operator->() {
      37      302484 :         return ptr();
      38             :       }
      39             : 
      40             :       PointerType ptr() {
      41      303293 :         return &boost::apply_visitor(
      42             :             [](auto &value) -> decltype(auto) { return (value); }, variant_);
      43             :       }
      44             : 
      45             :       ConstReferenceType operator*() const {
      46         726 :         return *ptr();
      47             :       }
      48             : 
      49             :       ConstPointerType operator->() const {
      50       57207 :         return ptr();
      51             :       }
      52             : 
      53             :       ConstPointerType ptr() const {
      54       57916 :         return &boost::apply_visitor(
      55             :             [](const auto &value) -> decltype(auto) { return (value); },
      56       57916 :             variant_);
      57             :       }
      58             : 
      59             :      private:
      60             :       VariantType variant_;
      61             :     };
      62             :   }  // namespace detail
      63             : }  // namespace shared_model
      64             : 
      65             : #endif  // IROHA_REFERENCE_HOLDER_HPP

Generated by: LCOV version 1.13