mCRL2
Loading...
Searching...
No Matches
aterm.h
Go to the documentation of this file.
1// Author(s): Maurice Laveaux.
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_DETAIL_ATERM_APPL_H
11#define MCRL2_ATERMPP_DETAIL_ATERM_APPL_H
12
15
16#include <array>
17
18namespace atermpp
19{
20
21template <typename Term>
22class term_appl_iterator;
23
24class aterm;
25
26namespace detail
27{
28
32template<std::size_t N = 1>
33class _aterm_appl : public _aterm
34{
35public:
36
38 template<typename ...Terms,
39 typename std::enable_if<are_terms<Terms...>::value>::type* = nullptr,
40 typename std::enable_if<sizeof...(Terms) == N>::type* = nullptr>
41 _aterm_appl(const function_symbol& sym, const Terms& ...arguments)
42 : _aterm(sym),
43 m_arguments{{arguments...}}
44 {
45 assert(N == sym.arity()); // The arity of the function symbol matches.
46 }
47
50 template<typename Iterator,
51 typename std::enable_if<mcrl2::utilities::is_iterator<Iterator>::value>::type* = nullptr>
52 _aterm_appl(const function_symbol& sym, Iterator it, Iterator end)
53 : _aterm(sym)
54 {
55 // The end is only used for debugging to ensure that the arity and std::distance(it, end) match.
57
58 for (std::size_t i = 0; i < N; ++i)
59 {
60 assert(it != end);
61 m_arguments[i] = *it;
62 ++it;
63 }
64
65 assert(it == end);
66 }
67
69 _aterm_appl(const function_symbol& sym, std::array<unprotected_aterm_core, N> arguments)
70 : _aterm(sym),
71 m_arguments(arguments)
72 {
73 assert(sym.arity() == N);
74 }
75
77 template<typename Iterator,
78 typename std::enable_if<mcrl2::utilities::is_iterator<Iterator>::value>::type* = nullptr>
79 _aterm_appl(const function_symbol& symbol, Iterator it, Iterator end, bool)
80 : _aterm(symbol)
81 {
82 // The end is only used for debugging to ensure that the arity and std::distance(it, end) match.
84
85 for (std::size_t i = 0; i < symbol.arity(); ++i)
86 {
87 // Prevent bound checking, the allocator must make sure that symbol.arity() arguments fit.
88 assert(it != end);
89 m_arguments.data()[i] = *it;
90 ++it;
91 }
92
93 assert(it == end);
94 }
95
97 const aterm_core& arg(std::size_t index) const
98 {
99 return static_cast<const aterm_core&>(m_arguments.data()[index]);
100 }
101
103 explicit operator _aterm_appl<1>& ()
104 {
105 return reinterpret_cast<_aterm_appl<1>&>(*this);
106 }
107
108private:
109 std::array<unprotected_aterm_core, N> m_arguments;
110};
111
114
118template<typename T = _term_appl>
120{
121private:
123 constexpr static std::size_t term_appl_size(std::size_t arity)
124 {
125 return sizeof(T) + (arity - 1) * sizeof(aterm_core);
126 }
127
128public:
129 using size_type = std::size_t;
130 using pointer = T*;
131 using value_type = T;
132
133 template <class U>
134 struct rebind
135 {
137 };
138
140 template<typename ForwardIterator>
141 T* allocate_args(const function_symbol& symbol, ForwardIterator, ForwardIterator)
142 {
143 // We assume that object T contains the _aterm_appl<aterm, 1> at the end and reserve extra space for parameters.
144 char* newTerm = m_packed_allocator.allocate(term_appl_size(symbol.arity()));
145 return reinterpret_cast<T*>(newTerm);
146 }
147
151 {
152 // We assume that object T contains the _aterm_appl<aterm, 1> at the end and reserve extra space for parameters.
153 char* newTerm = m_packed_allocator.allocate(term_appl_size(symbol.arity()));
154 return reinterpret_cast<T*>(newTerm);
155 }
156
158 template<typename ForwardIterator>
159 void construct(T* element, const function_symbol& symbol, ForwardIterator begin, ForwardIterator end)
160 {
161 new (element) T(symbol, begin, end, true);
162 }
163
165 void destroy(T* element)
166 {
167 assert(element != nullptr);
168
169 // Only destroy the function symbol.
170 _term_appl& term = *element;
171 term.function().~function_symbol();
172 }
173
174 void deallocate(T* element, std::size_t)
175 {
176 assert(element != nullptr);
177
178 // Deallocate the memory of this aterm.
179 _term_appl& term = *element;
180 m_packed_allocator.deallocate(reinterpret_cast<char*>(element), term_appl_size(term.function().arity()));
181 }
182
183 // These member functions are to ensure parity with the memory_pool.
184 constexpr std::size_t capacity() const { return 0; }
185 constexpr std::size_t consolidate() const noexcept { return 0; }
186 constexpr bool has_free_slots() const noexcept { return false; }
187
188private:
189 std::allocator<char> m_packed_allocator;
190};
191
192static_assert(sizeof(_term_appl) == sizeof(_aterm) + sizeof(aterm_core), "Sanity check: aterm size");
193
194template < class Derived, class Base >
196 typename std::enable_if<
197 std::is_base_of<aterm, Base>::value &&
198 std::is_base_of<aterm, Derived>::value
199>::type* = nullptr);
200
201} // namespace detail
202} // namespace atermpp
203
204#endif // MCRL2_ATERMPP_DETAIL_ATERM_APPL_H
The aterm_core base class that provides protection of the underlying shared terms.
Definition aterm_core.h:182
This class allocates _aterm_appl objects where the size is based on the arity of the function symbol.
Definition aterm.h:120
T * allocate_args(const function_symbol &symbol, unprotected_aterm_core *)
Allocates space for an _aterm_appl where the arity is given by the function symbol.
Definition aterm.h:150
T * allocate_args(const function_symbol &symbol, ForwardIterator, ForwardIterator)
Allocates space for an _aterm_appl where the arity is given by the function symbol.
Definition aterm.h:141
std::allocator< char > m_packed_allocator
Definition aterm.h:189
static constexpr std::size_t term_appl_size(std::size_t arity)
Definition aterm.h:123
void destroy(T *element)
Specialize destroy for _aterm_appl to only destroy the function symbol. The reference count for the a...
Definition aterm.h:165
constexpr std::size_t capacity() const
Definition aterm.h:184
void deallocate(T *element, std::size_t)
Definition aterm.h:174
constexpr bool has_free_slots() const noexcept
Definition aterm.h:186
constexpr std::size_t consolidate() const noexcept
Definition aterm.h:185
void construct(T *element, const function_symbol &symbol, ForwardIterator begin, ForwardIterator end)
Constructs an _aterm_appl with arguments taken from begin, the arity is given by the function symbol.
Definition aterm.h:159
This class stores a term followed by N arguments. Where N should be equal to the arity of the functio...
Definition aterm.h:34
_aterm_appl(const function_symbol &sym, const Terms &...arguments)
Constructs a term application with the given symbol and arguments.
Definition aterm.h:41
_aterm_appl(const function_symbol &symbol, Iterator it, Iterator end, bool)
constructs a term application with the given symbol and its arguments from the iterator.
Definition aterm.h:79
_aterm_appl(const function_symbol &sym, std::array< unprotected_aterm_core, N > arguments)
Constructs a term application with the given symbol and arguments.
Definition aterm.h:69
std::array< unprotected_aterm_core, N > m_arguments
Definition aterm.h:109
_aterm_appl(const function_symbol &sym, Iterator it, Iterator end)
constructs a term application with the given symbol and an iterator where the number of elements is e...
Definition aterm.h:52
const aterm_core & arg(std::size_t index) const
Definition aterm.h:97
This is the class to which an aterm points.
Definition aterm_core.h:48
const function_symbol & function() const noexcept
Definition aterm_core.h:55
std::size_t arity() const
Return the arity (number of arguments) of the function symbol (function_symbol).
Iterator for term_appl.
An unprotected term does not change the reference count of the shared term when it is copied or moved...
Definition aterm_core.h:32
term_appl_iterator< Derived > aterm_appl_iterator_cast(term_appl_iterator< Base > a, typename std::enable_if< std::is_base_of< aterm, Base >::value &&std::is_base_of< aterm, Derived >::value >::type *=nullptr)
This function can be used to translate an term_appl_iterator of one sort into another.
_aterm_appl<> _term_appl
A default instantiation for the underlying term application.
Definition aterm.h:113
The main namespace for the aterm++ library.
Definition algorithm.h:21
void mcrl2_unused(T &&...)
Function that can be used to silence unused parameter warnings.
Definition unused.h:20
_aterm_appl_allocator< U > other
Definition aterm.h:136
Checks whether condition holds for all types passed as variadic template.
Definition type_traits.h:41
add your file description here.