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_int.h"
14#include "mcrl2/atermpp/aterm_list.h"
15#include "mcrl2/atermpp/concepts.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)
27{
28 return x;
29}
30
31/// \brief The general aterm stream interface, which enables the use of a transformer to
32/// change the written/read terms.
34{
35public:
36 virtual ~aterm_stream();
37
38 /// \brief Sets the given transformer to be applied to following writes.
39 void set_transformer(aterm_transformer transformer) { m_transformer = transformer; }
40
41 /// \returns The currently assigned transformer function.
42 aterm_transformer* get_transformer() const { return m_transformer; }
43
44protected:
45 aterm_transformer* m_transformer = identity;
46};
47
48/// \brief The interface for a class that writes aterm to a stream.
49/// Every written term is retrieved by the corresponding aterm_istream::get() call.
51{
52public:
53 ~aterm_ostream() override;
54
55 /// \brief Write the given term to the stream.
56 virtual void put(const aterm& term) = 0;
57};
58
59/// \brief The interface for a class that reads aterm from a stream.
60/// The default constructed term aterm() indicates the end of the stream.
62{
63public:
64 ~aterm_istream() override;
65
66 /// \brief Reads an aterm from this stream.
67 virtual void get(aterm& t) = 0;
68};
69
70// These free functions provide input/output operators for these streams.
71
72/// \brief Sets the given transformer to be applied to following reads.
73inline aterm_istream& operator>>(aterm_istream& stream, aterm_transformer transformer)
74{
75 stream.set_transformer(transformer);
76 return stream;
77}
78inline aterm_ostream& operator<<(aterm_ostream& stream, aterm_transformer transformer)
79{
80 stream.set_transformer(transformer);
81 return stream;
82}
83
84/// \brief Write the given term to the stream.
85inline aterm_ostream& operator<<(aterm_ostream& stream, const aterm& term)
86{
87 stream.put(term);
88 return stream;
89}
90
91/// \brief Read the given term from the stream, but for aterm_list we want to use a specific one that performs
92/// validation (defined below).
93inline aterm_istream& operator>>(aterm_istream& stream, aterm& term)
94{
95 stream.get(term);
96 return stream;
97}
98
99// Utility functions
100
101/// \brief A helper class to restore the state of the aterm_{i,o}stream objects upon destruction. Currently, only
102/// preserves the transformer object.
104{
105public:
107 : m_stream(stream)
108 {
110 }
111
112 // This class is a scope guard: copying or moving it would restore the stream
113 // state more than once, corrupting the transformer of the referenced stream.
118
120
121private:
123 aterm_transformer* m_transformer;
124};
125
126/// \brief Write any container (that is not an aterm itself) to the stream.
127template <typename T>
128 requires std::ranges::range<T> && (!IsATerm<T>)
130{
131 // Write the number of elements, followed by each element in the container.
133
134 for (const auto& element : container)
135 {
136 stream << element;
137 }
138
139 return stream;
140}
141
142/// \brief Read any container (that is not an aterm itself) from the stream.
143template <typename T>
144 requires std::ranges::range<T> && (!IsATerm<T>)
146{
147 // Insert the next nof_elements into the container.
150
151 auto it = std::inserter(container, container.end());
152 for (std::size_t i = 0; i < nof_elements.value(); ++i)
153 {
154 typename T::value_type element;
155 stream >> element;
156 it = element;
157 }
158
159 return stream;
160}
161
162// r-value forwarding to the streaming operators, used for convenience.
163template<typename T>
165{
166 static_cast<aterm_ostream&>(stream) << t;
167 return std::move(stream);
168}
169
170template<typename T>
172{
173 static_cast<aterm_istream&>(stream) >> t;
174 return std::move(stream);
175}
176
177/// \brief Sends the name of a function symbol to an ostream.
178/// \param out The out stream.
179/// \param f The function symbol to be output.
180/// \return The stream.
182{
183 return out << f.name();
184}
185
186/// \brief Prints the name of a function symbol as a string.
187/// \param f The function symbol.
188/// \return The string representation of r.
189inline const std::string& pp(const function_symbol& f)
190{
191 return f.name();
192}
193
194/// \brief Writes term t to a stream in binary aterm format.
196
197/// \brief Reads a term from a stream in binary aterm format.
198void read_term_from_binary_stream(std::istream& is, aterm& t);
199
200/// \brief Writes term t to a stream in textual format.
201void write_term_to_text_stream(const aterm& t, std::ostream& os);
202
203/// \brief Reads a term from a stream which contains the term in textual format.
204void read_term_from_text_stream(std::istream& is, aterm& t);
205
206/// \brief Reads an aterm from a string. The string can be in either binary or text format.
207aterm read_term_from_string(const std::string& s);
208
209/// \brief Reads an aterm_list from a string. The string can be in either binary or text format.
210/// \details If the input is not a string, an aterm is returned of the wrong type.
211/// \return The term corresponding to the string.
213{
214 const aterm_list l = down_cast<aterm_list>(read_term_from_string(s));
215 assert(l.type_is_list());
216 return l;
217}
218
219/// \brief Reads an aterm_int from a string. The string can be in either binary or text format.
220/// \details If the input is not an int, an aterm is returned of the wrong type.
221/// \return The aterm_int corresponding to the string.
222inline aterm_int read_int_from_string(const std::string& s)
223{
224 const aterm_int n = down_cast<aterm_int>(read_term_from_string(s));
225 assert(n.type_is_int());
226 return n;
227}
228
229/// \brief Reads an aterm from a string. The string can be in either binary or text format.
230/// \details If the input is not an aterm, an aterm is returned of the wrong type.
231/// \return The term corresponding to the string.
232inline aterm read_appl_from_string(const std::string& s)
233{
234 const aterm a = read_term_from_string(s);
235 assert(a.type_is_appl());
236 return a;
237}
238
239} // namespace atermpp
240
241#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
The interface for a class that reads aterm from a stream. The default constructed term aterm() indica...
Definition aterm_io.h:62
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:51
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:104
aterm_stream_state(aterm_stream_state &&)=delete
aterm_stream_state & operator=(aterm_stream_state &&)=delete
aterm_transformer * m_transformer
Definition aterm_io.h:123
aterm_stream_state(aterm_stream &stream)
Definition aterm_io.h:106
aterm_stream_state(const aterm_stream_state &)=delete
aterm_stream_state & operator=(const aterm_stream_state &)=delete
aterm_stream & m_stream
Definition aterm_io.h:122
The general aterm stream interface, which enables the use of a transformer to change the written/read...
Definition aterm_io.h:34
aterm_transformer * m_transformer
Definition aterm_io.h:45
aterm_transformer * get_transformer() const
Definition aterm_io.h:42
void set_transformer(aterm_transformer transformer)
Sets the given transformer to be applied to following writes.
Definition aterm_io.h:39
thread_aterm_pool(aterm_pool &global_pool)
std::stack< std::reference_wrapper< _aterm > > m_todo
A reusable todo stack.
A unordered_map class in which aterms can be stored.
thread_aterm_pool & g_thread_term_pool()
A reference to the thread local term pool storage.
aterm_pool & g_aterm_pool_instance()
A reference to the global term pool storage.
thread_aterm_pool * g_main_thread_pool
Pointer to the main thread's pool; set on its first access and used by ~thread_aterm_pool to transfer...
mcrl2::utilities::shared_guard lock_shared_aterm_pool()
Acquires a shared lock on the global thread aterm pool.
std::array< std::byte, sizeof(aterm_pool)> g_aterm_pool_storage
Storage for a global term pool that is not initialized.
The main namespace for the aterm++ library.
aterm identity(const aterm &x)
The default transformer that maps each term to itself.
Definition aterm_io.h:26
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:93
void read_term_from_binary_stream(std::istream &is, aterm &t)
Reads a term from a stream in binary aterm format.
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:232
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:222
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:73
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:212