This new function template returns whether or not some STL container
contains some value instead of using the awkward find() and end()
methods each time.
Similar to new STL contains() methods of C++20.
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Idea480f9001272dfaf1dc0df60251983be8f3962
cpp-common/bt2c/align.hpp \
cpp-common/bt2c/c-string-view.hpp \
cpp-common/bt2c/call.hpp \
+ cpp-common/bt2c/contains.hpp \
cpp-common/bt2c/dummy.cpp \
cpp-common/bt2c/endian.hpp \
cpp-common/bt2c/exc.hpp \
--- /dev/null
+/*
+ * Copyright (c) 2024 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP
+#define BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP
+
+namespace bt2c {
+
+/*
+ * Returns whether or not the STL container `container` contains the
+ * value `val`.
+ */
+template <typename T, typename V>
+bool contains(const T& container, const V& val) noexcept
+{
+ return container.find(val) != container.end();
+}
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2C_CONTAINS_HPP */