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

          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_COMMON_OBJ_UTILS_HPP
       7             : #define IROHA_COMMON_OBJ_UTILS_HPP
       8             : 
       9             : #include <boost/optional.hpp>
      10             : 
      11             : namespace iroha {
      12             : 
      13             :   /**
      14             :    * Create map get function for value retrieval by key
      15             :    * @tparam K - map key type
      16             :    * @tparam V - map value type
      17             :    * @param map - map for value retrieval
      18             :    * @return function which takes key, returns value if key exists,
      19             :    * nullopt otherwise
      20             :    */
      21             :   template <typename C>
      22             :   auto makeOptionalGet(C map) {
      23             :     return [&map](auto key) -> boost::optional<typename C::mapped_type> {
      24          49 :       auto it = map.find(key);
      25          49 :       if (it != std::end(map)) {
      26          48 :         return it->second;
      27             :       }
      28           1 :       return boost::none;
      29          49 :     };
      30             :   }
      31             : 
      32             :   /**
      33             :    * Return function which invokes class method by pointer to member with
      34             :    * provided arguments
      35             :    *
      36             :    * class A {
      37             :    * int f(int, double);
      38             :    * }
      39             :    *
      40             :    * A a;
      41             :    * int i = makeMethodInvoke(a, 1, 1.0);
      42             :    *
      43             :    * @tparam T - provided class type
      44             :    * @tparam Args - provided arguments types
      45             :    * @param object - class object
      46             :    * @param args - function arguments
      47             :    * @return described function
      48             :    */
      49             :   template <typename T, typename... Args>
      50             :   auto makeMethodInvoke(T &object, Args &&... args) {
      51             :     return [&](auto f) { return (object.*f)(std::forward<Args>(args)...); };
      52             :   }
      53             : 
      54             :   /**
      55             :    * Assign the value to the object member
      56             :    * @tparam V - object member type
      57             :    * @tparam B - object type
      58             :    * @param object - object value for member assignment
      59             :    * @param member - pointer to member in block
      60             :    * @return object with deserialized member on success, nullopt otherwise
      61             :    */
      62             :   template <typename V, typename B>
      63             :   auto assignObjectField(B object, V B::*member) {
      64             :     return [=](auto value) mutable {
      65          24 :       object.*member = value;
      66          21 :       return boost::make_optional(object);
      67             :     };
      68             :   }
      69             : 
      70             :   /**
      71             :    * Assign the value to the object member. Block is wrapped in monad
      72             :    * @tparam P - monadic type
      73             :    * @tparam V - object member type
      74             :    * @tparam B - object type
      75             :    * @param object - object value for member assignment
      76             :    * @param member - pointer to member in object
      77             :    * @return object with deserialized member on success, nullopt otherwise
      78             :    */
      79             :   template <template <typename C> class P, typename V, typename B>
      80             :   auto assignObjectField(P<B> object, V B::*member) {
      81             :     return [=](auto value) mutable {
      82          49 :       (*object).*member = value;
      83          49 :       return boost::make_optional(object);
      84             :     };
      85             :   }
      86             : }  // namespace iroha
      87             : 
      88             : #endif  // IROHA_COMMON_OBJ_UTILS_HPP

Generated by: LCOV version 1.13