From 0e83bc31dcc441ef642ced9daef51a91991cccb9 Mon Sep 17 00:00:00 2001 From: "Maarten L. Hekkelman" Date: Tue, 30 Jan 2024 16:29:22 +0100 Subject: [PATCH] some small optimisations --- include/cif++/condition.hpp | 2 +- include/cif++/item.hpp | 53 +++++++++++++++++++++++++++++-------- src/item.cpp | 4 +-- src/pdb/reconstruct.cpp | 4 +-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/include/cif++/condition.hpp b/include/cif++/condition.hpp index 5ed2038..d72e89d 100644 --- a/include/cif++/condition.hpp +++ b/include/cif++/condition.hpp @@ -370,7 +370,7 @@ namespace detail { key_equals_condition_impl(item &&i) : m_item_name(i.name()) - , m_value(i.value()) + , m_value(std::forward(i).value()) { } diff --git a/include/cif++/item.hpp b/include/cif++/item.hpp index 58b3768..5013e9f 100644 --- a/include/cif++/item.hpp +++ b/include/cif++/item.hpp @@ -120,8 +120,6 @@ class item if (r.ec != std::errc()) throw std::runtime_error("Could not format number"); - assert(r.ptr >= buffer and r.ptr < buffer + sizeof(buffer)); - *r.ptr = 0; m_value.assign(buffer, r.ptr - buffer); } @@ -141,8 +139,6 @@ class item if (r.ec != std::errc()) throw std::runtime_error("Could not format number"); - assert(r.ptr >= buffer and r.ptr < buffer + sizeof(buffer)); - *r.ptr = 0; m_value.assign(buffer, r.ptr - buffer); } @@ -158,8 +154,6 @@ class item if (r.ec != std::errc()) throw std::runtime_error("Could not format number"); - assert(r.ptr >= buffer and r.ptr < buffer + sizeof(buffer)); - *r.ptr = 0; m_value.assign(buffer, r.ptr - buffer); } @@ -174,12 +168,21 @@ class item /// \brief constructor for an item with name \a name and as /// content value \a value - item(const std::string_view name, const std::string_view value) + item(const std::string_view name, std::string_view value) : m_name(name) , m_value(value) { } + /// \brief constructor for an item with name \a name and as + /// content value \a value + template, int> = 0> + item(const std::string_view name, T &&value) + : m_name(name) + , m_value(std::move(value)) + { + } + /// \brief constructor for an item with name \a name and as /// content the optional value \a value template @@ -219,7 +222,8 @@ class item /** @endcond */ std::string_view name() const { return m_name; } ///< Return the name of the item - std::string_view value() const { return m_value; } ///< Return the value of the item + std::string_view value() const & { return m_value; } ///< Return the value of the item + std::string value() const && { return std::move(m_value); } ///< Return the value of the item /// \brief replace the content of the stored value with \a v void value(std::string_view v) { m_value = v; } @@ -363,8 +367,35 @@ struct item_handle template item_handle &operator=(const T &value) { - item v{ "", value }; - assign_value(v); + assign_value(item{ "", value }.value()); + return *this; + } + + /** + * @brief Assign value @a value to the item referenced + * + * @tparam T Type of the value + * @param value The value + * @return reference to this item_handle + */ + template + item_handle &operator=(T &&value) + { + assign_value(item{ "", std::move(value) }.value()); + return *this; + } + + /** + * @brief Assign value @a value to the item referenced + * + * @tparam T Type of the value + * @param value The value + * @return reference to this item_handle + */ + template + item_handle &operator=(const char (&value)[N]) + { + assign_value({ "", std::move(value) }); return *this; } @@ -508,7 +539,7 @@ struct item_handle uint16_t m_item_ix; row_handle &m_row_handle; - void assign_value(const item &value); + void assign_value(std::string_view value); }; // So sad that older gcc implementations of from_chars did not support floats yet... diff --git a/src/item.cpp b/src/item.cpp index e4cfb9c..04cbb1e 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -52,10 +52,10 @@ std::string_view item_handle::text() const return {}; } -void item_handle::assign_value(const item &v) +void item_handle::assign_value(std::string_view value) { assert(not m_row_handle.empty()); - m_row_handle.assign(m_item_ix, v.value(), true); + m_row_handle.assign(m_item_ix, value, true); } void item_handle::swap(item_handle &b) diff --git a/src/pdb/reconstruct.cpp b/src/pdb/reconstruct.cpp index 26ae7f6..f761da5 100644 --- a/src/pdb/reconstruct.cpp +++ b/src/pdb/reconstruct.cpp @@ -325,7 +325,7 @@ void checkChemCompRecords(datablock &db) items.emplace_back(item{ "formula_weight", compound->formula_weight() }); if (not items.empty()) - chem_comp_entry.assign(std::move(items)); + chem_comp_entry.assign(items); } } } @@ -412,7 +412,7 @@ void checkAtomRecords(datablock &db) items.emplace_back(item{ "formula_weight", compound->formula_weight() }); if (not items.empty()) - chem_comp_entry.assign(std::move(items)); + chem_comp_entry.assign(items); } if (is_peptide and not has_seq_id(k))