aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorKeith Rothman <537074+litghost@users.noreply.github.com>2021-02-17 10:18:24 -0800
committerKeith Rothman <537074+litghost@users.noreply.github.com>2021-02-17 12:03:17 -0800
commit558a753d3da5a2243a74b8d4e1c24044bdfb5c2e (patch)
tree391052b070b0965e5a73d27c204089c042499275 /common
parent9e0ca7282743afd6a17fe347c1ad3a5d1cd4070d (diff)
downloadnextpnr-558a753d3da5a2243a74b8d4e1c24044bdfb5c2e.tar.gz
nextpnr-558a753d3da5a2243a74b8d4e1c24044bdfb5c2e.tar.bz2
nextpnr-558a753d3da5a2243a74b8d4e1c24044bdfb5c2e.zip
Refactor "get only from iterator" to a utility.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
Diffstat (limited to 'common')
-rw-r--r--common/util.h23
1 files changed, 23 insertions, 0 deletions
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 <typename ForwardIterator>
+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 <typename ForwardRange> 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