19 template <
class Element,
class NameField,
class IdField>
class database {
54 while (file.ignore(element_type::line_size + 1))
64 for (
auto i = 0u; i < count; ++i) {
102 while (file.ignore(element_type::line_size + 1))
145 typename NameField::value_type name;
146 if (reactions::fields::read_field<typename NameField::range_type>(
149 "Error reading the database; data format not understood");
151 if (new_element.template get<NameField>() == name)
153 "Attempt to register an element with similar name to an " 154 "element in the database");
156 typename IdField::value_type id;
157 if (reactions::fields::read_field<typename IdField::range_type>(
160 "Error reading the database; data format not understood");
162 if (new_element.template get<IdField>() ==
id)
164 "Attempt to register an element with similar ID to an " 165 "element in the database");
227 return cache_status::user;
229 return cache_status::full;
277 template <
class ElementReader>
283 for (
auto i = 0u; i < n; ++i) {
285 auto new_element = func();
293 el.template get<NameField>() ==
294 new_element.template get<NameField>() ||
295 el.template get<IdField>() ==
296 new_element.template get<IdField>());
299 (
std::string{
"User-defined element clashes with database " 301 new_cache.
back().name() +
"\"")
318 template <
class... Args>
324 return (el.template get<NameField>() ==
325 new_element.template get<NameField>() ||
326 el.template get<IdField>() ==
327 new_element.template get<IdField>());
330 (
std::string{
"User-registered element clashes: \""} +
331 new_element.name() +
"\"")
386 template <std::
size_t I>
390 using field = std::tuple_element_t<I, typename element_type::fields_type>;
392 if constexpr (fields::is_optional_field_v<field>) {
394 typename field::value_type::value_type value;
396 auto sc = reactions::fields::read_field<typename field::range_type>(
400 std::get<I>(tuple).emplace(value);
404 return reactions::fields::read_field<typename field::range_type>(
405 std::get<I>(tuple), line) !=
413 return (read_field<I>(tuple, line) && ...);
427 typename element_type::base_type tuple;
431 "Error reading the database; data format not understood");
443 if (el.template get<Field>() == v)
450 for (
auto const &el : m_cache)
451 if (el.template get<Field>() == v)
466 typename Field::value_type ref;
467 auto sc = reactions::fields::read_field<typename Field::range_type>(
472 "Error reading the database; data format not understood");
485 (
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:417
the conversion succeeded
Definition: fields.hpp:358
const_iterator_type begin() const
Begining of the cache.
Definition: database.hpp:241
std::string to_string(T const &v)
Convert the given object to a string.
Definition: fields.hpp:546
Base database class.
Definition: database.hpp:19
virtual element_type operator()(int id) const =0
Create an element accessing by ID.
cache_type & elements()
Underlying vector of elements.
Definition: database.hpp:238
void register_element(Args &&... args)
Register a new element.
Definition: database.hpp:129
Definition: database.hpp:201
cache_status status() const
Status of the cache.
Definition: database.hpp:224
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:319
T make_move_iterator(T... args)
the conversion failed
Definition: fields.hpp:360
Raised whenever a problem is detected in the database.
Definition: exceptions.hpp:101
typename cache_type::const_iterator const_iterator_type
Definition: database.hpp:204
cache_status
Code to define the status of the cache.
Definition: database.hpp:201
const_iterator_type user_registered_cend() const
End of the user-registered elements.
Definition: database.hpp:260
T shrink_to_fit(T... args)
std::string m_db
Path to the database file.
Definition: database.hpp:194
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:411
void clear()
Clear the cache.
Definition: database.hpp:210
Raised when an element is not found within a database.
Definition: exceptions.hpp:88
Definition: database.hpp:14
const_iterator_type database_cbegin() const
Begining of the database elements.
Definition: database.hpp:247
const_iterator_type end() const
End of the cache.
Definition: database.hpp:244
void enable_cache()
Definition: database.hpp:91
Definition: database.hpp:201
virtual ~database()
Definition: database.hpp:24
size_type size() const
Number of cached elements.
Definition: database.hpp:273
void set_database_path(std::string const &s)
Definition: database.hpp:178
the object is missing
Definition: fields.hpp:359
Common operations on fields.
database(std::string &&db)
Definition: database.hpp:490
std::string const & get_database_path() const
Get the path to the database file.
Definition: database.hpp:121
Definition: database.hpp:201
void clear_database_elements()
Clear the cache.
Definition: database.hpp:217
const_iterator_type database_cend() const
End of the database elements.
Definition: database.hpp:250
cache_type const & elements() const
Underlying vector of elements.
Definition: database.hpp:235
Element element_type
Definition: database.hpp:22
typename cache_type::size_type size_type
Definition: database.hpp:205
std::vector< element_type > all_elements() const
All the elements in the database file.
Definition: database.hpp:35
element_type access(T const &v) const
Access an element using the field accessor.
Definition: database.hpp:437
void disable_cache()
Disable the cache.
Definition: database.hpp:83
cache_type m_vector
Collection of elements.
Definition: database.hpp:340
element_type read_element(std::string const &line) const
Advance to the next element in the file and read it.
Definition: database.hpp:425
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:387
std::streampos skip_commented_lines(std::ifstream &file) const
Skip lines with comments (preceeded by *)
Definition: database.hpp:370
void add_database_elements(size_type n, ElementReader func)
Definition: database.hpp:278
const_iterator_type user_registered_cbegin() const
Begining of the user-registered elements.
Definition: database.hpp:255
size_type database_size() const
Number of elements associated to the database.
Definition: database.hpp:265
std::ifstream open_database() const
Open the database.
Definition: database.hpp:350
size_type m_separator
Definition: database.hpp:344
size_type user_registered_size() const
Number of user-registered elements.
Definition: database.hpp:268
Cache of elements.
Definition: database.hpp:197
T emplace_back(T... args)
void clear_cache()
Clear the cache, removing also user-registered elements.
Definition: database.hpp:80