mCRL2
Loading...
Searching...
No Matches
aterm_io.h
Go to the documentation of this file.
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#include "mcrl2/utilities/type_traits.h"
16
17namespace atermpp
18{
19
20/// \brief A function that is applied to all terms. The resulting term should only use
21/// a subset of the original arguments (i.e. not introduce new terms).
22/// \details Typical usage is removing the index traits from function symbols that represent operators.
23using aterm_transformer = aterm(const aterm&);
24
25/// \brief The default transformer that maps each term to itself.
26inline aterm identity(const aterm& x) { return x; }
27
28/// \brief The general aterm stream interface, which enables the use of a transformer to
29/// change the written/read terms.
31{
32public:
33 virtual ~aterm_stream();
34
35 /// \brief Sets the given transformer to be applied to following writes.
36 void set_transformer(aterm_transformer transformer) { m_transformer = transformer; }
37
38 /// \returns The currently assigned transformer function.
39 aterm_transformer* get_transformer() const { return m_transformer; }
40
41protected:
42 aterm_transformer* m_transformer = identity;
43};
44
45/// \brief The interface for a class that writes aterm to a stream.
46/// Every written term is retrieved by the corresponding aterm_istream::get() call.
48{
49public:
50 virtual ~aterm_ostream();
51
52 /// \brief Write the given term to the stream.
53 virtual void put(const aterm& term) = 0;
54};
55
56/// \brief The interface for a class that reads aterm from a stream.
57/// The default constructed term aterm() indicates the end of the stream.
59{
60public:
61 virtual ~aterm_istream();
62
63 /// \brief Reads an aterm from this stream.
64 virtual void get(aterm& t) = 0;
65};
66
67// These free functions provide input/output operators for these streams.
68
69/// \brief Sets the given transformer to be applied to following reads.
70inline aterm_istream& operator>>(aterm_istream& stream, aterm_transformer transformer) { stream.set_transformer(transformer); return stream; }
71inline aterm_ostream& operator<<(aterm_ostream& stream, aterm_transformer transformer) { stream.set_transformer(transformer); return stream; }
72
73/// \brief Write the given term to the stream.
74inline aterm_ostream& operator<<(aterm_ostream& stream, const aterm& term) { stream.put(term); return stream; }
75
76/// \brief Read the given term from the stream, but for aterm_list we want to use a specific one that performs validation (defined below).
77inline aterm_istream& operator>>(aterm_istream& stream, aterm& term) { stream.get(term); return stream; }
78
79// Utility functions
80
81/// \brief A helper class to restore the state of the aterm_{i,o}stream objects upon destruction. Currently, onlt
82/// preserves the transformer object.
84{
85public:
87 : m_stream(stream)
88 {
90 }
91
93 {
95 }
96
97private:
99 aterm_transformer* m_transformer;
100};
101
102/// \brief Write any container (that is not an aterm itself) to the stream.
103template<typename T,
104 typename std::enable_if_t<mcrl2::utilities::is_iterable_v<T>, int> = 0,
105 typename std::enable_if_t<!std::is_base_of<aterm, T>::value, int> = 0>
106inline aterm_ostream& operator<<(aterm_ostream& stream, const T& container)
107{
108 // Write the number of elements, followed by each element in the container.
109 stream << aterm_int(std::distance(container.begin(), container.end()));
110
111 for (const auto& element : container)
112 {
113 stream << element;
114 }
115
116 return stream;
117}
118
119/// \brief Read any container (that is not an aterm itself) from the stream.
120template<typename T,
121 typename std::enable_if_t<mcrl2::utilities::is_iterable_v<T>, int> = 0,
122 typename std::enable_if_t<!std::is_base_of<aterm, T>::value, int> = 0>
123inline aterm_istream& operator>>(aterm_istream& stream, T& container)
124{
125 // Insert the next nof_elements into the container.
126 aterm_int nof_elements;
127 stream >> nof_elements;
128
129 auto it = std::inserter(container, container.end());
130 for (std::size_t i = 0; i < nof_elements.value(); ++i)
131 {
132 typename T::value_type element;
133 stream >> element;
134 it = element;
135 }
136
137 return stream;
138}
139
140template<typename T>
141inline aterm_ostream& operator<<(aterm_ostream&& stream, const T& t) { stream << t; return stream; }
142
143template<typename T>
144inline aterm_istream& operator>>(aterm_istream&& stream, T& t) { stream >> t; return stream; }
145
146/// \brief Sends the name of a function symbol to an ostream.
147/// \param out The out stream.
148/// \param f The function symbol to be output.
149/// \return The stream.
150inline
151std::ostream& operator<<(std::ostream& out, const function_symbol& f)
152{
153 return out << f.name();
154}
155
156/// \brief Prints the name of a function symbol as a string.
157/// \param f The function symbol.
158/// \return The string representation of r.
159inline const std::string& pp(const function_symbol& f)
160{
161 return f.name();
162}
163
164/// \brief Writes term t to a stream in binary aterm format.
165void write_term_to_binary_stream(const aterm& t, std::ostream& os);
166
167/// \brief Reads a term from a stream in binary aterm format.
168void read_term_from_binary_stream(std::istream& is, aterm& t);
169
170/// \brief Writes term t to a stream in textual format.
171void write_term_to_text_stream(const aterm& t, std::ostream& os);
172
173/// \brief Reads a term from a stream which contains the term in textual format.
174void read_term_from_text_stream(std::istream& is, aterm& t);
175
176/// \brief Reads an aterm from a string. The string can be in either binary or text format.
177aterm read_term_from_string(const std::string& s);
178
179/// \brief Reads an aterm_list from a string. The string can be in either binary or text format.
180/// \details If the input is not a string, an aterm is returned of the wrong type.
181/// \return The term corresponding to the string.
182inline aterm_list read_list_from_string(const std::string& s)
183{
184 const aterm_list l = down_cast<aterm_list>(read_term_from_string(s));
185 assert(l.type_is_list());
186 return l;
187}
188
189/// \brief Reads an aterm_int from a string. The string can be in either binary or text format.
190/// \details If the input is not an int, an aterm is returned of the wrong type.
191/// \return The aterm_int corresponding to the string.
192inline aterm_int read_int_from_string(const std::string& s)
193{
194 const aterm_int n = down_cast<aterm_int>(read_term_from_string(s));
195 assert(n.type_is_int());
196 return n;
197}
198
199/// \brief Reads an aterm from a string. The string can be in either binary or text format.
200/// \details If the input is not an aterm, an aterm is returned of the wrong type.
201/// \return The term corresponding to the string.
202inline aterm read_appl_from_string(const std::string& s)
203{
205 assert(a.type_is_appl());
206 return a;
207}
208
209
210} // namespace atermpp
211
212#endif // MCRL2_ATERMPP_ATERM_IO_H
An integer term stores a single std::size_t value. It carries no arguments.
Definition aterm_int.h:26
std::size_t value() const noexcept
Provide the value stored in an aterm.
Definition aterm_int.h:54
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:59
virtual void get(aterm &t)=0
Reads an aterm from this stream.
The interface for a class that writes aterm to a stream. Every written term is retrieved by the corre...
Definition aterm_io.h:48
virtual void put(const aterm &term)=0
Write the given term to the stream.
A helper class to restore the state of the aterm_{i,o}stream objects upon destruction....
Definition aterm_io.h:84
aterm_transformer * m_transformer
Definition aterm_io.h:99
aterm_stream_state(aterm_stream &stream)
Definition aterm_io.h:86
aterm_stream & m_stream
Definition aterm_io.h:98
The general aterm stream interface, which enables the use of a transformer to change the written/read...
Definition aterm_io.h:31
aterm_transformer * m_transformer
Definition aterm_io.h:42
aterm_transformer * get_transformer() const
Definition aterm_io.h:39
void set_transformer(aterm_transformer transformer)
Sets the given transformer to be applied to following writes.
Definition aterm_io.h:36
This is the class to which an aterm points.
Definition aterm_core.h:48
void add_deletion_hook(function_symbol sym, term_callback callback)
Add a callback that is triggered whenever a term with the given function symbol is destroyed.
thread_aterm_pool(aterm_pool &global_pool)
const std::string & name() const
Return the name of the function_symbol.
bool type_is_appl() const noexcept
Dynamic check whether the term is an aterm.
Definition aterm_core.h:55
bool type_is_int() const noexcept
Dynamic check whether the term is an aterm_int.
Definition aterm_core.h:63
std::aligned_storage< sizeof(aterm_pool), alignof(aterm_pool)>::type g_aterm_pool_storage
Storage for a global term pool that is not initialized.
static aterm_pool & g_aterm_pool_instance
A reference to the global term pool storage.
thread_aterm_pool & g_thread_term_pool()
A reference to the thread local term pool storage.
The main namespace for the aterm++ library.
Definition algorithm.h:21
aterm identity(const aterm &x)
The default transformer that maps each term to itself.
Definition aterm_io.h:26
void write_term_to_binary_stream(const aterm &t, std::ostream &os)
Writes term t to a stream in binary aterm format.
aterm_istream & operator>>(aterm_istream &stream, aterm &term)
Read the given term from the stream, but for aterm_list we want to use a specific one that performs v...
Definition aterm_io.h:77
term_list< aterm > aterm_list
A term_list with elements of type aterm.
Definition aterm_list.h:497
void(* term_callback)(const aterm &)
Definition aterm.h:194
void read_term_from_binary_stream(std::istream &is, aterm &t)
Reads a term from a stream in binary aterm format.
const std::string & pp(const function_symbol &f)
Prints the name of a function symbol as a string.
Definition aterm_io.h:159
aterm_istream & operator>>(aterm_istream &stream, T &container)
Read any container (that is not an aterm itself) from the stream.
Definition aterm_io.h:123
aterm read_appl_from_string(const std::string &s)
Reads an aterm from a string. The string can be in either binary or text format.
Definition aterm_io.h:202
aterm_int read_int_from_string(const std::string &s)
Reads an aterm_int from a string. The string can be in either binary or text format.
Definition aterm_io.h:192
aterm_istream & operator>>(aterm_istream &&stream, T &t)
Definition aterm_io.h:144
aterm read_term_from_string(const std::string &s)
Reads an aterm from a string. The string can be in either binary or text format.
void add_deletion_hook(const function_symbol &, term_callback)
Check for reasonably sized aterm (32 bits, 4 bytes) This check might break on perfectly valid archite...
void write_term_to_text_stream(const aterm &t, std::ostream &os)
Writes term t to a stream in textual format.
aterm_istream & operator>>(aterm_istream &stream, aterm_transformer transformer)
Sets the given transformer to be applied to following reads.
Definition aterm_io.h:70
void read_term_from_text_stream(std::istream &is, aterm &t)
Reads a term from a stream which contains the term in textual format.
aterm_list read_list_from_string(const std::string &s)
Reads an aterm_list from a string. The string can be in either binary or text format.
Definition aterm_io.h:182
static constexpr bool GlobalThreadSafe
Enables thread safety for the whole toolset.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
std::size_t operator()(const atermpp::detail::reference_aterm< T > &t) const