LCOV - code coverage report
Current view: top level - atermpp/include/mcrl2/atermpp - function_symbol_generator.h (source / functions) Hit Total Coverage
Test: mcrl2_coverage.info.cleaned Lines: 18 18 100.0 %
Date: 2020-02-13 00:44:47 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Author(s): Wieger Wesselink, Jan Friso Groote
       2             : // Copyright: see the accompanying file COPYING or copy at
       3             : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING
       4             : //
       5             : // Distributed under the Boost Software License, Version 1.0.
       6             : // (See accompanying file LICENSE_1_0.txt or copy at
       7             : // http://www.boost.org/LICENSE_1_0.txt)
       8             : //
       9             : /// \file mcrl2/atermpp/function_symbol_generator.h
      10             : /// \brief add your file description here.
      11             : 
      12             : #ifndef MCRL2_ATERMPP_FUNCTION_SYMBOL_GENERATOR_H
      13             : #define MCRL2_ATERMPP_FUNCTION_SYMBOL_GENERATOR_H
      14             : 
      15             : #include "mcrl2/atermpp/detail/global_aterm_pool.h"
      16             : 
      17             : #include <cctype>
      18             : 
      19             : namespace atermpp {
      20             : 
      21             : /// \brief Generates unique function symbols with a given prefix.
      22             : class function_symbol_generator : private mcrl2::utilities::noncopyable
      23             : {
      24             : protected:
      25             :   const std::string m_prefix;
      26             : 
      27             :   /// \brief Cache the value that is set in the constructor
      28             :   std::size_t  m_initial_index;
      29             : 
      30             :   /// \brief A local string cache to prevent allocating new strings for every function symbol generated.
      31             :   std::string  m_string_buffer;
      32             : 
      33             :   /// \brief A reference to the index as present in the function symbol generator.
      34             :   std::shared_ptr<std::size_t> m_index;
      35             : 
      36             : public:
      37             :   /// \brief Constructor
      38             :   /// \param[in] prefix The prefix of the generated generated strings.
      39             :   /// \pre The prefix may not be empty, and it may not have trailing digits
      40        2624 :   function_symbol_generator(const std::string& prefix)
      41        2624 :     : m_prefix(prefix),
      42        2624 :       m_string_buffer(prefix)
      43             :   {
      44        2624 :     assert(!prefix.empty() && !(std::isdigit(*prefix.rbegin())));
      45             : 
      46             :     // Obtain a reference to the first index possible.
      47        2624 :     m_index = detail::g_term_pool().get_symbol_pool().register_prefix(prefix);
      48             : 
      49        2624 :     m_initial_index = *m_index;
      50        2624 :   }
      51             : 
      52             :   /// \brief Restores the index back to the value that was initially assigned in the constructor.
      53       45240 :   void clear()
      54             :   {
      55       45240 :     *m_index = m_initial_index;
      56       45240 :   }
      57             : 
      58        2624 :   ~function_symbol_generator()
      59        2624 :   {
      60        2624 :     detail::g_term_pool().get_symbol_pool().deregister(m_prefix);
      61        2624 :   }
      62             : 
      63             :   /// \brief Generates a unique function symbol with the given prefix followed by a number.
      64        4448 :   function_symbol operator()(std::size_t arity = 0)
      65             :   {
      66             :     // Put the number m_index after the prefix in the string buffer.
      67        4448 :     mcrl2::utilities::number2string(*m_index, m_string_buffer, m_prefix.size());
      68             : 
      69             :     // Increase the index.
      70        4448 :     ++(*m_index);
      71             : 
      72             :     // Generate a new function symbol with prefix + index.
      73        4448 :     return function_symbol(m_string_buffer, arity, false);
      74             :   }
      75             : };
      76             : 
      77             : } // namespace atermpp
      78             : 
      79             : #endif // MCRL2_ATERMPP_FUNCTION_SYMBOL_GENERATOR_H

Generated by: LCOV version 1.13