20 template <
class Element,
class NameField,
class IdField>
class database {
55 while (file.ignore(element_type::line_size + 1))
65 for (
auto i = 0u; i < count; ++i) {
103 while (file.ignore(element_type::line_size + 1))
146 typename NameField::value_type name;
147 if (reactions::fields::read_field<typename NameField::range_type>(
150 "Error reading the database; data format not understood");
152 if (new_element.template get<NameField>() == name)
154 "Attempt to register an element with similar name to an " 155 "element in the database");
157 typename IdField::value_type id;
158 if (reactions::fields::read_field<typename IdField::range_type>(
161 "Error reading the database; data format not understood");
163 if (new_element.template get<IdField>() ==
id)
165 "Attempt to register an element with similar ID to an " 166 "element in the database");
228 return cache_status::user;
230 return cache_status::full;
278 template <
class ElementReader>
284 for (
auto i = 0u; i < n; ++i) {
286 auto new_element = func();
294 el.template get<NameField>() ==
295 new_element.template get<NameField>() ||
296 el.template get<IdField>() ==
297 new_element.template get<IdField>());
300 (
std::string{
"User-defined element clashes with database " 302 new_cache.
back().name() +
"\"")
319 template <
class... Args>
325 return (el.template get<NameField>() ==
326 new_element.template get<NameField>() ||
327 el.template get<IdField>() ==
328 new_element.template get<IdField>());
331 (
std::string{
"User-registered element clashes: \""} +
332 new_element.name() +
"\"")
387 template <std::
size_t I>
391 using field = std::tuple_element_t<I, typename element_type::fields_type>;
393 if constexpr (fields::is_optional_field_v<field>) {
395 typename field::value_type::value_type value;
397 auto sc = reactions::fields::read_field<typename field::range_type>(
401 std::get<I>(tuple).emplace(value);
405 return reactions::fields::read_field<typename field::range_type>(
406 std::get<I>(tuple), line) !=
414 return (read_field<I>(tuple, line) && ...);
432 "Error reading the database; data format not understood");
444 if (el.template get<Field>() == v)
451 for (
auto const &el : m_cache)
452 if (el.template get<Field>() == v)
467 typename Field::value_type ref;
468 auto sc = reactions::fields::read_field<typename Field::range_type>(
473 "Error reading the database; data format not understood");
486 (
std::string{
"Unable to find element with "} + Field::title +
" \"" +
bool read_line(typename element_type::base_type &tuple, std::string const &line) const
Read all the fields from a line.
Definition: database.hpp:418
the conversion succeeded
Definition: fields.hpp:366
const_iterator_type begin() const
Begining of the cache.
Definition: database.hpp:242
std::string to_string(T const &v)
Convert the given object to a string.
Definition: fields.hpp:554
Base database class.
Definition: database.hpp:20
virtual element_type operator()(int id) const =0
Create an element accessing by ID.
cache_type & elements()
Underlying vector of elements.
Definition: database.hpp:239
void register_element(Args &&... args)
Register a new element.
Definition: database.hpp:130
Definition: database.hpp:202
cache_status status() const
Status of the cache.
Definition: database.hpp:225
A PDG particle, based on the fields of a particle in the PDG database.
Definition: pdg.hpp.in:138
Exceptions that can be thrown when running the functions of the package.
element_type const & add_user_element(Args &&... args)
Add a new element (by the user)
Definition: database.hpp:320
T make_move_iterator(T... args)
the conversion failed
Definition: fields.hpp:368
Raised whenever a problem is detected in the database.
Definition: exceptions.hpp:101
typename cache_type::const_iterator const_iterator_type
Definition: database.hpp:205
cache_status
Code to define the status of the cache.
Definition: database.hpp:202
const_iterator_type user_registered_cend() const
End of the user-registered elements.
Definition: database.hpp:261
T shrink_to_fit(T... args)
std::string m_db
Path to the database file.
Definition: database.hpp:195
void operator=(database &&)=delete
class reactions::database::database::cache m_cache
bool read_line(typename element_type::base_type &tuple, std::string const &line, std::index_sequence< I... >) const
Read all the fields from a line.
Definition: database.hpp:412
void clear()
Clear the cache.
Definition: database.hpp:211
pdg_element_base base_type
Base class.
Definition: pdg.hpp.in:142
Raised when an element is not found within a database.
Definition: exceptions.hpp:88
Common tools and base objects to handle databases.
Definition: database.hpp:15
const_iterator_type database_cbegin() const
Begining of the database elements.
Definition: database.hpp:248
const_iterator_type end() const
End of the cache.
Definition: database.hpp:245
void enable_cache()
Definition: database.hpp:92
Definition: database.hpp:202
virtual ~database()
Definition: database.hpp:25
size_type size() const
Number of cached elements.
Definition: database.hpp:274
void set_database_path(std::string const &s)
Definition: database.hpp:179
the object is missing
Definition: fields.hpp:367
Common operations on fields.
database(std::string &&db)
Definition: database.hpp:491
std::string const & get_database_path() const
Get the path to the database file.
Definition: database.hpp:122
Definition: database.hpp:202
void clear_database_elements()
Clear the cache.
Definition: database.hpp:218
const_iterator_type database_cend() const
End of the database elements.
Definition: database.hpp:251
cache_type const & elements() const
Underlying vector of elements.
Definition: database.hpp:236
Element element_type
Definition: database.hpp:23
typename cache_type::size_type size_type
Definition: database.hpp:206
std::vector< element_type > all_elements() const
All the elements in the database file.
Definition: database.hpp:36
element_type access(T const &v) const
Access an element using the field accessor.
Definition: database.hpp:438
void disable_cache()
Disable the cache.
Definition: database.hpp:84
cache_type m_vector
Collection of elements.
Definition: database.hpp:341
element_type read_element(std::string const &line) const
Advance to the next element in the file and read it.
Definition: database.hpp:426
bool read_field(typename element_type::base_type &tuple, std::string const &line) const
Read a field with the given index from a line.
Definition: database.hpp:388
std::streampos skip_commented_lines(std::ifstream &file) const
Skip lines with comments (preceeded by *)
Definition: database.hpp:371
void add_database_elements(size_type n, ElementReader func)
Definition: database.hpp:279
const_iterator_type user_registered_cbegin() const
Begining of the user-registered elements.
Definition: database.hpp:256
size_type database_size() const
Number of elements associated to the database.
Definition: database.hpp:266
std::ifstream open_database() const
Open the database.
Definition: database.hpp:351
size_type m_separator
Definition: database.hpp:345
size_type user_registered_size() const
Number of user-registered elements.
Definition: database.hpp:269
Cache of elements.
Definition: database.hpp:198
T emplace_back(T... args)
void clear_cache()
Clear the cache, removing also user-registered elements.
Definition: database.hpp:81