24 template <
class Element>
class decay;
44 template <
class FillElement,
class FillExpression,
class ArrowSwitch>
46 std::string::const_iterator
const &end,
47 FillElement fill_element,
48 FillExpression fill_expression,
49 ArrowSwitch arrow_switch) {
51 while (tokens::match_token<tokens::space>(sit))
54 if (tokens::match_token<tokens::left_bra>(sit))
56 "Expression starts with another expression", end - sit);
61 if (tokens::match_token<tokens::space>(sit)) {
73 }
else if (tokens::match_token<tokens::left_bra>(sit)) {
82 if (!tokens::match_token<tokens::right_bra>(sit))
84 "Expected closing braces", end - sit);
96 }
else if (tokens::match_token<tokens::right_bra>(sit)) {
105 }
else if (tokens::match_token<tokens::arrow>(sit)) {
131 template <
class Process>
133 typename Process::builder_type
builder) {
138 auto const send = str.
cend();
140 Process p{sit, send, builder};
143 if (tokens::match_token<tokens::right_bra>(sit))
176 template <
class Element>
class node final {
185 : m_type{std::move(processes::node_kind::element)},
186 m_ptr{ptr.release()} {}
190 : m_type{std::move(processes::node_kind::reaction)},
191 m_ptr{ptr.release()} {}
195 : m_type{std::move(processes::node_kind::decay)}, m_ptr{ptr.release()} {
199 node(
node &&other) : m_type{other.m_type}, m_ptr{other.m_ptr} {
200 other.m_ptr =
nullptr;
208 case (processes::node_kind::element):
209 delete ptr_as_element_wrapper();
211 case (processes::node_kind::reaction):
212 delete ptr_as_reaction();
214 case (processes::node_kind::decay):
215 delete ptr_as_decay();
217 case (processes::node_kind::unknown):
219 "A node type should always be set (internal error); please " 227 node &operator=(
node const &) =
delete;
230 bool is_element()
const {
return m_type == processes::node_kind::element; }
234 return m_type == processes::node_kind::reaction;
238 bool is_decay()
const {
return m_type == processes::node_kind::decay; }
241 processes::node_kind
type()
const {
return m_type; }
248 return ptr_as_element_wrapper()->operator->();
270 processes::node_kind m_type = processes::node_kind::unknown;
277 namespace processes::detail {
285 template <
class Element>
289 auto size = first.size();
291 if (size != second.size())
297 for (
auto i = 0u; i < size; ++i) {
299 for (
auto j = 0u; j < size; ++j) {
305 if (second[j].type() != first[i].type())
309 switch (first[i].type()) {
310 case (processes::node_kind::element):
311 if (*(first[i].ptr_as_element()) == *(second[i].ptr_as_element()))
314 case (processes::node_kind::reaction):
315 if (*(first[i].ptr_as_reaction()) == *(second[i].ptr_as_reaction()))
318 case (processes::node_kind::decay):
319 if (*(first[i].ptr_as_decay()) == *(second[i].ptr_as_decay()))
322 case (processes::node_kind::unknown):
324 "A node can not be of unknown type (internal error); please " 420 return !(*
this == other);
437 std::make_unique<reaction>(std::string::const_iterator &,
438 std::string::const_iterator
const &,
441 template <
class Process>
444 typename Process::builder_type);
463 [&](std::string::const_iterator
const &start) ->
void {
467 auto fill_reaction = [&]() ->
void {
470 auto arrow_switch = [&]() ->
void {
471 if (!m_reactants.size())
475 if (current_set == &m_products)
479 current_set = &m_products;
485 if (!m_reactants.size())
489 if (!m_products.size())
525 Element
const &
head()
const {
return *(*m_head); }
536 return (*m_head == *other.
m_head) &&
542 return !(*
this == other);
556 decay(std::string::const_iterator &&begin,
558 :
decay{begin, end, builder} {}
562 std::make_unique<decay>(std::string::const_iterator &,
565 template <
class Process>
568 typename Process::builder_type);
580 decay(std::string::const_iterator &sit,
584 bool fill_products =
false;
587 [&](std::string::const_iterator
const &start) ->
void {
590 }
else if (fill_products) {
591 this->m_products.emplace_back(
597 auto fill_decay = [&] {
601 }
else if (fill_products) {
602 this->m_products.push_back(
603 std::make_unique<decay>(sit, end,
builder));
608 auto arrow_switch = [&] {
617 fill_products =
true;
625 "No elements have been parsed", end - sit);
627 if (!m_products.size())
644 template <
class Element>
648 return processes::make_process<reaction<Element>>(str,
builder);
656 template <
class Element>
658 return make_reaction_for<Element>(str, element_traits::builder<Element>);
667 template <
class Element>
670 return processes::make_process<decay<Element>>(str,
builder);
679 return make_decay_for<Element>(str, element_traits::builder<Element>);
Description of a process where reactants generate a set of products.
Definition: processes.hpp:23
Element m_element
Underlying element.
Definition: processes.hpp:368
Syntax error with an unformatted message.
Definition: exceptions.hpp:69
Base class for types referred to a node.
Definition: processes.hpp:163
syntax_error update(std::string const &str)
Definition: exceptions.hpp:77
bool is_element() const
Check if the underlying class is an element.
Definition: processes.hpp:230
bool is_reaction() const
Check if the underlying class is a reaction.
Definition: processes.hpp:233
Element const * ptr_as_element() const
Return the pointer to the underlying object casted to an element.
Definition: processes.hpp:247
reaction< Element > make_reaction(std::string const &str)
Create a new reaction.
Definition: processes.hpp:657
element_traits::builder_tpl_t< Element > builder_type
Definition: processes.hpp:510
decay_type const * ptr_as_decay() const
Return the pointer to the underlying object casted to a decay.
Definition: processes.hpp:257
nodes_type m_products
Products.
Definition: processes.hpp:497
void process_expression(std::string::const_iterator &sit, std::string::const_iterator const &end, FillElement fill_element, FillExpression fill_expression, ArrowSwitch arrow_switch)
Internal function to process an expression.
Definition: processes.hpp:45
Exceptions that can be thrown when running the functions of the package.
reaction_type const * ptr_as_reaction() const
Return the pointer to the underlying object casted to a reaction.
Definition: processes.hpp:252
node(node &&other)
Move constructor.
Definition: processes.hpp:199
processes::node_kind type() const
Get the node type.
Definition: processes.hpp:241
node(std::unique_ptr< reaction_type > &&ptr)
Constructor from a reaction.
Definition: processes.hpp:189
Raised when unexpected problems appear, which should be reported as bugs.
Definition: exceptions.hpp:38
Element const & operator*() const
Access the underlying element by reference.
Definition: processes.hpp:362
bool check_nodes(std::vector< node< Element >> const &first, std::vector< node< Element >> const &second)
Compare two nodes.
Definition: processes.hpp:286
node(std::unique_ptr< decay_type > &&ptr)
Construction from a decay.
Definition: processes.hpp:194
Description of a process where head particle generate a set of products.
Definition: processes.hpp:24
static constexpr auto builder
Default builder for a given kind of element.
Definition: element_traits.hpp:42
nodes_type m_products
Products.
Definition: processes.hpp:635
Contains macros to define smart enumeration types.
nodes_type const & reactants() const
Get the reactants of the reaction.
Definition: processes.hpp:396
bool operator==(decay< Element > const &other) const
Comparison operator.
Definition: processes.hpp:531
Tokens allowed in a reaction or decay.
std::function< T(std::string const &)> const & builder_tpl_t
General builder type for a given kind of element.
Definition: element_traits.hpp:49
Main namespace of the Reactions package.
Definition: all.hpp:21
node_object const * object() const
Get the pointer to the underlying object.
Definition: processes.hpp:244
static const size_t size
Definition: tokens.hpp:16
bool operator!=(decay< Element > const &other) const
Comparison operator.
Definition: processes.hpp:541
bool operator==(reaction< Element > const &other) const
Compare two reactions.
Definition: processes.hpp:408
Base class for a process node.
Definition: processes.hpp:176
Template for elements of a reaction or decay.
Definition: processes.hpp:22
REACTIONS_POW_ENUM_WITH_UNKNOWN(node_kind, element, reaction, decay)
Node types.
Process make_process(std::string const &str, typename Process::builder_type builder)
Make a new process (a reaction or a decay)
Definition: processes.hpp:132
reaction< Element > make_reaction_for(std::string const &str, typename reaction< Element >::builder_type builder)
Create a new reaction with a custom builder.
Definition: processes.hpp:646
decay< Element > make_decay_for(std::string const &str, typename decay< Element >::builder_type builder)
Create a new decay with a custom builder.
Definition: processes.hpp:668
Element const * operator->() const
Access the underlying element by pointer.
Definition: processes.hpp:364
bool is_decay() const
Check if the underlying class is a decay.
Definition: processes.hpp:238
reaction(std::string::const_iterator &&begin, std::string::const_iterator const &end, builder_type builder)
Constructor from the string iterators.
Definition: processes.hpp:431
nodes_type const & products() const
Get the products of the reaction.
Definition: processes.hpp:399
Element const & head() const
Get the head particle of the decay.
Definition: processes.hpp:525
Utilities to work with element types.
decay(std::string::const_iterator &sit, std::string::const_iterator const &end, builder_type builder)
Constructor from the string iterators.
Definition: processes.hpp:580
std::optional< element_type > m_head
Head particle.
Definition: processes.hpp:633
element_type const * ptr_as_element_wrapper() const
Definition: processes.hpp:264
bool operator!=(reaction< Element > const &other) const
Compare two reactions.
Definition: processes.hpp:419
reaction(std::string::const_iterator &sit, std::string::const_iterator const &end, builder_type builder)
Constructor from the string iterators.
Definition: processes.hpp:456
nodes_type const & products() const
Get the products of the decay.
Definition: processes.hpp:528
node(std::unique_ptr< element_type > &&ptr)
Constructor from an element.
Definition: processes.hpp:184
decay(std::string::const_iterator &&begin, std::string::const_iterator const &end, builder_type builder)
Constructor from the string iterators.
Definition: processes.hpp:556
element_traits::builder_tpl_t< Element > builder_type
Underlying element type.
Definition: processes.hpp:380
decay< Element > make_decay(std::string const &str)
Create a new decay.
Definition: processes.hpp:678
nodes_type m_reactants
Reactants.
Definition: processes.hpp:495
element_wrapper(Element &&e)
Constructor given the underlying element.
Definition: processes.hpp:350
~node() noexcept(false)
Destructor.
Definition: processes.hpp:204