From 558a753d3da5a2243a74b8d4e1c24044bdfb5c2e Mon Sep 17 00:00:00 2001 From: Keith Rothman <537074+litghost@users.noreply.github.com> Date: Wed, 17 Feb 2021 10:18:24 -0800 Subject: Refactor "get only from iterator" to a utility. Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com> --- common/util.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'common') diff --git a/common/util.h b/common/util.h index 07ebac75..55718344 100644 --- a/common/util.h +++ b/common/util.h @@ -158,6 +158,29 @@ inline NetInfo *get_net_or_empty(CellInfo *cell, const IdString port) return nullptr; } +// Get only value from a forward iterator begin/end pair. +// +// Generates assertion failure if std::distance(begin, end) != 1. +template +inline const typename ForwardIterator::reference get_only_value(ForwardIterator begin, ForwardIterator end) +{ + NPNR_ASSERT(begin != end); + const typename ForwardIterator::reference ret = *begin; + ++begin; + NPNR_ASSERT(begin == end); + return ret; +} + +// Get only value from a forward iterator range pair. +// +// Generates assertion failure if std::distance(r.begin(), r.end()) != 1. +template inline auto get_only_value(ForwardRange r) +{ + auto b = r.begin(); + auto e = r.end(); + return get_only_value(b, e); +} + NEXTPNR_NAMESPACE_END #endif -- cgit v1.2.3