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
16
17namespace atermpp
18{
19
24
26inline aterm identity(const aterm& x) { return x; }
27
31{
32public:
33 virtual ~aterm_stream();
34
36 void set_transformer(aterm_transformer transformer) { m_transformer = transformer; }
37
40
41protected:
43};
44
48{
49public:
50 virtual ~aterm_ostream();
51
53 virtual void put(const aterm& term) = 0;
54};
55
59{
60public:
61 virtual ~aterm_istream();
62
64 virtual void get(aterm& t) = 0;
65};
66
67// These free functions provide input/output operators for these streams.
68
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
74inline aterm_ostream& operator<<(aterm_ostream& stream, const aterm& term) { stream.put(term); return stream; }
75
77inline aterm_istream& operator>>(aterm_istream& stream, aterm& term) { stream.get(term); return stream; }
78
79// Utility functions
80
84{
85public:
87 : m_stream(stream)
88 {
90 }
91
93 {
95 }
96
97private:
100};
101
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
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
150inline
151std::ostream& operator<<(std::ostream& out, const function_symbol& f)
152{
153 return out << f.name();
154}
155
159inline const std::string& pp(const function_symbol& f)
160{
161 return f.name();
162}
163
165void write_term_to_binary_stream(const aterm& t, std::ostream& os);
166
168void read_term_from_binary_stream(std::istream& is, aterm& t);
169
171void write_term_to_text_stream(const aterm& t, std::ostream& os);
172
174void read_term_from_text_stream(std::istream& is, aterm& t);
175
177aterm read_term_from_string(const std::string& s);
178
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
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
202inline aterm read_appl_from_string(const std::string& s)
203{
204 const aterm a = read_term_from_string(s);
205 assert(a.type_is_appl());
206 return a;
207}
208
209
210} // namespace atermpp
211
212#endif // MCRL2_ATERMPP_ATERM_IO_H
Term containing an integer.
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
const std::string & name() const
Return the name of the function_symbol.
A list of aterm objects.
Definition aterm_list.h:24
bool type_is_list() const noexcept
Dynamic check whether the term is an aterm_list.
Definition aterm_core.h:72
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
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.
void read_term_from_binary_stream(std::istream &is, aterm &t)
Reads a term from a stream in binary aterm format.
std::string pp(const atermpp::aterm &t)
Transform an aterm to an ascii string.
Definition aterm.h:440
std::ostream & operator<<(std::ostream &out, const atermpp::aterm &t)
Send the term in textual form to the ostream.
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(const aterm &) aterm_transformer
A function that is applied to all terms. The resulting term should only use a subset of the original ...
Definition aterm_io.h:23
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 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