Skip to content

Commit

Permalink
some small optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
mhekkel committed Jan 30, 2024
1 parent 75a5f79 commit 0e83bc3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/cif++/condition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<item>(i).value())
{
}

Expand Down
53 changes: 42 additions & 11 deletions include/cif++/item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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<typename T, std::enable_if_t<std::is_same_v<T, std::string>, 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 <typename T>
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -363,8 +367,35 @@ struct item_handle
template <typename T>
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 <typename T>
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 <size_t N>
item_handle &operator=(const char (&value)[N])
{
assign_value({ "", std::move(value) });
return *this;
}

Expand Down Expand Up @@ -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...
Expand Down
4 changes: 2 additions & 2 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/pdb/reconstruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 0e83bc3

Please sign in to comment.