LCOV - code coverage report
Current view: top level - shared_model/utils - string_builder.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 11 18 61.1 %
Date: 2018-12-05 17:11:35 Functions: 14 28 50.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_SHARED_MODEL_STRING_BUILDER_HPP
       7             : #define IROHA_SHARED_MODEL_STRING_BUILDER_HPP
       8             : 
       9             : #include <string>
      10             : 
      11             : namespace shared_model {
      12             :   namespace detail {
      13             :     /**
      14             :      * A simple string builder class for building pretty looking strings
      15             :      */
      16             :     class PrettyStringBuilder {
      17             :      public:
      18             :       /**
      19             :        * Initializes new string with a provided name
      20             :        * @param name - name to initialize
      21             :        */
      22             :       PrettyStringBuilder &init(const std::string &name);
      23             : 
      24             :       /**
      25             :        * Inserts new level marker
      26             :        */
      27             :       PrettyStringBuilder &insertLevel();
      28             : 
      29             :       /**
      30             :        * Closes new level marker
      31             :        */
      32             :       PrettyStringBuilder &removeLevel();
      33             : 
      34             :       /**
      35             :        * Appends new field to string as a "name=value" pair
      36             :        * @param name - field name to append
      37             :        * @param value - field value
      38             :        */
      39             :       PrettyStringBuilder &append(const std::string &name,
      40             :                                   const std::string &value);
      41             : 
      42             :       /**
      43             :        * Appends new single value to string
      44             :        * @param value - value to append
      45             :        */
      46             :       PrettyStringBuilder &append(const std::string &value);
      47             : 
      48             :       /**
      49             :        * Appends a new collection to string
      50             :        * @tparam Collection - type of collection
      51             :        * @tparam Transform - type of transformation function
      52             :        * @param c - collection to append
      53             :        * @param t - transformation function
      54             :        */
      55             :       template <typename Collection, typename Transform>
      56             :       PrettyStringBuilder &appendAll(Collection &&c, Transform &&t) {
      57        1794 :         insertLevel();
      58        5049 :         for (auto &val : c) {
      59        3252 :           append(t(val));
      60             :         }
      61        1797 :         removeLevel();
      62        1797 :         return *this;
      63           0 :       }
      64             : 
      65             :       /**
      66             :        * Appends a new named collection to string
      67             :        * @tparam Collection - type of collection
      68             :        * @tparam Transform - type of transformation function
      69             :        * @param name - field name to append
      70             :        * @param c - collection to append
      71             :        * @param t - transformation function
      72             :        */
      73             :       template <typename Collection, typename Transform>
      74             :       PrettyStringBuilder &appendAll(
      75             :           const std::string &name, Collection &&c, Transform &&t) {
      76           0 :         result_.append(name);
      77           0 :         result_.append(keyValueSeparator);
      78           0 :         appendAll(c, t);
      79           0 :         result_.append(singleFieldsSeparator);
      80           0 :         result_.append(spaceSeparator);
      81           0 :         return *this;
      82             :       }
      83             : 
      84             :       /**
      85             :        * Finalizes appending and returns constructed string.
      86             :        * @return resulted string
      87             :        */
      88             :       std::string finalize();
      89             : 
      90             :      private:
      91             :       std::string result_;
      92        9048 :       const std::string beginBlockMarker = "[";
      93        9048 :       const std::string endBlockMarker = "]";
      94        9046 :       const std::string keyValueSeparator = "=";
      95        9046 :       const std::string singleFieldsSeparator = ",";
      96        9047 :       const std::string initSeparator = ":";
      97        9048 :       const std::string spaceSeparator = " ";
      98             :     };
      99             :   }  // namespace detail
     100             : }  // namespace shared_model
     101             : 
     102             : #endif  // IROHA_SHARED_MODEL_STRING_BUILDER_HPP

Generated by: LCOV version 1.13