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

          Line data    Source code
       1             : // Author(s): 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             : 
      10             : #ifndef MCRL2_ATERMPP_ATERM_IO_H
      11             : #define MCRL2_ATERMPP_ATERM_IO_H
      12             : 
      13             : #include "mcrl2/atermpp/aterm_list.h"
      14             : #include "mcrl2/atermpp/aterm_int.h"
      15             : 
      16             : namespace atermpp
      17             : {
      18             : 
      19             : /// \brief A function that is applied to all terms. The resulting term should only use
      20             : ///        a subset of the original arguments (i.e. not introduce new terms).
      21             : /// \details Typical usage is removing the index traits from function symbols that represent operators.
      22             : using aterm_transformer = aterm_appl(const aterm_appl&);
      23             : 
      24             : /// \brief The default transformer that maps each term to itself.
      25      423284 : inline aterm_appl identity(const aterm_appl& x) { return x; }
      26             : 
      27             : /// \brief The interface for a class that writes aterm to a stream.
      28             : ///        Every written term is retrieved by the corresponding aterm_istream::get() call.
      29       12640 : class aterm_ostream
      30             : {
      31             : public:
      32             :   virtual ~aterm_ostream();
      33             : 
      34             :   /// \brief Sets the given transformer to be applied to following writes.
      35         122 :   void set_transformer(aterm_transformer transformer) { m_transformer = transformer; }
      36             : 
      37             :   /// \returns The currently assigned transformer function.
      38             :   aterm_transformer* get_transformer() const { return m_transformer; }
      39             : 
      40             :   /// \brief Write the given term to the stream.
      41             :   virtual void put(const aterm& term) = 0;
      42             : 
      43             : protected:
      44             :   aterm_transformer* m_transformer = identity;
      45             : };
      46             : 
      47             : /// \brief The interface for a class that reads aterm from a stream.
      48             : ///        The default constructed term aterm() indicates the end of the stream.
      49       10235 : class aterm_istream
      50             : {
      51             : public:
      52             :   virtual ~aterm_istream();
      53             : 
      54             :   /// \brief Sets the given transformer to be applied to following reads.
      55         123 :   void set_transformer(aterm_transformer transformer) { m_transformer = transformer; }
      56             : 
      57             :   /// \returns The currently assigned transformer function.
      58             :   aterm_transformer* get_transformer() const { return m_transformer; }
      59             : 
      60             :   /// \brief Reads an object of type T from this stream, using the object specific >> operator.
      61             :   template<typename T>
      62             :   T get();
      63             : 
      64             :   /// \brief Reads a single term from this stream.
      65             :   virtual aterm get() = 0;
      66             : 
      67             : protected:
      68             :   aterm_transformer* m_transformer = identity;
      69             : };
      70             : 
      71             : // These free functions provide input/output operators for these streams.
      72             : 
      73             : /// \brief Sets the given transformer to be applied to following reads.
      74         122 : inline aterm_ostream& operator<<(aterm_ostream& stream, aterm_transformer transformer) { stream.set_transformer(transformer); return stream; }
      75             : 
      76             : /// \brief Write the given term to the stream.
      77       16033 : inline aterm_ostream& operator<<(aterm_ostream& stream, const aterm& term) { stream.put(term); return stream; }
      78             : 
      79             : /// \brief Sets the given transformer to be applied to following reads.
      80         123 : inline aterm_istream& operator>>(aterm_istream& stream, aterm_transformer transformer) { stream.set_transformer(transformer); return stream; }
      81             : 
      82             : /// \brief Reads a single term from this stream.
      83          11 : inline aterm_istream& operator>>(aterm_istream& stream, aterm& term) { term = stream.get(); return stream; }
      84             : 
      85             : template<typename T>
      86       12526 : inline aterm_ostream& operator<<(aterm_ostream&& stream, const T& t) { stream << t; return stream; }
      87             : 
      88             : template<typename T>
      89          11 : inline aterm_istream& operator>>(aterm_istream&& stream, T& t) { stream >> t; return stream; }
      90             : 
      91             : template<typename T>
      92             : inline T aterm_istream::get() { T t; *this >> t; return t; }
      93             : 
      94             : // These are utility functions.
      95             : 
      96             : /// \brief Send the term in textual form to the ostream.
      97             : std::ostream& operator<<(std::ostream& out, const aterm& t);
      98             : 
      99             : /// \param t The input aterm.
     100             : /// \return A string representation of the given term derived from an aterm.
     101       10029 : inline std::string pp(const aterm& t)
     102             : {
     103       20058 :   std::ostringstream oss;
     104       10029 :   oss << t;
     105       20058 :   return oss.str();
     106             : }
     107             : 
     108             : /// \brief Sends the name of a function symbol to an ostream.
     109             : /// \param out The out stream.
     110             : /// \param f The function symbol to be output.
     111             : /// \return The stream.
     112             : inline
     113           2 : std::ostream& operator<<(std::ostream& out, const function_symbol& f)
     114             : {
     115           2 :   return out << f.name();
     116             : }
     117             : 
     118             : /// \brief Prints the name of a function symbol as a string.
     119             : /// \param f The function symbol.
     120             : /// \return The string representation of r.
     121             : inline const std::string& pp(const function_symbol& f)
     122             : {
     123             :   return f.name();
     124             : }
     125             : 
     126             : /// \brief Writes term t to a stream in binary aterm format.
     127             : void write_term_to_binary_stream(const aterm& t, std::ostream& os);
     128             : 
     129             : /// \brief Reads a term from a stream in binary aterm format.
     130             : aterm read_term_from_binary_stream(std::istream& is);
     131             : 
     132             : /// \brief Writes term t to a stream in textual format.
     133             : void write_term_to_text_stream(const aterm& t, std::ostream& os);
     134             : 
     135             : /// \brief Reads a term from a stream which contains the term in textual format.
     136             : aterm read_term_from_text_stream(std::istream& is);
     137             : 
     138             : /// \brief Reads an aterm from a string. The string can be in either binary or text format.
     139             : aterm read_term_from_string(const std::string& s);
     140             : 
     141             : /// \brief Reads an aterm_list from a string. The string can be in either binary or text format.
     142             : /// \details If the input is not a string, an aterm is returned of the wrong type.
     143             : /// \return The term corresponding to the string.
     144          15 : inline aterm_list read_list_from_string(const std::string& s)
     145             : {
     146          15 :   const aterm_list l = down_cast<aterm_list>(read_term_from_string(s));
     147          15 :   assert(l.type_is_list());
     148          15 :   return l;
     149             : }
     150             : 
     151             : /// \brief Reads an aterm_int from a string. The string can be in either binary or text format.
     152             : /// \details If the input is not an int, an aterm is returned of the wrong type.
     153             : /// \return The aterm_int corresponding to the string.
     154           2 : inline aterm_int read_int_from_string(const std::string& s)
     155             : {
     156           2 :   const aterm_int n = down_cast<aterm_int>(read_term_from_string(s));
     157           2 :   assert(n.type_is_int());
     158           2 :   return n;
     159             : }
     160             : 
     161             : /// \brief Reads an aterm_appl from a string. The string can be in either binary or text format.
     162             : /// \details If the input is not an aterm_appl, an aterm is returned of the wrong type.
     163             : /// \return The term corresponding to the string.
     164          29 : inline aterm_appl read_appl_from_string(const std::string& s)
     165             : {
     166          29 :   const aterm_appl a = down_cast<aterm_appl>(read_term_from_string(s));
     167          29 :   assert(a.type_is_appl());
     168          29 :   return a;
     169             : }
     170             : 
     171             : 
     172             : } // namespace atermpp
     173             : 
     174             : #endif // MCRL2_ATERMPP_ATERM_IO_H

Generated by: LCOV version 1.13